I try to write a python script that searches a txt file (english dictionary) for anagrams. I have those three functions:
def is_anagram(a,b):
a_ = list(a)
a_.sort()
b_ = list(b)
b_.sort()
if a_ == b_ and a != b:
return True
else:
return False
def find_anagrams(word,t):
_res=[word]
for line in t:
check = line.strip()
if is_anagram(check,word):
_res += [check]
return _res
def find_all_anagrams(f):
res = {}
void = []
for line in f:
word = line.strip()
_list = list(word)
_list.sort()
key = tuple(''.join(_list))
if key not in res and key not in void:
if find_anagrams(word,f) == []:
void += [key]
res[key] = find_anagrams(word,f)
return res
If i call the find_all_anagrams function with:
fin = open ('words.txt')
print find_all_anagrams(fin)
The program stops after the first loop and just gives me
{('a', 'a'): ['aa']}
Why does it not continue and process the second line of words.txt? Btw the words.txt file is the one from Moby Project that can be downloaded here(http://thinkpython.com/code/words.txt)