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


Groups > comp.lang.python > #69842

Re: Keeping track of things with dictionaries

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.021
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'exists.': 0.07; 'clean.': 0.09; 'will,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; "(it's": 0.16; 'buggy': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'length.': 0.16; 'subject:dictionaries': 0.16; "value',": 0.16; "{'a':": 0.16; 'wrote:': 0.18; 'cheap': 0.19; 'seems': 0.21; 'appears': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'define': 0.26; 'header:In-Reply-To:1': 0.27; 'words': 0.29; 'message- id:@mail.gmail.com': 0.30; 'getting': 0.31; "skip:' 10": 0.31; '>>>>': 0.31; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'list': 0.37; 'skip:[ 10': 0.38; 'pm,': 0.38; 'little': 0.38; 'even': 0.60; 'skip:o 30': 0.61; 'situation': 0.65; 'side': 0.67; 'frank': 0.68; 'skip:w 30': 0.69; 'default': 0.69; 'abc': 0.84; 'effects,': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Goa4COW7tmqx/a4mqFXIz7NtzNnINNw9dDSbCRfzr8o=; b=b/C7WKcADRWZu6nRjWlwxIDtvrP5MaKDQT0c9pKd5CA4GH9b6SUznOVeFoB1/HlKNT uG9XdEWBfVPdmO6jJ+hCrpbpJHS1qc22r+wByB76fTJRM+jlkhIDagdxycaGqVvBRJb1 vtHApbmS+VjsSidW/OYQEt49Uz4abU4NLc1ZFguaglo6PA3rwBnVZInIHzoHJpuK1DyH zjTM/IzniHJAi15FVcxH8CZ42QqZPX1qHXupOjxcavEl4OBr6rxdZOkesfWzyXqM7vHL /5IWWTJAVjan1slVIKMoXv1r9X8P+JQUzyBSztCawF4CrtaMMtkVWD5zXdv4l+M0Dvzl R0yg==
MIME-Version 1.0
X-Received by 10.69.31.74 with SMTP id kk10mr2601393pbd.169.1396944028601; Tue, 08 Apr 2014 01:00:28 -0700 (PDT)
In-Reply-To <li07kt$4cl$1@ger.gmane.org>
References <534105ce$0$1365$4fafbaef@reader1.news.tin.it> <21ef5159-ad95-4d43-a2d6-7ecda941d978@googlegroups.com> <CAPTjJmqFBt2XX+BDfNHz0gaGOrDkhtpBzrR29DUWN36girzcSw@mail.gmail.com> <li07kt$4cl$1@ger.gmane.org>
Date Tue, 8 Apr 2014 18:00:28 +1000
Subject Re: Keeping track of things with dictionaries
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.9000.1396944031.18130.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396944031 news.xs4all.nl 2884 [2001:888:2000:d::a6]:52556
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69842

Show key headers only | View raw


On Tue, Apr 8, 2014 at 5:14 PM, Frank Millman <frank@chagford.com> wrote:
> It appears that when you use 'setdefault', the default is always evaluated,
> even if the key exists.
>
>>>> def get_value(val):
> ...   print('getting value', val)
> ...   return val*2
> ...
>>>> my_dict = {}
>>>> my_dict.setdefault('a', get_value('xyz'))
> getting value xyz
> 'xyzxyz'
>>>> my_dict.setdefault('a', get_value('abc'))
> getting value abc
> 'xyzxyz'
>>>> my_dict
> {'a': 'xyzxyz'}
>>>>
>
> It seems odd. Is there a situation where this behaviour is useful?

If the default value is cheap to define and has no side effects, it
can be very clean.

words_by_length = {}
for word in open("/usr/share/dict/words"):
    words_by_length.setdefault(len(word), []).append(word)

This will, very conveniently, give you a list of all words of a
particular length. (It's actually a little buggy but you get the
idea.)

ChrisA

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


Thread

Keeping track of things with dictionaries Giuliano Bertoletti <gbe32241@libero.it> - 2014-04-06 09:44 +0200
  Re: Keeping track of things with dictionaries Peter Otten <__peter__@web.de> - 2014-04-06 10:23 +0200
  Re: Keeping track of things with dictionaries Josh English <Joshua.R.English@gmail.com> - 2014-04-07 21:02 -0700
    Re: Keeping track of things with dictionaries Chris Angelico <rosuav@gmail.com> - 2014-04-08 14:08 +1000
      Re: Keeping track of things with dictionaries Josh English <Joshua.R.English@gmail.com> - 2014-04-07 23:22 -0700
    Re: Keeping track of things with dictionaries "Frank Millman" <frank@chagford.com> - 2014-04-08 09:14 +0200
      Re: Keeping track of things with dictionaries Steven D'Aprano <steve@pearwood.info> - 2014-04-08 07:47 +0000
    Re: Keeping track of things with dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-08 01:53 -0600
    Re: Keeping track of things with dictionaries Chris Angelico <rosuav@gmail.com> - 2014-04-08 18:00 +1000
    Re: Keeping track of things with dictionaries Peter Otten <__peter__@web.de> - 2014-04-08 10:21 +0200
    Re: Keeping track of things with dictionaries "Frank Millman" <frank@chagford.com> - 2014-04-08 10:26 +0200
    Re: Keeping track of things with dictionaries "Frank Millman" <frank@chagford.com> - 2014-04-08 10:31 +0200
      Re: Keeping track of things with dictionaries alex23 <wuwei23@gmail.com> - 2014-04-09 12:34 +1000
    Re: Keeping track of things with dictionaries Chris Angelico <rosuav@gmail.com> - 2014-04-08 18:35 +1000
      Re: Keeping track of things with dictionaries Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-04-09 12:43 +1200
        Re: Keeping track of things with dictionaries Chris Angelico <rosuav@gmail.com> - 2014-04-09 12:33 +1000
          Re: Keeping track of things with dictionaries alex23 <wuwei23@gmail.com> - 2014-04-09 12:45 +1000
            Re: Keeping track of things with dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-08 21:19 -0600
            Re: Keeping track of things with dictionaries Gene Heskett <gheskett@wdtv.com> - 2014-04-08 23:31 -0400
            Re: Keeping track of things with dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-08 21:37 -0600
    Re: Keeping track of things with dictionaries "Frank Millman" <frank@chagford.com> - 2014-04-08 11:28 +0200
    Re: Keeping track of things with dictionaries Chris Angelico <rosuav@gmail.com> - 2014-04-08 19:34 +1000
    Re: Keeping track of things with dictionaries "Frank Millman" <frank@chagford.com> - 2014-04-08 11:41 +0200
  Re: Keeping track of things with dictionaries Gene Heskett <gheskett@wdtv.com> - 2014-04-09 05:51 -0400

csiph-web