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


Groups > comp.lang.python > #67064

Re: intersection, union, difference, symmetric difference for dictionaries

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(at': 0.04; 'resulting': 0.04; 'duplicate': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'way:': 0.09; '2.7': 0.14; "'c',": 0.16; '-tkc': 0.16; 'evaluating': 0.16; 'least)': 0.16; 'lhs': 0.16; 'readable': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'rhs': 0.16; 'subject:dictionaries': 0.16; "{'a':": 0.16; 'wrote:': 0.18; 'result.': 0.19; '>>>': 0.22; 'otherwise,': 0.22; 'preferred': 0.22; 'header:User-Agent:1': 0.23; 'propose': 0.24; 'replace': 0.24; 'skip': 0.24; 'earlier': 0.24; 'least': 0.26; 'header:X -Complaints-To:1': 0.27; 'correct': 0.29; 'tim': 0.29; 'sets': 0.30; '(on': 0.31; '3.x': 0.31; 'chase': 0.31; 'keys': 0.31; 'operations': 0.35; 'but': 0.35; "didn't": 0.36; 'possible': 0.36; 'two': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'space': 0.40; 'save': 0.62; 'choose': 0.64; 'week,': 0.64
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: intersection, union, difference, symmetric difference for dictionaries
Date Tue, 25 Feb 2014 22:54:43 +0100
Organization None
References <G57Pu.24239$Th2.4990@tornado.fastwebnet.it> <CANc-5UwjkYwKJ5cqvu6c1XNG=vBuGimxuueBBFXUyuEbYoVNmw@mail.gmail.com> <20140225150351.063fd961@bigbox.christie.dr>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bdad84.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.7371.1393365296.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1393365296 news.xs4all.nl 2884 [2001:888:2000:d::a6]:48121
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:67064

Show key headers only | View raw


Tim Chase wrote:

> On 2014-02-25 14:40, Skip Montanaro wrote:
>> What's the correct result of evaluating this expression?
>> 
>> {'A': 1} | {'A': 2}
>> 
>> I can see (at least) two possible "correct" answers.
> 
> I would propose at least four:
> 
>   {'A': 1}   # choose the LHS
>   {'A': 2}   # choose the RHS
>   {'A': (1,2)} # a resulting pair of both
>   set(['A']) # you did set-ops, so you get a set
> 
> If dicts were to support set ops, 

They do in 2.7 and 3.x.

> the last one would be my preferred
> result.
> 
> I just had to perform set operations on a pair of dicts earlier this
> week, shrugged, and did things the manual/explicit way:
> 
>   a_dict = dict(...)
>   b_dict = dict(...)
>   a_set = set(a_dict)
>   b_set = set(b_dict)
>   added_keys = b_set - a_set
>   removed_keys = a_set - b_set
>   same_keys = a_set & b_set
>   diff_keys = a_set ^ b_set
>   all_keys = a_set | b_set

>>> a = dict.fromkeys("ab")
>>> b = dict.fromkeys("bc")
>>> a.viewkeys() - b.viewkeys()
set(['a'])
>>> a.viewkeys() & b.viewkeys()
set(['b'])
>>> a.viewkeys() ^ b.viewkeys()
set(['a', 'c'])
>>> a.viewkeys() | b.viewkeys()
set(['a', 'c', 'b'])

For 3.x replace viewkeys() with keys().

> It would save some space if I didn't have to duplicate all the keys
> into sets (on the order of 10-100k small strings), instead being able
> to directly perform the set-ops on the dicts.  But otherwise, it was
> pretty readable & straight-forward.
> 
> -tkc

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 20:32 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries Nick Timkovich <prometheus235@gmail.com> - 2014-02-25 14:37 -0600
  Re: intersection, union, difference, symmetric difference for dictionaries Skip Montanaro <skip@pobox.com> - 2014-02-25 14:40 -0600
  Re: intersection, union, difference, symmetric difference for dictionaries Peter Otten <__peter__@web.de> - 2014-02-25 21:53 +0100
  Re: intersection, union, difference, symmetric difference for dictionaries Peter Otten <__peter__@web.de> - 2014-02-25 21:58 +0100
  Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 15:03 -0600
    Re: intersection, union, difference, symmetric difference for dictionaries Duncan Booth <duncan.booth@invalid.invalid> - 2014-02-25 22:21 +0000
      Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <tim@thechases.com> - 2014-02-25 16:35 -0600
      Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 16:36 -0600
      Re: intersection, union, difference, symmetric difference for dictionaries Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-02-25 22:58 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-25 23:10 +0000
      Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 20:21 -0600
  Re: intersection, union, difference, symmetric difference for dictionaries Ben Finney <ben+python@benfinney.id.au> - 2014-02-26 08:27 +1100
  Re:intersection, union, difference, symmetric difference for dictionaries Dave Angel <davea@davea.name> - 2014-02-25 16:35 -0500
  Re: intersection, union, difference, symmetric difference for dictionaries Peter Otten <__peter__@web.de> - 2014-02-25 22:54 +0100
  Re: intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 22:02 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 22:03 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 16:10 -0600
  Re: intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 22:11 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-25 22:35 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries MRAB <python@mrabarnett.plus.com> - 2014-02-25 23:07 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-26 00:37 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries Ben Finney <ben+python@benfinney.id.au> - 2014-02-26 10:14 +1100
  Re: intersection, union, difference, symmetric difference for dictionaries MRAB <python@mrabarnett.plus.com> - 2014-02-25 23:25 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries Grant Edwards <invalid@invalid.invalid> - 2014-02-25 20:44 +0000
  Re: intersection, union, difference, symmetric difference for dictionaries John Gordon <gordon@panix.com> - 2014-02-25 20:44 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Chris Angelico <rosuav@gmail.com> - 2014-03-02 09:29 +1100

csiph-web