简体   繁体   中英

how to pass a function to re.findall() in python

I have a list of words that I want to search in a string of large text.I have defined a function that returns each individual word but I dont know how to pass the function to re.findall(). I want to get any sentense that contains any word that is in words list. Can someone assist:) here is what I got:

strings = ['some large text', 'some large text'...]
ad = []
words = ['ascertained','deep','detected','disclosed','disinterred','espied','explored','exposed','famous','happened upon','identified','invented','learned','observed','perceived','presented','revealed','searched out','shown','sighted','spotted','unveiled']
def word():
    for i in words:
        t = word[i]
    return t

for i in range(len(strings)):
    ad += re.findall(r"([^.]*?word()[^.]*\.)",strings[i])
sep = ''
adc = sep.join(ad)

Do you want something like this:

strings = ['some large text', 'some large text', 'dont disclosed it']
words = ['ascertained','deep','detected','disclosed','disinterred','espied','explored','exposed','famous','happened upon','identified','invented','learned','observed','perceived','presented','revealed','searched out','shown','sighted','spotted','unveiled']

reg = re.compile("(?=(" + "|".join(map(re.escape, words)) + "))")
ad = [i for i in strings if len(re.findall(reg,i))]
ad = ', '.join(ad)

ad:

'dont disclosed it'

OR

strings = ['my name is D.', 'I am 18 years old', 'I love deep learning', 'detected something fishy'] 
ad = []
words = ['ascertained','deep','detected','disclosed','disinterred','espied','explored','exposed','famous','happened upon','identified','invented','learned','observed','perceived','presented','revealed','searched out','shown','sighted','spotted','unveiled']

for i in range(len(strings)):
    ad += [strings[i] for word in words if len(re.findall(f"([^.]*?{word}[^.]*)", strings[i]))]

Output:

ad:

['I love deep learning', 'detected something fishy']
for i in range(len(strings)):
    ad += [re.findall(f"([^.]*?{word}[^.]*\.)", strings[i]) for word in words]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM