Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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,例如,如果我们有 ListofLists1 = [[0], [0], [1, 3], [1, 4], []] 及 我们定义 Diff = ListofLists2 - ListofLists1 我希望有以下形式的输出 Diff = [[2], [1,2,3], [4], [3], [0,1,3]] 对于我正在做的特定类型的工作,我总是希望在ListofLists2的相应(相同索引)列表中包含的ListofLists1中的任何列表中找到所有元素。但是,ListofLists2中的列表可能包

例如,如果我们有

ListofLists1 = [[0], [0], [1, 3], [1, 4], []] 

我们定义

Diff = ListofLists2 - ListofLists1
我希望有以下形式的输出

Diff = [[2], [1,2,3], [4], [3], [0,1,3]]

对于我正在做的特定类型的工作,我总是希望在ListofLists2的相应(相同索引)列表中包含的ListofLists1中的任何列表中找到所有元素。但是,ListofLists2中的列表可能包含ListofLists1中相应列表中不存在的元素。

将两个列表压缩在一起,作为列表理解的一部分对它们进行迭代,并在每种情况下找到集合对称差异,将每个结果集设置为列表类型:

ListofLists1 = [[0], [0], [1, 3], [1, 4], []] 
ListofLists2 = [[0, 2], [0, 1, 2, 3], [1, 3, 4], [1, 3, 4], [0, 1, 3]]

[list(set(l1).symmetric_difference(l2)) for l1,l2 in zip(ListofLists1,ListofLists2)]
给出:

[[2], [1, 2, 3], [4], [3], [0, 1, 3]]

刚才我脑子里又想出了一个解决办法。那怎么办

Diff = map( lambda x,y: list(set(x) - set(y)), ListofLists2, ListofLists1 )
这也会回来

Diff = [[2], [1,2,3], [4], [3], [0,1,3]]
为了


有没有想过这种方法将如何与StackOverflow的答案进行比较(就长度通常为数千万的列表的实现速度而言)?

欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南。在这里申请。StackOverflow不是编码或教程服务。在寻求帮助之前,请自己好好尝试一下。根据堆栈溢出准则发布您的尝试,我们将尝试一下。
Diff = [[2], [1,2,3], [4], [3], [0,1,3]]
ListofLists1 = [[0], [0], [1, 3], [1, 4], []] 
ListofLists2 = [[0, 2], [0, 1, 2, 3], [1, 3, 4], [1, 3, 4], [0, 1, 3]]