簡體   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