我在python中使用any()
from inActivePhrase import phrase
detailslist=[]
for detail in detailslist:
inactive = any(term in detail for term in phrase)
短语将包含下面的字符串列表
phrase = ["not active","Closed",etc..]
功能正常。但我想得到细节中存在的短语。
示例:
detail = "this is not active"
inactive = any(term in detail for term in phrase)
if inactive:
print('matched phrase' + term) //how can i do this
其中"不活跃"是匹配的短语。所以我想打印它。
我该怎么做?谁能帮我 ?
谢谢,
答案 0 :(得分:4)
你可以有几个术语匹配的细节
detail = "this is not active"
inactive = [term for term in phrase if term in detail]
if inactive:
print('matched phrases' + inactive)
答案 1 :(得分:2)
使用next
,它会在找到第一个匹配项后立即迭代并停止。如果没有找到任何内容,则返回默认值(在这种情况下为None
):
detail = "this is not active"
inactive = next((term for term in phrase if term in detail), None)
if inactive:
print('matched phrase' + inactive)