【发布时间】:2018-11-23 14:21:11
【问题描述】:
我正在尝试对列表中的项目应用验证。我已经设法打开它,但正在努力比较这两个词是否是字谜。
这是我在终端中的结果。
anagram: ['word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff']
Anagram
在这个示例中,很明显我对两个相同的变量 A、B 做错了,但不知道该怎么做。
word1 = open('a.txt', 'r').read().split()
word2 = open('a.txt', 'r').read().split()
count = {}
validation = True
if len(a) == len(b):
for i in range(len(a)):
if a[i] in count:
count[a[i]] += 1
else:
count[a[i]] = 1
if b[i] in count:
count[b[i]] += 1
else:
count[b[i]] = 1
for i in count:
if count[i] % 2 == 0:
validation = "Anagram"
else:
validation = "Not Anagram"
break
else:
validation = "Not Anagram"
print(validation)
我到底在做什么?
我想在终端中实现这个。
anagram: ['word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff']
anagram, not anagram, anagram, not anagram, anagram, not anagram, anagram, not anagram
【问题讨论】:
-
也许提供您想要达到的预期结果。
-
完成。见上文^