[portland] Still Having List/Tuple Problems

Rich Shepard rshepard at appl-ecosys.com
Thu Apr 17 00:23:29 CEST 2008


On Wed, 16 Apr 2008, Dylan Reinhardt wrote:

> Another approach to add to the pile:
>
>>>> my_tuples = [('A',1),('A',2), ('A',3), ('A',4), ('B',1), ('B',2),
> ('B',3), ('B',4)]
>>>> grouper = {}
>>>> for left, right in my_tuples:
> ...         grouper.setdefault(left, []).append(right)
> ...
>
> now you have a dict with values groped by keys, as shown below...
>
>>>> for key in grouper.keys():
> ...        print key
> ...        for value in grouper[key]:
> ...             print '\t%s' % value
> ...
> A
> 	1
> 	2
> 	3
> 	4
> B
> 	1
> 	2
> 	3
> 	4

Dylan,

   Thank you for expanding my horizons. While all the examples print the
right side item of each tuple, I need to interate through those for each
left side item. It's fighting me, but that's my lack of python experience
showing up. I'll get there.

Much appreciated,

Rich

-- 
Richard B. Shepard, Ph.D.               |  Integrity            Credibility
Applied Ecosystem Services, Inc.        |            Innovation
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


More information about the Portland mailing list