How to union nested Sets / A single set from nested sets?

Rustom Mody rustompmody at gmail.com
Wed Jan 6 11:19:21 EST 2016


On Wednesday, January 6, 2016 at 6:48:28 PM UTC+5:30, mviljamaa wrote:
> I'm forming sets by set.adding to sets and this leads to sets such as:
> 
> Set([ImmutableSet(['a', ImmutableSet(['a'])]), ImmutableSet(['b', 
> 'c'])])
> 
> Is there way union these to a single set, i.e. get
> 
> Set(['a', 'b', 'c'])
> 
> ?

Dont know what version of python spells it that way.. seems old

In 2.7 you can do this:

>>> a=set([frozenset(['a']), frozenset(['b','c'])]) 
>>> {y for x in a for y in x}
set(['a', 'c', 'b'])
It may be easier to understand written the way set-expressions in math are normally written:
{y | x ∈ a, y ∈ x}
And then treat the python as an ASCII-fication of the math


Likewise
>>> frozenset(y for x in a for y in x)
frozenset(['a', 'c', 'b'])
>>>



More information about the Python-list mailing list