简体   繁体   中英

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:

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 ,

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

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

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.

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