我在寻找字谜函数时遇到了问题。我想创建一个字典,它包含所有单词的字谜列表。sig变量是按字母顺序排列的单词的签名。这本词典以 d={“word1”:[word2,word3,word4],“word5”:[word10,word9,word20]等…} 输入文件包含英语单词列表
def get_signature(word):
"""takes a word breaks it into a list and sort it alph, and then
back into a word
"""
l = word.split()
l.sort()
w = ''.join(l)
return w
def is_anagram(sig, word):
"""takes a given number of letters and compare to another
word to see if they are anagrams: must be same length
"""
if len(word) != len(sig): #if words not same len return False
print "here1"
return False
for ch in sig:
if ch not in word:
return False
return True
def finding_anagrams(fin):
"""Ex 12-4 this funcion reads a wordlist and find the words that makes up
the most anagrams
"""
#read the wordlist file
d = {}
for line in fin:
word = line.strip().lower()
sig = get_signature(word) #put the letters in alphabetical
if sig not in d:
d[sig] = []
for l in fin:
w = l.strip().lower()
print w, sig, "here"
if is_anagram(sig, w):
d[sig].append(w)
return d
def print_anagrams(number, d):
"""prints all anagrams of given word
"""
for key, value in d.items():
if len(key) == number:
print key, d[key]
main()
filein = open("words.txt")
anagrams = finding_anagrams(filein)
print_anagrams(5, anagrams)
if __name__ == "__main__":
main()
您在file对象上迭代了两次,在第一个内部循环之后,您已经耗尽了迭代器。使用defaultdict将更有效,仅调用单词上的sorted也将避免不必要的函数调用
您还可以删除对is\u anagram的需要,这将反过来删除对sig的需要:
相关问题 更多 >
编程相关推荐