Skip to main content
 首页 » 编程设计

python之如何获取匹配的字符串

2025年05月04日24zhenyulu

我在 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 

其中“not active”是匹配的短语。所以我想打印它。

我该怎么做。谁能帮我 ?

谢谢,

请您参考如下方法:

你可以有多个术语匹配细节

detail = "this is not active" 
inactive = [term for term in phrase if term in detail] 
if inactive: 
    print('matched phrases' + inactive)