Hi! I'm working on code for a personal project thingie, and I'm stumped by part of it.
I am trying to search a line (provided in a separate .txt file) to see if I can find any consecutive occurrences of elements of a list in said line.

For example: in my list are words like "is" and "young." I would like to my module to search the text file and return words such as "youngish" which contains both of these words in immediate conjunction. I've only been able to produce words containing one or two elements of the list, but never in conjunction.

Advice from wiser python users than I would be much appreciated!

6 Years
Discussion Span
Last Post by vegaseat

You can concatenate the two words into a search word and use module re to help you. Something along this line ...

import re

text = "The pleasant peasantry likes to eat pheasant!"

find = 'as' + 'ant'

for word in text.split():
    found = re.search(find, word)
    if found:
       print("found '%s' in %s" % (found.group(0), word)) 

found 'asant' in pleasant
found 'asant' in peasantry
found 'asant' in pheasant!

Also Python has a build-in function find(). I an sure you can apply this hint to your problem.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.