简体   繁体   English

Python 中两个列表列表的区别

[英]Difference between two lists of lists in Python

I have two lists of lists:我有两个列表列表:

a = [[1,2,3],[4,5,6],[7,8,9]]
b = [[1,2,3],[9,9,9]]

I would like to get a set difference between them - expected outcome:我想在它们之间取得一定的差异 - 预期结果:

c = a - b = [[4,5,6],[7,8,9]].

I tried set() and set.difference() but it seems not to be able to compare lists.我尝试了 set() 和 set.difference() 但似乎无法比较列表。

Just use list comprehensions like so:只需像这样使用列表推导:

a = [[1,2,3],[4,5,6],[7,8,9]]
b = [[1,2,3],[9,9,9]]
c = [d for d in a if d not in b]
print(c)

Output: Output:

[[4, 5, 6], [7, 8, 9]]

You can iterate through one and check if it's in the other.您可以遍历一个并检查它是否在另一个中。

[numbers for numbers in a if numbers not in b]

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

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