map/filter/reduce/lambda opinions and background unscientific mini-survey

Devan L devlai at gmail.com
Wed Jul 6 12:01:14 EDT 2005


> Here's a couple of examples from my own code:
>
> # from a Banzhaf Power Index calculator
> # adds things that aren't numbers
> return reduce(operator.add,
>     (VoteDistributionTable({0: 1, v: 1}) for v in electoral_votes))

return sum([VoteDistributionTable({0:1, v:1} for v in
electoral_votes],VoteDistributionTable({}))
Any time you use operator.add, you can probably use
sum(sequence,initialvalue)

> # from a custom numeric class
> # converts a tuple of digits into a number
> mantissa = sign * reduce(lambda a, b: 10 * a + b, mantissa)

I'll admit I can't figure out a way to replace reduce without writing
some ugly code here, but I doubt these sorts of things appear often.




More information about the Python-list mailing list