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.
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:
[[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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.