簡體   English   中英

如何判斷是否在較大的字符串中找到了字符串列表?

[英]How do I tell if a list of strings are found within a larger string?

我有一個單詞列表(即):

['bad', 'good', 'smart']

我需要找到文件中哪些行包含該行中的所有這些單詞。 我試過了:

for line in file_text: 
    if [w for w in list] in line:
        print(line) 

但是我收到以下錯誤:

TypeError: 'in <string>' requires string as left operand, not list

什么是簡單的pythonic方法?

我認為您想使用all (或any ):

>>> lst = ["the", "fox", "brown"]
>>> sentence = "the quick brown fox jumps over the lazy dog"
>>> all(word in sentence for word in lst)
True

一線

for line in file_text:
    [line for w in list if w in line and w == 'smart']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM