2015-09-22 11 views

Odpowiedz

7

skutecznym sposobem jest z set.union (* obj.values ​​())

Na przykład:

obj = { 
    'US': set([1,2,3]), 
    'FR': set([3,4,5]), 
    'BE': set([5,6,7]) 
} 

set.union(*obj.values()) 
Out[35]: {1, 2, 3, 4, 5, 6, 7} 
0

lekko neater sposób byłoby użyć operatora |:

items = obj['US'] | obj['FR'] | obj['BE'] 
1

można użytkownik chain tutaj:

from itertools import chain 
set(chain(*obj.values())) 
0

Zestaw ten obsługuje operatora | do wykonywania związków. Obsługuje również metodę .union. Więc co chcesz, to prawdopodobnie

items = set.union(*obj.values()) 

lub

items = obj['US'] | obj['FR'] | obj['BE'] 

jeśli chcesz być jawne.

Powiązane problemy