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


Groups > comp.lang.python > #5452

Re: dict: retrieve the original key by key

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
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; 'cache': 0.04; 'memory.': 0.05; 'dictionary': 0.07; 'identical': 0.07; 'python': 0.07; '+0200,': 0.09; 'sun,': 0.09; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; 'set:': 0.16; 'subject:key': 0.16; '\xa0this': 0.16; 'cheers,': 0.20; 'header:In-Reply-To:1': 0.22; 'values': 0.23; 'keys': 0.23; 'objects': 0.24; 'received:209.85.161.46': 0.26; 'received:mail-fx0-f46.google.com': 0.26; 'message- id:@mail.gmail.com': 0.28; 'received:209.85.161': 0.29; 'equal': 0.31; 'to:addr:python-list': 0.32; 'idea': 0.32; 'received:209.85': 0.37; 'steven': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'used': 0.38; 'to:addr:python.org': 0.39; 'received:209': 0.39; 'would': 0.40; 'header:Received:5': 0.40; '2011': 0.62; 'subject:original': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=qefj9jcO7+yGULSjhOWUHVGqBXeFOg7STHZHQz5vmOU=; b=MEurU9pcbr6iZ3wXIRpq+uzaZFCdXU6mB/E2FjRB/JJoneJHxbP7OXtYwLPjbwQk0L 8OKf7UzOfXvkIJRcYXLeK48snEVD2b1HYtAiJgI7LrUMKGEuTqizolEzKOgCxGA5HBpi In8ImwhJnMZaxABltvHhKB6uWwouuIM7p6Tm8=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=Axwo+eKZdBLRcQ74g0duUGaiLD0N1SXM868ka6esvkAn0skQZy2jJBLWk8UekMeZug mO5DrHqvuP/eScx7myjQCsCdBCyH2R4xQxfMG5y2/m/b6ay9H/RH82hp0zGL5xebGv8N 1oiOPE3tdDyUXXYTHkXcx8bEXBhZKixFQn96Y=
MIME-Version 1.0
In-Reply-To <4dcfa885$0$29996$c3e8da3$5496439d@news.astraweb.com>
References <874o4wwenu.fsf@falma.de> <BANLkTikszH_r-PAuL0pJ8reMc+bGafuRew@mail.gmail.com> <mailman.1584.1305450715.9059.python-list@python.org> <4dcfa885$0$29996$c3e8da3$5496439d@news.astraweb.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 15 May 2011 18:47:45 -0600
Subject Re: dict: retrieve the original key by key
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1606.1305506896.9059.python-list@python.org> (permalink)
Lines 24
NNTP-Posting-Host 82.94.164.166
X-Trace 1305506897 news.xs4all.nl 81485 [::ffff:82.94.164.166]:55509
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5452

Show key headers only | View raw


On Sun, May 15, 2011 at 4:18 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Sun, 15 May 2011 11:11:41 +0200, Christoph Groth wrote:
>
>> I would like to avoid having _multiple_ objects which are equal (a == b)
>> but not the same (a is not b).  This would save a lot of memory.
>
> Based on the idea of interning, which is used for Python strings:
>
> cache = {}
> def my_intern(obj):
>    return cache.setdefault(obj, obj)

And if the idea of a dictionary with identical keys and values
offends, you can also use a set:

cache = set()
def my_intern(obj):
  cache.add(obj)
  return cache.intersection([obj]).pop()

Cheers,
Ian

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


Thread

Re: dict: retrieve the original key by key Christoph Groth <cwg@falma.de> - 2011-05-15 11:11 +0200
  Re: dict: retrieve the original key by key Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-15 10:18 +0000
    Re: dict: retrieve the original key by key Christoph Groth <cwg@falma.de> - 2011-05-15 12:46 +0200
    Re: dict: retrieve the original key by key Terry Reedy <tjreedy@udel.edu> - 2011-05-15 16:53 -0400
    Re: dict: retrieve the original key by key Chris Rebert <clp2@rebertia.com> - 2011-05-15 14:19 -0700
    Re: dict: retrieve the original key by key Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-15 18:47 -0600

csiph-web