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


Groups > comp.lang.python > #22600 > unrolled thread

Is there a better way to do this snippet?

Started bypython <w.g.sneddon@gmail.com>
First post2012-04-03 07:36 -0700
Last post2012-04-03 17:24 +0200
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#22600 — Is there a better way to do this snippet?

Frompython <w.g.sneddon@gmail.com>
Date2012-04-03 07:36 -0700
SubjectIs there a better way to do this snippet?
Message-ID<52664090-0833-4ec2-af8b-326737dd67d1@w5g2000yqi.googlegroups.com>
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)):
... 		g23tag[key].append(value)
... 	else:
... 		g23tag[key] = [value]

[toc] | [next] | [standalone]


#22604

FromAlain Ketterlin <alain@dpt-info.u-strasbg.fr>
Date2012-04-03 17:02 +0200
Message-ID<87r4w4505w.fsf@dpt-info.u-strasbg.fr>
In reply to#22600
python <w.g.sneddon@gmail.com> writes:

> 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)):
> ... 		g23tag[key].append(value)
> ... 	else:
> ... 		g23tag[key] = [value]

for item in tag23gr:
    g23tag.setdefault(item[0],[]).append(item[1])

-- Alain.

[toc] | [prev] | [next] | [standalone]


#22611

Fromnn <pruebauno@latinmail.com>
Date2012-04-03 08:37 -0700
Message-ID<f5a2c32d-4a39-4e25-b839-6011168b0249@k4g2000yqa.googlegroups.com>
In reply to#22604
On Apr 3, 11:02 am, Alain Ketterlin <al...@dpt-info.u-strasbg.fr>
wrote:
> python <w.g.sned...@gmail.com> writes:
> > 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)):
> > ...                g23tag[key].append(value)
> > ...        else:
> > ...                g23tag[key] = [value]
>
> for item in tag23gr:
>     g23tag.setdefault(item[0],[]).append(item[1])
>
> -- Alain.

Or alternatively:

from collections import defaultdict
g23tag = defaultdict(list)
for item in tag23gr:
....g23tag[item[0]].append(item[1])

[toc] | [prev] | [next] | [standalone]


#22613

FromAlain Ketterlin <alain@dpt-info.u-strasbg.fr>
Date2012-04-03 18:26 +0200
Message-ID<87k41w4w8e.fsf@dpt-info.u-strasbg.fr>
In reply to#22611
nn <pruebauno@latinmail.com> writes:

>> > for item in tag23gr:
>> > ...        value, key = tuple(item)
>> > ...        if(g23tag.get(key)):
>> > ...                g23tag[key].append(value)
>> > ...        else:
>> > ...                g23tag[key] = [value]
>>
>> for item in tag23gr:
>>     g23tag.setdefault(item[0],[]).append(item[1])

> Or alternatively:
>
> from collections import defaultdict
> g23tag = defaultdict(list)
> for item in tag23gr:
> ....g23tag[item[0]].append(item[1])

Very handy in that case, but in general I dislike the idea of silently
inserting a default value when the access is a read, e.g., in
x=g23tag[wrung]. Explicit is better than implicit, as they say. YMMV.

-- Alain.

[toc] | [prev] | [next] | [standalone]


#22626

Fromnn <pruebauno@latinmail.com>
Date2012-04-03 11:03 -0700
Message-ID<f89fd4d4-f736-47bb-b9f1-7d42f4ce7826@h9g2000yqe.googlegroups.com>
In reply to#22613
On Apr 3, 12:26 pm, Alain Ketterlin <al...@dpt-info.u-strasbg.fr>
wrote:
> nn <prueba...@latinmail.com> writes:
> >> > for item in tag23gr:
> >> > ...        value, key = tuple(item)
> >> > ...        if(g23tag.get(key)):
> >> > ...                g23tag[key].append(value)
> >> > ...        else:
> >> > ...                g23tag[key] = [value]
>
> >> for item in tag23gr:
> >>     g23tag.setdefault(item[0],[]).append(item[1])
> > Or alternatively:
>
> > from collections import defaultdict
> > g23tag = defaultdict(list)
> > for item in tag23gr:
> > ....g23tag[item[0]].append(item[1])
>
> Very handy in that case, but in general I dislike the idea of silently
> inserting a default value when the access is a read, e.g., in
> x=g23tag[wrung]. Explicit is better than implicit, as they say. YMMV.
>
> -- Alain.

Valid point. Preferred choice depends on the access patterns to the
dict (e.g. one write and multiple reads, multiple writes and one loop
over items, etc.)

[toc] | [prev] | [next] | [standalone]


#22607

FromChris Angelico <rosuav@gmail.com>
Date2012-04-04 01:09 +1000
Message-ID<mailman.1280.1333465778.3037.python-list@python.org>
In reply to#22600
On Wed, Apr 4, 2012 at 12:36 AM, python <w.g.sneddon@gmail.com> wrote:
> for item in tag23gr:
> ...     value, key = tuple(item)
> ...     if(g23tag.get(key)):
> ...             g23tag[key].append(value)
> ...     else:
> ...             g23tag[key] = [value]

Simple enhancement: Use setdefault. Instead of the if, just use:

g23tag.setdefault(key,[]).append(value)

That'll cover both cases in one.

You can leave off the explicit tuple construction; if item is a
two-element list, you can unpack it directly. You can also embed that
straight into your for loop:

for value,key in tag23gr:

Do both and you cut your loop down to two lines. Cool! :)

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#22610

FromPeter Otten <__peter__@web.de>
Date2012-04-03 17:24 +0200
Message-ID<mailman.1282.1333466694.3037.python-list@python.org>
In reply to#22600
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)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web