Combining two lists and removing duplicates, without removing duplicates in original list

后端 未结 11 1878
逝去的感伤
逝去的感伤 2020-11-28 04:53

I have two lists that i need to combine where the second list has any duplicates of the first list ignored. .. A bit hard to explain, so let me show an example of what the c

11条回答
  •  天涯浪人
    2020-11-28 05:24

    You can bring this down to one single line of code if you use numpy:

    a = [1,2,3,4,5,6,7]
    b = [2,4,7,8,9,10,11,12]
    
    sorted(np.unique(a+b))
    
    >>> [1,2,3,4,5,6,7,8,9,10,11,12]
    

提交回复
热议问题