Union of multiple sets in python

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

    from functools import reduce
    
    out = list(reduce(set.union, iterable))
    

    as long as at least the first the element of iterable is a set. Otherwise,

    out = list(reduce(set.union, iterable[1:], set(iterable[0])))
    

提交回复
热议问题