What i am trying to do is take a text file and return a dictionary of the anagrams (words that make a new word when rearranged alphabetically) in the file. So if the file contained the words dub and bud, then the code should create a key bdu and attach the strings dub and bud to them in a tuple or list or what have you.
Now my code outputs exactly what i want, except that instead of one key with multiple values, im getting identical keys for every value. To draw back to my previous example, i get the key bdu for dub, then another bdu key for bud. How would i remove identical keys and merge key values to one key?
def anagrams(f):
'''takes a file and returns a list of anagrams in the file'''
wordget = open(f).read().lower().split()
dic = {}
for w in wordget:
if ("".join(sortword(w))) in wordget:
dic = {("".join(sortword(w))):w}
for key in dic.keys():
print "'%s': %s" % (key, dic[key])
return None
Any help would be appreciated. I hope to come up with a solution that runs quick too, even with files containing tens of thousands of words (like books)