简体   繁体   English

python,找到两个不等列表之间的差异

[英]python, find difference between two unequal lists

how to find the difference between to list which have not the same length?如何找到长度不同的列表之间的差异?

Both lists are normalized.两个列表都是规范化的。

This depends on what discrete difference you're talking about.这取决于您所谈论的离散差异。

If you'd like all elements in A not in B:如果您希望 A 中的所有元素不在 B 中:

list(set(A) - set(B))

If you'd like all elements not in both lists:如果您想要不在两个列表中的所有元素:

list(set(A).symmetric_difference(set(B)))

The difference can be seen in this example:在这个例子中可以看出区别:

In : set([1, 2]) - set([2, 3])
Out: set([1]) 

In : set([1, 2]).symmetric_difference(set([2, 3]))
Out: set([1, 3])

Functionally , Functionally

list(filter(lambda x: x[0] != x[1], zip(l1, l2)))

you can zip the two lists and check if the entires are not the same and filter out to get those differences.您可以压缩两个列表并检查它们是否相同并过滤以获取这些差异。 You can change this to fit your specific needs such as getting a list of the same length as l1 and l2 but with True and False if they match or not您可以更改它以满足您的特定需求,例如获取与 l1 和 l2 长度相同的列表,但如果它们匹配或不匹配,则使用TrueFalse

list(map(lambda x: x[0] == x[1], zip(l1,l2)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM