How to combine multiple lists?

I read this: Combining two lists and removing duplicates without deleting duplicates in the original list , but my need goes further. I have at least 30 lists, and I need a union without duplicates of all lists. Right now, my first attempt was to simply use + to add the entire member to one large list, and then use set to remove duplicates, but I'm not sure if this is the best solution:

Edit - Add Samples:

list_a = ['abc','bcd','dcb']
list_b = ['abc','xyz','ASD']
list_c = ['AZD','bxd','qwe']
big_list = list_a + list_b + list_c
print list(set(big_list)) # Prints ['abc', 'qwe', 'bcd', 'xyz', 'dcb', 'ASD', 'bxd']

My real question is: if this is the best way to go with this combination?

+4
source share
4 answers

, , set.update .

>>> lists = [[1,2,3], [3,4,5], [5,6,7]]
>>> result = set()
>>> result.update(*lists)
>>> 
>>> result
{1, 2, 3, 4, 5, 6, 7}

: :

>>> list_a = ['abc','bcd','dcb']
>>> list_b = ['abc','xyz','ASD']
>>> list_c = ['AZD','bxd','qwe']
>>> 
>>> result = set()
>>> result.update(list_a, list_b, list_c)
>>> result
{'ASD', 'xyz', 'qwe', 'bxd', 'AZD', 'bcd', 'dcb', 'abc'}
+3

set.union(set1, set2, set3, ..).

>>> l1 = [1,2,3]
>>> l2 = [2,3,4]
>>> l3 = [3,4,5]
>>> set.union(*[set(x) for x in (l1, l2, l3)])
{1, 2, 3, 4, 5}

( Py2, Py3, @Lynn!):

>>> set.union(*map(set, (l1, l2, l3)))
set([1, 2, 3, 4, 5])
+2

set.union , set.

set , set.union, set.update ( , ) , set.union .

>>> list_a = ['abc','bcd','dcb']
>>> list_b = ['abc','xyz','ASD']
>>> list_c = ['AZD','bxd','qwe']

>>> result = set().union(list_a, list_b, list_c)
>>> result
{'ASD', 'xyz', 'qwe', 'bxd', 'AZD', 'bcd', 'dcb', 'abc'}
+1

, , , :

from itertools import chain

def union_lists(*iterables):
    union = []
    lookup = set()

    flattened = chain.from_iterable(iterables)

    for item in flattened:
        if item not in lookup:
            lookup.add(item)
            union.append(item)

    return union

, , set(), . set() , , O(1), , .

itertools.chain.from_iterable, O(n).

, :

>>> list_a = ['abc','bcd','dcb']
>>> list_b = ['abc','xyz','ASD']
>>> list_c = ['AZD','bxd','qwe']
>>> print(union_lists(list_a, list_b, list_c))
['abc', 'bcd', 'dcb', 'xyz', 'ASD', 'AZD', 'bxd', 'qwe']
>>> list_d = ['bcd', 'AGF', 'def']
>>> print(union_lists(list_a, list_b, list_c, list_d))
['abc', 'bcd', 'dcb', 'xyz', 'ASD', 'AZD', 'bxd', 'qwe', 'AGF', 'def']
0

Source: https://habr.com/ru/post/1691623/


All Articles