Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101298
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Peter Otten <__peter__@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: How to union nested Sets / A single set from nested sets? |
| Date | Wed, 06 Jan 2016 14:39:32 +0100 |
| Organization | None |
| Lines | 44 |
| Message-ID | <mailman.27.1452087583.2305.python-list@python.org> (permalink) |
| References | <bd94920f139af986c1a304f5ce844f9d@kapsi.fi> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | news.uni-berlin.de f5dclK1pVapkZt+7diTbOQtc0J4lAw/q/DSazQzSqK0g== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:How': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:set': 0.09; 'python': 0.10; '2.7': 0.13; "'a',": 0.16; "'b',": 0.16; "'d',": 0.16; 'frozenset': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'set()': 0.16; 'set,': 0.16; 'with?': 0.16; 'wrote:': 0.16; 'nested': 0.18; '>>>': 0.20; '3.x': 0.22; 'forming': 0.22; 'sets': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'subset': 0.29; "i'm": 0.30; 'fixed': 0.31; 'i.e.': 0.35; 'skip:i 20': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'version': 0.38; 'data': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'avoid': 0.61; 'subject: / ': 0.63; 'union': 0.67; 'as:': 0.79; 'or:': 0.84; 'subject:Sets': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | p57bd8deb.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| 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:101298 |
Show key headers only | View raw
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'])
>
> ?
1 What version of Python are you working with?
Both Python 2.7 and 3.x have built-in set and frozenset types that you
should use.
2 Instead of flattening the nested data I recommend that you avoid nesting
in the first place by using update() instead of add()
>>> x = {"a", "b"}
>>> y = {"b", "c"}
>>> z = {"a", "c", "d"}
>>> result = set()
>>> for subset in x, y, z:
... result.update(subset) # or: result |= subset
...
>>> result
{'b', 'a', 'd', 'c'}
For a fixed number of subsets you can avoid the loop and write
>>> result = set()
>>> result.update(x, y, z)
>>> result
{'b', 'a', 'd', 'c'}
or
>>> x | y | z
{'b', 'a', 'd', 'c'}
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: How to union nested Sets / A single set from nested sets? Peter Otten <__peter__@web.de> - 2016-01-06 14:39 +0100
csiph-web