I wrote this code to find opposites of words (i.e., words backwards). Input is a list of words. If this list contains a word + its opposite, then it should form a pair. The output should be a list of tuples (pairs).
The output should:
- contain every pair of words only once – it should contain either (‘reed’, ‘deer’) or (‘deer’, ‘reed’), but not both.
- not contain palindromes (words which are opposite to themselves) – e.g. ‘refer’
def find_opposites(lst): if lst == () or len(lst) == 1: return () else: opposites = () # contains all words backwards (except palindromes) result = () for word in lst: if word != word(::-1): # avoid palindromes opposites.append(word(::-1)) for word in opposites: if word in lst: # if word in both opposites list and original list, # then it should form a pair with its opposite tup = (word(::-1), word) # create tuple if tup(::-1) not in result: # append tuple only if its opposite in not already in result result.append(tup) return result
I have tried to substitute the list opposites with a dictionary as I know lists are slower to process, but it still takes a very long time.
Please let me know if you have any ideas on how to make it run faster, as it takes ages when I run on my corpora which contains over 11.000 words. Thanks a lot 🙂