Finding a union of lists of lists in Python

后端 未结 2 1618
悲哀的现实
悲哀的现实 2021-01-27 18:41

Suppose we have

 temp1 = [1, 2, 3]
 temp2 = [1, 2, 3]
 temp3 = [3, 4, 5]

How do I get the union of the three temporary variables?

Expec

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-27 19:29

    I created a matrix to change the code easily for a generalized input:

    temp1=[1,2,3]
    temp2=[3,2,6]
    temp3=[1,2,3]
    matrix = [temp1, temp2, temp3]
    
    result = []
    for l in matrix:
        if l not in result:
            result.append(l)
    
    print result
    

提交回复
热议问题