如何比较python子列表中的元素?

如何比较python子列表中的元素?,python,list,sublist,Python,List,Sublist,如何在python中比较子列表中的元素?我想将索引1和索引2与其他列表进行比较。并且想要不匹配的子列表 list1 = [['10.100.10.37', 19331, '2020-9-28 6:38:10', 15, 16], ['10.100.10.37', 29331, '2020-9-28 6:38:10', 15 ,17]] list2 = [ ['10.100.10.37', 19331, '2020-9-28 6:38:10', 15],['10.100.10.37', 1933

如何在python中比较子列表中的元素?我想将索引1和索引2与其他列表进行比较。并且想要不匹配的子列表

list1 =  [['10.100.10.37', 19331, '2020-9-28 6:38:10', 15, 16], ['10.100.10.37', 29331, '2020-9-28 6:38:10', 15 ,17]]
list2 = [ ['10.100.10.37', 19331, '2020-9-28 6:38:10', 15],['10.100.10.37', 19331, '2020-9-28 9:38:10', 15],['10.100.10.37', 21301, '2020-9-28 6:38:10', 15]]


new_items = []
for item in list2:
    if not any(x[1] == item[1] for x in list1):
       if not any(x[2] != item[2] for x in list1):       
           new_items.append(item)
   
print(new_items)
我得到的输出为(实际输出):

预期产出:

[['10.100.10.37', 19331, '2020-9-28 9:38:10', 15], 
['10.100.10.37', 21301, '2020-9-28 6:38:10', 15]]

代码中的主要问题:嵌套的
任何
函数调用都不会执行您想要的操作(代码不会将
list1
中每个列表的第一个和第二个索引与
list2
中子列表的各自索引进行比较)

列表理解和
任何
调用都可以做到:

new_items=[列表2中的项目(如果没有)(列表1中的项目[1]==x[1]和项目[2]==x[2])]
使用切片的版本(如果需要增加连续检查的数量):

new_items=[列表2中的项目(如果没有)(列表1中的项目[1:3]==x[1:3])
使用
过滤器的替代版本(问题更简单):

tmp=[x[1:3]表示列表1中的x]
新项目=列表(过滤器(tmp中的lambda x:not x[1:3],列表2))
[['10.100.10.37', 19331, '2020-9-28 9:38:10', 15], 
['10.100.10.37', 21301, '2020-9-28 6:38:10', 15]]