【发布时间】:2020-10-22 18:57:21
【问题描述】:
我想遍历我的“匹配”列表中的每个元素并提取该关键字出现的所有字符串 - 它可以是大写或小写:
请有人告诉我哪里出错了?
我尝试过的代码是:
match=['FUN','HAPPY','FLORAL', 'alpha','12133','water12']
data=['the fun we are', 'hello there', 'Happy today is the case','112133 is it', 'FLORAL is my fave']
lst=[]
for i in match:
for j in data:
if i.lower() in j.lower():
lst.append(j)
else:
lst.append('not found')
想要的输出:
lst=['the fun we are','Happy today is the case','112133 is it' ,'FLORAL is my fave']
谢谢
【问题讨论】:
-
您可以使用生成器申请
any()。any(m.lower() in i.lower() for m in match)
标签: python list loops for-loop