Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101340
| Path | csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: How to union nested Sets / A single set from nested sets? |
| Date | Thu, 7 Jan 2016 15:19:28 +0000 |
| Lines | 65 |
| Message-ID | <mailman.48.1452179990.2305.python-list@python.org> (permalink) |
| References | <bd94920f139af986c1a304f5ce844f9d@kapsi.fi> <mailman.31.1452091523.2305.python-list@python.org> <568e23e5$0$1590$c3e8da3$5496439d@news.astraweb.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de jw0IuG3AGdPK7vNthGPQzAe0k+HXuHwLxX1DURxOvNiQ== |
| Return-Path | <oscar.j.benjamin@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'cc:addr :python-list': 0.09; 'subject:How': 0.09; 'backwards': 0.09; 'benjamin': 0.09; 'subject:set': 0.09; 'python': 0.10; 'assume': 0.11; 'jan': 0.11; '2.7': 0.13; 'stack': 0.13; 'def': 0.13; 'translate': 0.15; 'thu,': 0.15; "'b',": 0.16; "'c',": 0.16; '2016': 0.16; '3):': 0.16; 'branch:': 0.16; 'builtins.': 0.16; 'cc:name:python list': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'set()': 0.16; 'set,': 0.16; 'stack:': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.16; 'nested': 0.18; '>>>': 0.20; 'versions': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'forming': 0.22; 'am,': 0.23; 'elements': 0.23; 'sets': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; '2.3': 0.27; "i'm": 0.30; 'branch': 0.30; 'getting': 0.33; 'problem': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'received:google.com': 0.35; 'i.e.': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'january': 0.38; 'received:209': 0.38; 'subject:from': 0.39; 'where': 0.40; 'still': 0.40; 'flat': 0.63; 'subject: / ': 0.63; "they're": 0.66; 'union': 0.67; 'levels': 0.70; 'now:': 0.72; 'as:': 0.79; "op's": 0.84; 'oscar': 0.84; 'subject:Sets': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=gNLhrhQ+QME4qzx63sux6vkYSXdfVzziRr5lYeQfHj0=; b=QGaxHxyoHcCFfOUkCMaf1drluujMpwfLl9p+GyRUM9Fnq9fCFuVPQ3F7INcT4nWK7R hKI9rwBbvQAwHo5cKairUSlOE8VjRrd3pTqosjjGkexXMrPu/npaFn6uim1TFiFuYx0j HqFuM1mm9zExyCTZi2KmCy+xSA1GQ/nGRj0iMKOphw0iDYfDbfRr5PjuZgjv5jRzUTWU NfESgyaHC6iCQ84G9xQqJf9RNW1iKpTKg6wjaa54Anmnf3hNnfh40ttDSh4CwTpAWi9b +l+hyj6gXxdQUgQ2RbLi/ctxgAHFxY5T7Za3n1J8WvHzenpp0hlwBjV+nX60/xIKTOmA RIgg== |
| X-Received | by 10.112.235.71 with SMTP id uk7mr33297980lbc.39.1452179988296; Thu, 07 Jan 2016 07:19:48 -0800 (PST) |
| In-Reply-To | <568e23e5$0$1590$c3e8da3$5496439d@news.astraweb.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:101340 |
Show key headers only | View raw
On 7 January 2016 at 08:37, Steven D'Aprano <steve@pearwood.info> wrote:
> On Thu, 7 Jan 2016 01:45 am, Oscar Benjamin wrote:
>
>> On 4 January 2016 at 02:40, mviljamaa <mviljamaa@kapsi.fi> 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'])
>>
>> Where are you getting Set and ImmutableSet from? Is that sympy or
>> something?
>
> Set and ImmutableSet were the original versions from Python 2.3 before the
> builtins. They're still available up to Python 2.7 (gone in 3):
>
>
> py> import sets
> py> sets.Set
> <class 'sets.Set'>
>
> but that's just for backwards compatibility, you should use the built-in
> versions if you can.
Okay, thanks Steve.
Then I translate the OP's problem as how to go from
S = set([frozenset(['a', frozenset(['a'])]), frozenset(['b', 'c'])])
to
set(['a', 'b', 'c'])
This is not just a union of the elements of the set since:
In [7]: {x for fs in S for x in fs}
Out[7]: set(['a', 'c', 'b', frozenset(['a'])])
The reason for this is that there are multiple levels of nesting. I
assume that the OP wants to recursively get all the elements from all
the nested sets and create a set out of those. Here's a breadth-first
search that can do that:
def flat_nested(tree):
flat = set()
stack = [iter(tree)]
while stack:
branch = stack.pop(0)
for x in branch:
if isinstance(x, frozenset):
stack.append(iter(x))
else:
flat.add(x)
return flat
And now:
In [15]: flat_nested(S)
Out[15]: set(['a', 'c', 'b'])
--
Oscar
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: How to union nested Sets / A single set from nested sets? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-01-06 14:45 +0000
Re: How to union nested Sets / A single set from nested sets? Steven D'Aprano <steve@pearwood.info> - 2016-01-07 19:37 +1100
Re: How to union nested Sets / A single set from nested sets? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-01-07 15:19 +0000
csiph-web