Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查列表列表中的位置元素是否不存在于其他列表列表中_Python_List - Fatal编程技术网

Python 如何检查列表列表中的位置元素是否不存在于其他列表列表中

Python 如何检查列表列表中的位置元素是否不存在于其他列表列表中,python,list,Python,List,我试图找到一种可靠的方法来检查list2(所有长度相同)中的位置列表元素是否不存在于任何I for I in list1 这看起来与我需要的正好相反: 理想情况下,我想要列表2的('m','n','b'),因为列表1的任何第三个元素中都找不到'b',所以如何将其分离出来 我寻找的不是“any”,而是这种伪代码: print x,y,z from list2 if 'z' is not found in any 3rd position element in all the lists pres

我试图找到一种可靠的方法来检查
list2
(所有长度相同)中的位置列表元素是否不存在于任何
I for I in list1

这看起来与我需要的正好相反:

理想情况下,我想要
列表2的
('m','n','b')
,因为
列表1的任何第三个元素中都找不到
'b'
,所以如何将其分离出来

我寻找的不是“any”,而是这种伪代码:

print x,y,z from list2 if 'z' is not found in any 3rd position element in all the lists present in "list1"

从第一个列表中创建所有第三个元素的集合,然后根据集合测试第二个列表中的元组:

third_positions = {t[2] for t in list1}
[t for t in list2 if t[2] not in third_positions]
首先存储要再次测试的值要比每次再次循环所有值有效得多

演示:

如果您必须使用
any()
,那么这也是可能的,但这会导致
list1
中的每个元组在
list2
上循环,因此O(NM)而不是O(N):


你是说第三要素吧?对于基于零的索引,
object[2]
是第三个元素,而不是第二个。是的,这就是我的意思,0,1,2,所以我认为英语中的第二个(包括第0个)枚举不是从0开始的<代码>对象[2]
是第三个元素。
third_positions = {t[2] for t in list1}
[t for t in list2 if t[2] not in third_positions]
>>> list1 = [('1', '2', '3'), ('a', 'b', 'c'), ('4', '5', '6')]
>>> list2 = [('a', 'b', 'c'), ('m', 'n', 'b'), ('p', 'q', '6')]
>>> third_positions = {t[2] for t in list1}
>>> print([t for t in list2 if t[2] not in third_positions])
[('m', 'n', 'b')]
[t for t in list2 if not any (t[2] == u[2] for u in list1)]