Finding intersection/difference between python lists

后端 未结 7 1574
别那么骄傲
别那么骄傲 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:13

    A list comprehension will work.

    a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
    b = ['the', 'when', 'send', 'we', 'us']
    filtered = [i for i in a if not i[0] in b]
    
    >>>print(filtered)
    [('why', 4), ('throw', 9), ('you', 1)]
    
    0 讨论(0)
  • 2021-01-12 00:15

    As this is tagged with numpy, here is a numpy solution using numpy.in1d benchmarked against the list comprehension:

    In [1]: a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
    
    In [2]: b = ['the', 'when', 'send', 'we', 'us']
    
    In [3]: a_ar = np.array(a, dtype=[('string','|S5'), ('number',float)])
    
    In [4]: b_ar = np.array(b)
    
    In [5]: %timeit filtered = [i for i in a if not i[0] in b]
    1000000 loops, best of 3: 778 ns per loop
    
    In [6]: %timeit filtered = a_ar[-np.in1d(a_ar['string'], b_ar)]
    10000 loops, best of 3: 31.4 us per loop
    

    So for 5 records the list comprehension is faster.

    However for large data sets the numpy solution is twice as fast as the list comprehension:

    In [7]: a = a * 1000
    
    In [8]: a_ar = np.array(a, dtype=[('string','|S5'), ('number',float)])
    
    In [9]: %timeit filtered = [i for i in a if not i[0] in b]
    1000 loops, best of 3: 647 us per loop
    
    In [10]: %timeit filtered = a_ar[-np.in1d(a_ar['string'], b_ar)]
    1000 loops, best of 3: 302 us per loop
    
    0 讨论(0)
  • 2021-01-12 00:18

    Use filter:

    c = filter(lambda (x, y): False if x in b else True, a)
    
    0 讨论(0)
  • 2021-01-12 00:19

    Easy way

    a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
    b = ['the', 'when', 'send', 'we', 'us']
    c=[] # a list to store the required tuples 
    #compare the first element of each tuple in with an element in b
    for i in a:
        if i[0] not in b:
            c.append(i)
    print(c)
    
    0 讨论(0)
  • 2021-01-12 00:26

    Try this :

    a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
    
    b = ['the', 'when', 'send', 'we', 'us']
    
    c=[]
    
    for x in a:
        if x[0] not in b:
            c.append(x)
    print c
    

    Demo: http://ideone.com/zW7mzY

    0 讨论(0)
  • 2021-01-12 00:27

    A list comprehension should work:

    c = [item for item in a if item[0] not in b]
    

    Or with a dictionary comprehension:

    d = dict(a)
    c = {key: value for key in d.iteritems() if key not in b}
    
    0 讨论(0)
提交回复
热议问题