Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Is there a better way to do this snippet?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


w.g.sneddon at gmail

Apr 3, 2012, 7:36 AM

Post #1 of 7 (277 views)
Permalink
Is there a better way to do this snippet?

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]
--
http://mail.python.org/mailman/listinfo/python-list


alain at dpt-info

Apr 3, 2012, 8:02 AM

Post #2 of 7 (268 views)
Permalink
Re: Is there a better way to do this snippet? [In reply to]

python <w.g.sneddon [at] gmail> 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.
--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Apr 3, 2012, 8:09 AM

Post #3 of 7 (268 views)
Permalink
Re: Is there a better way to do this snippet? [In reply to]

On Wed, Apr 4, 2012 at 12:36 AM, python <w.g.sneddon [at] gmail> 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
--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Apr 3, 2012, 8:24 AM

Post #4 of 7 (265 views)
Permalink
Re: Is there a better way to do this snippet? [In reply to]

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)


--
http://mail.python.org/mailman/listinfo/python-list


pruebauno at latinmail

Apr 3, 2012, 8:37 AM

Post #5 of 7 (264 views)
Permalink
Re: Is there a better way to do this snippet? [In reply to]

On Apr 3, 11:02am, 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])
--
http://mail.python.org/mailman/listinfo/python-list


alain at dpt-info

Apr 3, 2012, 9:26 AM

Post #6 of 7 (265 views)
Permalink
Re: Is there a better way to do this snippet? [In reply to]

nn <pruebauno [at] latinmail> 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.
--
http://mail.python.org/mailman/listinfo/python-list


pruebauno at latinmail

Apr 3, 2012, 11:03 AM

Post #7 of 7 (265 views)
Permalink
Re: Is there a better way to do this snippet? [In reply to]

On Apr 3, 12:26pm, 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.)
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.