Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101290 > unrolled thread

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

Started bymviljamaa <mviljamaa@kapsi.fi>
First post2016-01-04 04:40 +0200
Last post2016-01-07 00:33 -0500
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  How to union nested Sets / A single set from nested sets? mviljamaa <mviljamaa@kapsi.fi> - 2016-01-04 04:40 +0200
    Re: How to union nested Sets / A single set from nested sets? Rustom Mody <rustompmody@gmail.com> - 2016-01-06 08:19 -0800
    Re: How to union nested Sets / A single set from nested sets? Grobu <snailcoder@retrosite.invalid> - 2016-01-07 05:59 +0100
      Re: How to union nested Sets / A single set from nested sets? Joel Goldstick <joel.goldstick@gmail.com> - 2016-01-07 00:33 -0500

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

Frommviljamaa <mviljamaa@kapsi.fi>
Date2016-01-04 04:40 +0200
SubjectHow to union nested Sets / A single set from nested sets?
Message-ID<mailman.19.1452086285.2305.python-list@python.org>
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'])

?

[toc] | [next] | [standalone]


#101304

FromRustom Mody <rustompmody@gmail.com>
Date2016-01-06 08:19 -0800
Message-ID<41046824-5cef-4757-bc8d-8ade462c1478@googlegroups.com>
In reply to#101290
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'])
>>>

[toc] | [prev] | [next] | [standalone]


#101319

FromGrobu <snailcoder@retrosite.invalid>
Date2016-01-07 05:59 +0100
Message-ID<n6kr5v$amj$1@dont-email.me>
In reply to#101290
On 04/01/16 03:40, 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'])
>
> ?

There's a built-in "union" method for sets :

 >>> a = set( ['a', 'b'] )
 >>> b = set( ['c', 'd'] )
 >>> a.union(b)
set(['a', 'c', 'b', 'd'])

HTH

[toc] | [prev] | [next] | [standalone]


#101320

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2016-01-07 00:33 -0500
Message-ID<mailman.38.1452144831.2305.python-list@python.org>
In reply to#101319
On Wed, Jan 6, 2016 at 11:59 PM, Grobu <snailcoder@retrosite.invalid> wrote:

> On 04/01/16 03:40, 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'])
>>
>> ?
>>
>
> There's a built-in "union" method for sets :
>
> >>> a = set( ['a', 'b'] )
> >>> b = set( ['c', 'd'] )
> >>> a.union(b)
> set(['a', 'c', 'b', 'd'])
>
> HTH
> --
> https://mail.python.org/mailman/listinfo/python-list
>

plus 1

-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web