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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.