python如何判断txt文本里,并保留符合条件的句子(一句话至少需要包括指定词中的任意2个)

有一个文本,里面比方有
‘春天来了,冬天还会远吗。
夏天很炎热,是四季中最炎热的季节。
这里像冬天一样。’

关键词是‘春天,夏天,四季,冬天,季节’

留下文本是前两句。

感谢解答

str = '''春天来了,冬天还会远吗。
夏天很炎热,是四季中最炎热的季节。
这里像冬天一样。'''
sentences = str.split("。")
sentences2 = []
words = ['春天','夏天','四季','冬天','季节']
for s in sentences:
    count=0
    for w in words:
        if w in s:
            count+=1
    if count>=2:sentences2.append(s)
print(sentences2)

如果关键词超多的话,可以考虑ac自动机,pip install ahocorasick,用这个即使有10万关键词都能很快判断出来符合条件的句子

肯定还可以优化,先凑活用

with open("data.txt", "r", encoding="utf8") as f:
    # string = f.readlines()        #有换行符
    string = f.read().splitlines()  # 没有换行符

key_word = "春天,夏天,四季,冬天,季节".split(",")
#print(string)
#print(key_word)
count = 0
res = []
for i in string:
    for j in key_word:
        if j in i:
            count += 1
        if count >= 2:
            res.append(i)
            count = 0
            break

print(res)

str = '''春天来了,冬天还会远吗。夏天很炎热,是四季中最炎热的季节。这里像冬天一样。'''
sentences = str.split("。")
sentences2 = []
words = ['春天','夏天','四季','冬天','季节']
for s in sentences:
    count=0
    for w in words:
        if w in s:
            count+=1
    if count>=2:sentences2.append(s)
print(sentences2)