Pythonic way to sum n-th list element?
Steven Taschuk
staschuk at telusplanet.net
Sat Apr 19 13:26:32 EDT 2003
Quoth Alex Martelli:
[...]
> "sum([y[1] for y in x])" would be very simple and direct, IMHO; the
> "same" thing expressed with "reduce(operator.add,[y[1] for y in x])"
> may only feel "simple and direct" to PhD's in maths -- it feels
> _abstruse_ to most people, in my opinion.
What if operator.add were changed to take any number of arguments?
Then you could write
operator.add(*[y[1] for y in x])
Clear? Abstruse?
Btw, I don't know about most people, but I don't have a PhD in
math, and reduce(operator.add, L) seems simple and clear to me.
(I do agree that there is a point beyond which abstraction brings
diminishing returns; but reduce is well inside that limit, imho.)
--
Steven Taschuk staschuk at telusplanet.net
"Our analysis begins with two outrageous benchmarks."
-- Clinger et al., "Implementation strategies for continuations"
More information about the Python-list
mailing list