Union of multiple sets in python

后端 未结 7 991
逝去的感伤
逝去的感伤 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:38

    Using the unpacking operator *:

    >> list(set.union(*map(set, a)))
    [1, '44', '30', '42', '43', '40', '41', '34']
    

    (Thanks Raymond Hettinger for the comment!)

    (Note that

    set.union(*tup)
    

    will unpack to

    set.union(tup[0], tup[1], ... tup[n - 1])
    

    )

提交回复
热议问题