繁体   English   中英

在字符串中搜索两个或多个单词-Python故障排除程序

[英]Searching for two or more words in string - Python Troubleshooting Program

我知道用于搜索字符串中与另一个字符串匹配的单词的代码。

if any(word in problem for word in keyword_virus):
    #Some code her e.g. pc_virus()

但是,是否有任何代码可以让我检查两个或多个单词是否匹配/甚至对该代码进行任何修改?

谢谢 :)

keyword_virus = 'the brown fox jumps'
print([x for x in ['brown', 'jumps', 'notinstring'] if x in keyword_virus.split()])
#['brown', 'jumps']

这将返回keyword_virus中所有匹配的单词。

如果我正确理解了您的问题,则将重写for循环以检查清单中的每个单词并附加每个匹配项。

matches = []
for word in check_list:
  if word in problem_list:
    matches.append(word)

您将得到一个匹配单词的列表,从中可以计算每个单词的出现次数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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