2015-07-06 7 views
5

Questo è l'ingresso:Come posso trovare l'unione in un elenco di set in Python?

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

e l'uscita dovrebbe essere:

{1, 2, 3, 4, 5} 

Ho provato ad usare set().union(x), ma questo è l'errore che sto ricevendo:

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

risposta

11

Il la firma di set.union è union(other, ...). Spacchettare i set dalla lista:

In [6]: set.union(*x) 
Out[6]: {1, 2, 3, 4, 5} 
Problemi correlati