从其他python列表值中的python列表中查找字符串

时间:2019-03-28 08:46:26

标签: python python-3.x

我在python中有2个列表,想要检查list1的字符串值中是否存在list1的字符串值。如果所有都存在,则打印一条成功消息。

列表为:

list1 = ['star', 'moon', 'sun', 'kat']

list2 = ['This is a star', 'moonlight', '-sun', 'kat-trade', 'amaze']

在上述情况下,应该打印成功的消息。

2 个答案:

答案 0 :(得分:3)

allany一起使用:

print(all(any(i in x for x in list2) for i in list1))

输出:

True

答案 1 :(得分:1)

另一种方法是使用join

all([s in ' '.join(list2) for s in list1 ])
# True
相关问题