python - Finding words that are the reverse of each other in a file -


sorry newbie question, got starting. want simple program in file reverse words, wrote source, doesn't work. after goes second "for" loop, doesn't go first loop, instead ends program. clue?

def is_reverse(word1, word2):       if len(word1) == len(word2):      if word1 == word2[::-1]:        return true    return false  fin = open('list.txt')  word1 in fin:     word1 = word1.strip()     word1 = word1.lower()     word2 in fin:       word2 = word2.strip()       word2 = word2.lower()       print word1 + word2       if is_reverse(word1, word2) true:              print word1 + ' opposite of ' + word2  

edit: tried loop file vs list, , got curious (to me) result. if use code works:

def is_reverse(word1, word2):   if len(word1) == len(word2):       if word1 == word2[::-1]:         return true   return false  fin = open('list.txt') fin2 = ['test1','test2','test3','test4','test5'] word1 in fin:     word1 = word1.strip()     word1 = word1.lower()     word2 in fin2:       word2 = word2.strip()       word2 = word2.lower()       print word1 + word2       if is_reverse(word1, word2) true:              print word1 + ' opposite of ' + word2 

if exchange fin , fin2 first loop 1 itineration. can explain me why?

for word1 in fin iterates line-by-line, word1 line, not word. intended?

for word2 in fin using same iterator, think consume input, , for word1 in fin done once.

so simplest change have 2 files, file1 , file2, , re-open file2 every pass thru loop.

def is_reverse(word1, word2):       if len(word1) == len(word2):      if word1 == word2[::-1]:        return true    return false  file1 = open('list.txt')  word1 in file1:     word1 = word1.strip()     word1 = word1.lower()     file2 = open('list.txt')     word2 in file2:       word2 = word2.strip()       word2 = word2.lower()       print word1 + word2       if is_reverse(word1, word2):              print word1 + ' opposite of ' + word2  

but better way read files once list , iterate on list rather file, e.g.

def is_reverse(word1, word2):     if len(word1) == len(word2):         if word1 == word2[::-1]:             return true     return false  file = open('list.txt') words = list(file) word1 in words:     word1 = word1.strip()     word1 = word1.lower()     word2 in words:         word2 = word2.strip()         word2 = word2.lower()         print word1 + word2         if is_reverse(word1, word2):             print word1 + ' opposite of ' + word2  

to answer other question, why can iterate on same list twice not on same file:

a for element in iterable loop asks iterable iterator calling iterable.__iter__.

when python asks file iterator, file returns itself. means every iterator on file shares same state/position.

>>> file = open('testfile.txt') >>> it1 = iter(file) >>> it2 = iter(file) >>> id(it1) 3078689064l >>> id(it2) 3078689064l >>> id(file) 3078689064l 

when ask list iterator, different iterator every time, own separate information position.

>>> list = [1,2,3] >>> it3 = iter(list) >>> it4 = iter(list) >>> id(it3) 3078746156l >>> id(it4) 3078746188l >>> id(list) 3078731244l 

postscript

as hugh points out, iterating on list of words each word going inefficient.

here's way that's faster. change list.txt big file, e.g. /usr/share/dict/words on linux system see mean.

words = [] wordset = set(())  file = open('list.txt') line in file:     word = line.strip('\n')     words.append(word)     wordset.add(word)  word in words:     reversed = word[::-1]     if reversed in wordset:         print word + ' opposite of ' + reversed 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -