I need to define a function called find_anagrams(str) which returns a lists of all words in dictionary.txt that are anagrams of the input string. I already have a code which returns words from a list that are anagrams of a string.
def find_anagrams_in_word_list(str1,str_list):
anagrams = []
for word in str_list:
if len(word) == len(str1):
if (anagram(str1,word)):
anagrams.append(word)
print(word)
and a code which opens the dictionary file
def get_dictionary_word_list():
with open('dictionary.txt') as f:
return f.read().split
How do i send the input string and the word list (get_dictionary_word_list) to find_anagrams_in_word_list to get the result?
so if the str was 'advar' i would like it to return 'varad', 'davar', 'ravad' (assuming they are in the word file)
As in your previous question if
sorted(str1) == sorted(str2)
returnsTrue
you have an anagram. There is very little difference using a file, all you need is to split file into individual words, strip any whitespace and use a list comprehension again to compare:Or using your own code:
If you are using
split
you don't needrstrip