Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is there a better way to do this snippet?

Reply
Thread Tools

Is there a better way to do this snippet?

 
 
python
Guest
Posts: n/a
 
      04-03-2012
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]
 
Reply With Quote
 
 
 
 
Alain Ketterlin
Guest
Posts: n/a
 
      04-03-2012
python <> 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.
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      04-03-2012
On Wed, Apr 4, 2012 at 12:36 AM, python <> 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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-03-2012
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)


 
Reply With Quote
 
nn
Guest
Posts: n/a
 
      04-03-2012
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])
 
Reply With Quote
 
Alain Ketterlin
Guest
Posts: n/a
 
      04-03-2012
nn <> 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.
 
Reply With Quote
 
nn
Guest
Posts: n/a
 
      04-03-2012
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.)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Parsing Binary Structures; Is there a better way / What is your way? Paul Rubin Python 5 08-06-2009 08:06 AM
Are there better ADSL modem routers out there? Bypass UK VOIP 28 01-12-2007 12:41 AM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM
There MUST be a better way! dwa ASP .Net 5 01-29-2004 02:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57