简体   繁体   中英

How to check if a 2D list contains a list that partly contains another list

I'm trying to find out if my Tabu list (2D) contains a list that partly contains another list.

Like:

Tabu = [[1, 2, 3], [3, 2, 1, 0]]
Test = [3, 2, 1]
Test2 = [1, 3, 2]

Here Tabu contains a list: [3, 2, 1, 0] that contains [3, 2, 1] , so Tabu contains Test, but doesn't contain Test2 as there are no lists in Tabu that contain [1, 3, 2] in this order.

Note: All values of Test must be in a sublist of Tabu to pass. Changing the lists to sets is not an option. There are no repeating values in Test and only two seperate lists can contain the same value in Tabu.

Edit: More info and clarification

you need to iterate through the Tabu and check if all element of the Test list are in the sublist of Tabu

>>> Tabu = [[1, 2, 3], [4, 5, 6, 0]]
>>> Test = [4, 5, 6]
>>> 
>>> result = any(all(i in sublist for i in Test) for sublist in Tabu)
>>> result
True
>>> 

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