fin = open('words.txt')
the_dict = {}
"""the dictionary to store all of the letters words are made with. The keys are the letters that the words are made with
and the objects are the words that can be made with them (anagrams)"""
greatest_length = 0 # Most anagrams made from set of letters
for line in fin:
    tuple_test = () # the keys are tuples
    word = line.strip()
    tuple_test = tuple(sorted(word))
    if tuple_test in the_dict: # has that tuple of keys been inserted into the dictionary or not
        the_dict[tuple_test].append(word) # it has been so add the word to that list
        if len(the_dict[tuple_test]) >= greatest_length:
               greatest_length = len(the_dict[tuple_test]) # find the greatest number of objects per key
    else: # the tuple key has not been added to the dictionary yet so add it
        the_dict[tuple_test] = [word]
test_length = greatest_length
anagrams_found = 0
for i in range (greatest_length):
    for anagram in the_dict:
        if (len(the_dict[anagram]) ==test_length) and (len(the_dict[anagram]) > 1): # print all keys with more than one object (itself)
            print the_dict[anagram]
            anagrams_found +=1 # print them in order of length
    test_length -=1
print anagrams_found

# 2 longest anagrams
#['apers', 'asper', 'pares', 'parse', 'pears', 'prase', 'presa', 'rapes', 'reaps', 'spare', 'spear']
#['alerts', 'alters', 'artels', 'estral', 'laster', 'ratels', 'salter', 'slater', 'staler', 'stelar', 'talers']
# Apparently there were a total of 10,157 anagrams
