Union of multiple sets in python

后端 未结 7 971
逝去的感伤
逝去的感伤 2020-12-29 02:59
[[1, \'34\', \'44\'], [1, \'40\', \'30\', \'41\'], [1, \'41\', \'40\', \'42\'], [1, \'42\', \'41\', \'43\'], [1, \'43\', \'42\', \'44\'], [1, \'44\', \'34\', \'43\']         


        
7条回答
  •  自闭症患者
    2020-12-29 03:24

    >>> big = [[1, '34', '44'], [1, '40', '30', '41'], [1, '41', '40', '42'], [1, '42', '41', '43'], [1, '43', '42', '44'], [1, '44', '34', '43']]
    >>> set(reduce ( lambda l,a : l + a, big))
    set([1, '44', '30', '42', '43', '40', '41', '34'])
    

    And if you really want a list of a list as a final result

    >>>>[list(set(reduce ( lambda l,a : l + a, big)))]
    [[1, '44', '30', '42', '43', '40', '41', '34']]
    

    And if you don't like recoding a lambda function for the list addition :

    >>>>[list(set(reduce ( list.__add__, big)))]
    [[1, '44', '30', '42', '43', '40', '41', '34']]
    

    EDIT : after your recommendation about using itertools.chain instead of list.__add__ I ran a timeit for both with the original variable used by the original poster.

    It seems that timeit times list.__add__ around 2.8s and itertools.chain around 3.5 seconds.

    I checked on this page and yes, you were right with the itertools.chain contains a from_iterable method that grants a huge performance boost. see below with list.__add__, itertools.chain and itertools.chain.from_iterable.

    >>> timeit.timeit("[list(set(reduce ( list.__add__, big)))]", setup="big = [ [10,20,30,40] for ele in range(10000)]", number=30)
    16.051744650801993
    >>> timeit.timeit("[list(set(reduce ( itertools.chain, big)))]", setup="big = [ [10,20,30,40] for ele in range(10000)]", number=30)
    54.721315866467194
    >>> timeit.timeit("list(set(itertools.chain.from_iterable(big)))", setup="big = [ [10,20,30,40] for ele in range(10000)]", number=30)
    0.040056066849501804
    

    Thank you very much for your advises :)

提交回复
热议问题