2015-07-06 11 views
5

To wejście:Jak znaleźć związek na liście zestawów w Pythonie?

x = [{1, 2, 3}, {2, 3, 4}, {3, 4, 5}] 

i wyjście powinno być:

{1, 2, 3, 4, 5} 

Próbowałem użyć set().union(x) ale to jest błąd Dostaję:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: unhashable type: 'set' 

Odpowiedz

11

sygnatura set.union to union(other, ...). Rozpakuj zestawy z listy:

In [6]: set.union(*x) 
Out[6]: {1, 2, 3, 4, 5} 
Powiązane problemy