简体   繁体   中英

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?

If I understood the question correctly you can just use set and any :

any(set(lst2) <= set(x) for x in lst1)

where set(lst2) <= set(x) checks if lst2 is sublist of x .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM