[英]A function that takes two matrices as input, and returns a matrix with A * B. In Python
[英]check for anagram in pair of input given as phrase = A, B. where A and B are 2 words
這是我寫的代碼。 但它為每個字母表給出真/假。 我希望輸出給我一個真或假。 我應該做哪些改變?
Z = input()
Y = Z.split()
M = list(Y[0])
N = list(Y[1])
M.pop(-1)
for i in range(len(N)):
print(M.count(M[i]) == N.count(N[i]))
我會采取另一種方法。 如果您對兩個單詞的字母進行排序並將它們相互比較,那么如果單詞是彼此的字謎,您將得到一個True
:
>>> def is_anagram(word1, word2):
... return sorted(word1) == sorted(word2)
>>> is_anagram('elbow', 'below')
>>> True
>>> is_anagram('elbow', 'lower')
>>> False
檢查我的代碼:
A = "a decimal point".replace(" ","")
B = "i m a dot in place".replace(" ","")
def checkAnagram(A,B):
for i in range(len(A)):
if(A.count(A[i])!=B.count(A[i])):
return False
return True
if(len(A)!=len(B)):
print("Pair is not anagram.")
else:
if(checkAnagram(A,B)):
print("pair is anagram")
else:
print("Pair is not Anagram")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.