我试过这个,我希望程序将S1中的每个字母与S2中的每个字母进行比较,有什么想法吗?
s1= "Army"
s2= "Mary"
def anagram(S1,S2):
q=0
w=0
S1.lower()
S2.lower()
for i in S1:
if S1[q]==S2[w]:
print S1[q]
else:
q+=1
w+=1
anagram(s1,s2)
答案 0 :(得分:1)
考虑使用collections.counter
:
import collections
s1 = "Army"
s2 = "Mary"
s3 = "Zary"
def anagram(s1,s2):
return collections.Counter(s1.lower()) == collections.Counter(s2.lower())
print anagram(s1,s2) # True
print anagram(s1, s3) # False
试试here!