[英]How to check for a sublist in a list of lists?
Let's say I have this code: 假设我有以下代码:
lst1 = [['1','g','8','c','p'],['lion','tiger','bear','oh','my'],['true','false','maybe','g','8']]
lst2 = ['g','8']
How would I check if lst2 is in lst1? 我如何检查lst2是否在lst1中?
If I understood the question correctly you can just use set
and any
: 如果我正确理解了这个问题,则可以使用
set
和any
:
any(set(lst2) <= set(x) for x in lst1)
where set(lst2) <= set(x)
checks if lst2
is sublist of x
. 其中
set(lst2) <= set(x)
检查lst2
是否是x
子列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.