array filter in python?

后端 未结 10 1086
一向
一向 2020-12-14 05:27

For example, I have two lists

 A           = [6, 7, 8, 9, 10, 11, 12]
subset_of_A  = [6, 9, 12]; # the subset of A


the result should be [7, 8, 10, 11]; t         


        
相关标签:
10条回答
  • 2020-12-14 06:00

    tuple(set([6, 7, 8, 9, 10, 11, 12]).difference([6, 9, 12]))

    0 讨论(0)
  • 2020-12-14 06:03
    >>> A           = [6, 7, 8, 9, 10, 11, 12]
    >>> subset_of_A  = [6, 9, 12];
    >>> set(A) - set(subset_of_A)
    set([8, 10, 11, 7])
    >>> 
    
    0 讨论(0)
  • 2020-12-14 06:04

    How about

    set(A).difference(subset_of_A)
    
    0 讨论(0)
  • 2020-12-14 06:06

    Yes, the filter function:

    filter(lambda x: x not in subset_of_A, A)
    
    0 讨论(0)
提交回复
热议问题