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


Groups > comp.lang.python > #67073

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
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.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'duplicate': 0.07; 'memory.': 0.07; 'string': 0.09; 'booth': 0.09; 'iterate': 0.09; 'strings.': 0.09; 'cc:addr:python-list': 0.11; 'cc:name:python list': 0.16; 'dict': 0.16; 'optimised': 0.16; 'readable': 0.16; 'redundant': 0.16; 'right-hand': 0.16; 'storing': 0.16; 'string).': 0.16; 'subject:dictionaries': 0.16; 'to:addr:python.list': 0.16; 'to:addr:tim.thechases.com': 0.16; 'to:name:tim chase': 0.16; 'wrote:': 0.18; 'memory': 0.22; 'example': 0.22; 'otherwise,': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'bytes': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'tim': 0.29; "doesn't": 0.30; 'sets': 0.30; 'message-id:@mail.gmail.com': 0.30; '(on': 0.31; 'apparently': 0.31; 'away.': 0.31; 'chase': 0.31; 'keys': 0.31; 'operations.': 0.31; 'overhead': 0.31; 'view.': 0.31; 'ago': 0.33; 'table': 0.34; 'convert': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; "didn't": 0.36; 'entry': 0.36; 'similar': 0.36; 'being': 0.38; 'skip:. 20': 0.38; 'short': 0.38; 'skip:p 20': 0.39; 'space': 0.40; 'matter': 0.61; 'save': 0.62; 'total': 0.65; 'size.': 0.65; 'yes': 0.68; 'lose': 0.68; 'saving': 0.69; 'other.': 0.75; '(according': 0.84; 'oscar': 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=2/ccyPZxak/St4dNWTNdYrNRlUXsNv5wnbhzYqHQR9g=; b=Ljqkw1JKsJUMQrRonpuUzrUR2kwdxNiXI8xLCAjCrbWBaq+ZyDOFFFxrq9nEHQ68mn 35p+JytZtSf0CsoYeD6QqZmQ4PqjfrrCRFGdooGa8pIjT6xyq8eW9RFAFSfAjYltqU08 /1kAx5MIX/CUExU49ydCfAsfpHc05MXHnUC8rs075+ZR8bO9xFdz+t4McS1BbDDcXuE8 UKJVNE1y1HPHCjqAsW3eFWHihoMhVWXkrHOglh7drH0dcD6m9n0ox50GnbfJy906sdt3 iQXhtyQEbOA5Q/+VNj8uMTIJVh6IIRPi3w/54a0pIFtcQ3mJzZ5cLTmaxECF6MnT1WG9 9kcQ==
X-Received by 10.52.104.68 with SMTP id gc4mr2624986vdb.2.1393369148742; Tue, 25 Feb 2014 14:59:08 -0800 (PST)
MIME-Version 1.0
In-Reply-To <20140225163608.4c2fbd5c@bigbox.christie.dr>
References <G57Pu.24239$Th2.4990@tornado.fastwebnet.it> <CANc-5UwjkYwKJ5cqvu6c1XNG=vBuGimxuueBBFXUyuEbYoVNmw@mail.gmail.com> <mailman.7367.1393362209.18130.python-list@python.org> <XnsA2DFE35C41F12duncanbooth@127.0.0.1> <20140225163608.4c2fbd5c@bigbox.christie.dr>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Tue, 25 Feb 2014 22:58:48 +0000
Subject Re: intersection, union, difference, symmetric difference for dictionaries
To Tim Chase <python.list@tim.thechases.com>
Content-Type text/plain; charset=ISO-8859-1
Cc Python List <python-list@python.org>
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.7376.1393369156.18130.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1393369156 news.xs4all.nl 2830 [2001:888:2000:d::a6]:55615
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:67073

Show key headers only | View raw


On 25 February 2014 22:36, Tim Chase <python.list@tim.thechases.com> wrote:
> On 2014-02-25 22:21, Duncan Booth wrote:
>> > 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.
>> >
>> It doesn't matter whether they were small strings or full-length
>> novels, creating a set from a dict doesn't duplicate any strings.
>
> pre-my-new-learning-about .viewkeys() it sounds like set(my_dict)
> would have the overhead of storing an additional reference to a
> string per set-entry (rather than duplicating every string).
> With .viewkeys()/.keys(), it sounds like that overhead would go away.

You lose the redundant hash-table by using the keys view. The set hash
table takes 20-40 bytes per entry on this computer (according to
sys.getsizeof). On the same system a short string takes a similar
amount of memory. The .keys() view object apparently takes 24 bytes in
total so yes it's a reasonable memory saving if the dicts or of any
significant size.

However the .keys() objects are not necessarily as well optimised so
it may be faster to convert to sets for some operations. One example I
found some time ago is that for sets A & B will iterate over the
smaller set but for dicts A.keys() & B.keys() would always go over the
left-hand set (or right-hand - I don't remember). This makes a
considerable difference if one set is significantly larger than the
other.


Oscar

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