Find non-common elements in lists

后端 未结 6 2194
离开以前
离开以前 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 07:00

    You can use Intersection concept to deal with this kind of problems.

    b1 = [1,2,3,4,5,9,11,15]
    b2 = [4,5,6,7,8]
    set(b1).intersection(b2)
    Out[22]: {4, 5}
    

    Best thing about using this code is it works pretty fast for large data also. I have b1 with 607139 and b2 with 296029 elements when i use this logic I get my results in 2.9 seconds.

提交回复
热议问题