list1 = ['moonlight black','mint cream','electric black','deep blue',
'black','blue','flowing silver','crystal blue','ink black']
list2 = ["blue","black"]
for i in list1:
for j in list2:
if j in i:
print(j)
else:
print("not found")
输出:(我不想要这个)
not found
black
not found
not found
not found
black
blue
not found
not found
black
blue
not found
not found
not found
blue
not found
not found
black
如果'blue'
或'black'
存在于list_1
的项(或项的子字符串)中,我想打印'blue'
或'black'
,如果list_1
的字符串值中既不存在'blue'
也不存在'black'
,我想打印not found
。但是我的代码不工作。我希望输出如下所示:
black
not found
black
blue
black
blue
not found
blue
black
3条答案
按热度按时间4nkexdtk1#
因为我们在这里遇到了一些问题,所以使用regex是有意义的,如下所示:
scyqe7ek2#
也可以使用列表解析
ckx4rj1h3#
更新您的代码: