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

2012-04-03 Thread nn
On Apr 3, 12:26 pm, Alain Ketterlin 
wrote:
> 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.

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


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

2012-04-03 Thread Alain Ketterlin
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2012-04-03 Thread nn
On Apr 3, 11:02 am, Alain Ketterlin 
wrote:
> 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.

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


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

2012-04-03 Thread Peter Otten
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


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

2012-04-03 Thread Chris Angelico
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
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2012-04-03 Thread Alain Ketterlin
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.
-- 
http://mail.python.org/mailman/listinfo/python-list