How to compare a list of lists/sets in python?

后端 未结 8 1777
情深已故
情深已故 2020-12-01 03:47

What is the easiest way to compare the 2 lists/sets and output the differences? Are there any built in functions that will help me compare nested lists/sets?

Inputs:

8条回答
  •  無奈伤痛
    2020-12-01 04:10

    By using set comprehensions, you can make it a one-liner. If you want:

    to get a set of tuples, then:

    Differences = {tuple(i) for i in First_list} ^ {tuple(i) for i in Secnd_list}
    

    Or to get a list of tuples, then:

    Differences = list({tuple(i) for i in First_list} ^ {tuple(i) for i in Secnd_list})
    

    Or to get a list of lists (if you really want), then:

    Differences = [list(j) for j in {tuple(i) for i in First_list} ^ {tuple(i) for i in Secnd_list}]
    

    PS: I read here: https://stackoverflow.com/a/10973817/4900095 that map() function is not a pythonic way to do things.

提交回复
热议问题