[portland] Iterating Through Tuples In A List
Rich Shepard
rshepard at appl-ecosys.com
Fri Feb 15 22:00:22 CET 2008
On Fri, 15 Feb 2008, jason kirtland wrote:
> the groupby function in itertools is one way:
>
> rows = [('HabitatComplexity',2,1,'Decay S-Curve'),
> ('HabitatComplexity',2,2,'Growth S-Curve'),
> ('SpecialConcern',3,1,'Decay S-Curve'),
> ('SpecialConcern',3,2,'Bell Curve'),
> ('SpecialConcern',3,3,'Growth S-Curve'),
> ('Variety',2,1,'Decay S-Curve'),
> ('Variety',2,2,'Growth S-Curve'),]
> from operator import itemgetter
> from itertools import groupby
>
> for component, grouped in groupby(rows, key=itemgetter(0)):
> print component
> for row in grouped:
> print "\t%s" % row[3]
Jason,
This looks more elegant and maintainable that what I was doing. I'll read
about itertools and give this a try, too.
Thank you,
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