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


Groups > comp.lang.python > #22610

Re: Is there a better way to do this snippet?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'value,': 0.04; 'none,': 0.05; 'false,': 0.07; 'python': 0.09; 'collections': 0.09; 'from:addr:web.de': 0.09; 'boolean': 0.16; 'defaultdict': 0.16; 'received:80.91': 0.16; 'received:80.91.229': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:gmane.org': 0.16; 'received:list': 0.16; 'received:t-dialin.net': 0.16; 'syntax.': 0.16; 'tags.': 0.16; 'items.': 0.17; 'wrote:': 0.19; "who's": 0.20; 'import': 0.20; '"",': 0.21; 'keys': 0.21; 'trouble': 0.24; 'run': 0.25; 'figure': 0.26; 'header:User- Agent:1': 0.26; 'this.': 0.27; 'values': 0.28; 'wondering': 0.28; 'context,': 0.28; 'dictionary': 0.28; 'header:X-Complaints-To:1': 0.29; 'could': 0.32; 'lists': 0.33; 'false': 0.34; 'version': 0.34; 'two': 0.35; 'to:addr:python-list': 0.35; 'things': 0.35; 'list': 0.35; 'should': 0.35; 'subject:?': 0.35; 'there': 0.36; 'received:org': 0.36; 'item': 0.37; 'but': 0.37; 'subject:: ': 0.37; 'means': 0.38; 'skip:i 20': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; '...': 0.40; 'received:net': 0.60; 'evaluate': 0.62; 'first': 0.62; 'below.': 0.68; 'subject:this': 0.84; 'played': 0.84; 'subject:better': 0.84; 'subject:there': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Is there a better way to do this snippet?
Date Tue, 03 Apr 2012 17:24:46 +0200
Organization None
References <52664090-0833-4ec2-af8b-326737dd67d1@w5g2000yqi.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a70c.dip.t-dialin.net
User-Agent KNode/4.7.3
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.1282.1333466694.3037.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1333466694 news.xs4all.nl 6878 [2001:888:2000:d::a6]:56400
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22610

Show key headers only | View raw


python wrote:

> I played around with a few things and this works but was wondering if
> there was a better way to do this.
> My first thought was list comprehension but could not get a figure out
> the syntax.
> 
> tag23gr is a list of lists each with two items.
> g23tag is an empty dictionary when I run the for loop below.
> When is is complete each key is a graphic name who's values are a list
> of tags.
> 
> for item in tag23gr:
> ... 	value, key = tuple(item)
> ... 	if(g23tag.get(key)):

That should be

         if key in g23tag:

Your version means trouble for keys that evaluate to False in a boolean 
context, e. g. 0, False, None, "", (),...

> ... 		g23tag[key].append(value)
> ... 	else:
> ... 		g23tag[key] = [value]

from collections import defaultdict
g23tag = defaultdict(list)

for value, key in tag23gr:
    g23tag[key].append(value)

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


Thread

Is there a better way to do this snippet? python <w.g.sneddon@gmail.com> - 2012-04-03 07:36 -0700
  Re: Is there a better way to do this snippet? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-04-03 17:02 +0200
    Re: Is there a better way to do this snippet? nn <pruebauno@latinmail.com> - 2012-04-03 08:37 -0700
      Re: Is there a better way to do this snippet? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-04-03 18:26 +0200
        Re: Is there a better way to do this snippet? nn <pruebauno@latinmail.com> - 2012-04-03 11:03 -0700
  Re: Is there a better way to do this snippet? Chris Angelico <rosuav@gmail.com> - 2012-04-04 01:09 +1000
  Re: Is there a better way to do this snippet? Peter Otten <__peter__@web.de> - 2012-04-03 17:24 +0200

csiph-web