[portland] Iterating Through Tuples In A List
Nick Welch
mack at incise.org
Sat Feb 16 09:00:21 CET 2008
On Fri, Feb 15, 2008 at 12:43:36PM -0800, jason kirtland wrote:
> the groupby function in itertools is one way:
> 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]
Dang, that's pretty sweet. I remember seeing itemgetter before, but
groupby is a new discovery.
pprint.pprint([
(component, map(itemgetter(3), grouped))
for component, grouped
in groupby(rows, key=itemgetter(0))
])
Functional programming that looks like a perverted SQL. The world needs
more of this.
--
Nick Welch | mack @ incise.org | http://incise.org
More information about the Portland
mailing list