I need to find if items from a list appear in a string, and then add the items to a different list. This code works:
data =[]
line = \'akhgvfalfhda.dhgfa.lidh
The following should work quite efficiently. It implements the suggestion from @SomeDude given above
lens=set([len(i) for i in _legal])
d={}
for k in lens:
d[k]=[line[i:i+k] for i in range(len(line)-k)]
s=set(sum(d.values(), []))
result=list(s.intersection(set(_legal)))
for the following data (i changed "line" a bit as it returned an empty list due to uppecase in Thing1 and Thing2)
line = 'akhgvfalfhda.dhgfa.lidhfalihflaih**thing1**aoufgyafkugafkjhafkjhflahfklh**thing2**dlfkhalfhafli...'
_legal = ['thing1', 'thing2', 'thing3', 'thing4',...]
Output is:
print(result)
['thing2', 'thing1']
Explanation: We saved all possible lengths of the words in substrings. For these lengths, we created all possible substrings in text (set s). Finally we found common items in s and substrings, which is the answer to the question.