[英]check for anagram in pair of input given as phrase = A, B. where A and B are 2 words
this is the code I wrote.这是我写的代码。 but it gives True/False for each alphabet.但它为每个字母表给出真/假。 I want the output to give me a single true or false.我希望输出给我一个真或假。 what changes should I make?我应该做哪些改变?
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]))
I would take another approach.我会采取另一种方法。 If you sort the letters of both words and compare them against each other, then you will get a single True
if the words are anagrams of each other:如果您对两个单词的字母进行排序并将它们相互比较,那么如果单词是彼此的字谜,您将得到一个True
:
>>> def is_anagram(word1, word2):
... return sorted(word1) == sorted(word2)
>>> is_anagram('elbow', 'below')
>>> True
>>> is_anagram('elbow', 'lower')
>>> False
Check my code:检查我的代码:
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.