- Finding words that are the reverse of each other in a file
- def is_reverse(word1, word2):
- if len(word1) == len(word2):
- if word1 == word2[::-1]:
- return True
- return False
- fin = open('List.txt')
- for word1 in fin:
- word1 = word1.strip()
- word1 = word1.lower()
- for word2 in fin:
- word2 = word2.strip()
- word2 = word2.lower()
- print word1 + word2
- if is_reverse(word1, word2) is True:
- print word1 + ' is the opposite of ' + word2
- 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']
- for word1 in fin:
- word1 = word1.strip()
- word1 = word1.lower()
- for word2 in fin2:
- word2 = word2.strip()
- word2 = word2.lower()
- print word1 + word2
- if is_reverse(word1, word2) is True:
- print word1 + ' is the opposite of ' + word2
- def is_reverse(word1, word2):
- if len(word1) == len(word2):
- if word1 == word2[::-1]:
- return True
- return False
- file1 = open('List.txt')
- for word1 in file1:
- word1 = word1.strip()
- word1 = word1.lower()
- file2 = open('List.txt')
- for word2 in file2:
- word2 = word2.strip()
- word2 = word2.lower()
- print word1 + word2
- if is_reverse(word1, word2):
- print word1 + ' is the opposite of ' + word2
- 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)
- for word1 in words:
- word1 = word1.strip()
- word1 = word1.lower()
- for word2 in words:
- word2 = word2.strip()
- word2 = word2.lower()
- print word1 + word2
- if is_reverse(word1, word2):
- print word1 + ' is the opposite of ' + word2
- >>> file = open('testfile.txt')
- >>> it1 = iter(file)
- >>> it2 = iter(file)
- >>> id(it1)
- 3078689064L
- >>> id(it2)
- 3078689064L
- >>> id(file)
- 3078689064L
- >>> list = [1,2,3]
- >>> it3 = iter(list)
- >>> it4 = iter(list)
- >>> id(it3)
- 3078746156L
- >>> id(it4)
- 3078746188L
- >>> id(list)
- 3078731244L
- words = []
- wordset = set(())
- file = open('List.txt')
- for line in file:
- word = line.strip('n')
- words.append(word)
- wordset.add(word)
- for word in words:
- reversed = word[::-1]
- if reversed in wordset:
- print word + ' is the opposite of ' + reversed
- def getWords(fname):
- with open(fname) as inf:
- words = list(w.strip().lower() for w in inf)
- ws = set(words)
- words = list(ws)
- words.sort()
- return words, ws
- def wordsInReverse(words, wordset):
- for w in words:
- rw = w[::-1] # reverse the string
- if rw in wordset:
- yield w,rw
- def main():
- words, wordSet = getWords('List.txt')
- for w,rw in wordsInReverse(words, wordSet):
- if rw >= w: # don't print duplicates
- print('{0} is the opposite of {1}'.format(w, rw))
- if __name__=="__main__":
- main()
- from itertools import chain
- def main():
- words1, wordSet1 = getWords('List1.txt')
- words2, wordSet2 = getWords('List2.txt')
- for w,rw in chain(wordsInReverse(words1, wordSet2), wordsInReverse(words2, wordSet1)):
- print('{0} is the opposite of {1}'.format(w, rw))
- fin1 = open("list.txt")
- for word1 in fin1:
- fin2 = open("list.txt")
- for word2 in fin2:
- ...etc...
- with open('palindromic.txt') as f:
- ch = f.read()
- li = [ w for w in ch.split() if len(w)>1 ]
- dic ={}
- pals = set([])
- for line in li:
- word = line.strip().lower()
- if len(word)>1:
- if word not in dic:
- dic[word] = 1
- if word[::-1] in dic and word[::-1]!=word:
- pals.add(word)
- else:
- dic[word] += 1
- for w in pals:
- print w,dic[w],' ',w[::-1],dic[w[::-1]]