Find non-common elements in lists

后端 未结 6 2191
离开以前
离开以前 2020-11-30 06:34

I\'m trying to write a piece of code that can automatically factor an expression. For example, if I have two lists [1,2,3,4] and [2,3,5], the code should be able to find th

6条回答
  •  自闭症患者
    2020-11-30 06:58

    This should get the common and remaining elements

    lis1=[1,2,3,4,5,6,2,3,1]
    lis2=[4,5,8,7,10,6,9,8]
    
    common = list(dict.fromkeys([l1 for l1 in lis1 if l1 in lis2]))
    remaining = list(filter(lambda i: i not in common, lis1+lis2))
    

    common = [4, 5, 6]

    remaining = [1, 2, 3, 2, 3, 1, 8, 7, 10, 9, 8]

提交回复
热议问题