Finding intersection/difference between python lists

后端 未结 7 1578
别那么骄傲
别那么骄傲 2021-01-12 00:05

I have two python lists:

a = [(\'when\', 3), (\'why\', 4), (\'throw\', 9), (\'send\', 15), (\'you\', 1)]

b = [\'the\', \'when\', \'send\', \'we\', \'us\']
<         


        
7条回答
  •  时光说笑
    2021-01-12 00:33

    in is nice, but you should use sets at least for b. If you have numpy, you could also try np.in1d of course, but if it is faster or not, you should probably try.

    # ruthless copy, but use the set...
    b = set(b)
    filtered = [i for i in a if not i[0] in b]
    
    # with numpy (note if you create the array like this, you must already put
    # the maximum string length, here 10), otherwise, just use an object array.
    # its slower (likely not worth it), but safe.
    a = np.array(a, dtype=[('key', 's10'), ('val', int)])
    b = np.asarray(b)
    
    mask = ~np.in1d(a['key'], b)
    filtered = a[mask]
    

    Sets also have have the methods difference, etc. which probably are not to useful here, but in general probably are.

提交回复
热议问题