[英]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.我试图查明我的
Tabu
列表 (2D) 是否包含一个列表,该列表部分包含另一个列表。
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.这里 Tabu 包含一个列表:
[3, 2, 1, 0]
包含[3, 2, 1]
,因此 Tabu 包含 Test,但不包含 Test2,因为 Tabu 中没有列表包含[1, 3, 2]
按此顺序。
Note: All values of Test must be in a sublist of Tabu to pass.注意:Test 的所有值必须在 Tabu 的子列表中才能通过。 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.
Test 中没有重复值,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 并检查 Test 列表的所有元素是否都在 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
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.