I'm trying to create a new file of rows of anagrams from a current file.
def Ana(str1, str2):
str1_list = list(str1)
str1_list.sort()
str2_list = list(str2)
str2_list.sort()
return (str1_list == str2_list)
newerfile=open("ana.txt","w")
f = open("words.txt")
for word in f:
s = str(word)
for word2 in f:
if word!=word2:
if Ana(word, word2) is True:
s += (' ') + str(word2)
if s!=str(word):
newerfile.write(s)
The above is my current code, but all it gives me is an empty file. After experimenting a bit, I believe the problem is at the 4th last line - "if isAnagram(word, word2) if True:"
The function is not working for the file I have. I've tried a more basic version of the code to test the entire file against one word. Since the words 'was' and 'saw' are in the file, I should be getting those, but nothing is being printed.
y = 'was'
for line in open('real_words.txt'):
if isAnagram(line,'was') is True:
y += (' ') + str(line)
print(y)
The function is working fine when I provide a list of words, but not for a file. Any help is appreciated.
Also is there any way to delete the word and all anagrams from the file if function returns true?
(Assuming
words.txt
is a file with one word per line, and you are trying to find all pairs of anagrams of those words and print them to a second file, one pair per line.)There seem to be two problems with your code:
for word in f
you are using an iterator, and withfor word2 in f
you use the same iterator, i.e., that iterator will be exhausted after the first iteration of the loop!s
will always hold just the last pair of words, so you would write just that pair to the file. (Could just be a problem with indentation.)For getting all the combinations of two words, it would be best to use
itertools.combinations
, somewhat like this (untested, kind of pseudo-code):However, this will write just one pair of anagrams per line. If you want to write entire groups into one line, I guess you will need two loops, like in your code. Just remember not to use the same iterator for both loops, e.g. put the contents of the filt into a list first, and then use that list for the loops.
You could also use a list comprehension for this:
(Note that this still is not perfect, as the lines will be repeated, once for each word in the group. But I'm sure you can figure out the rest by yourself.)