Inefficient summing
Terry Reedy
tjreedy at udel.edu
Thu Oct 9 14:47:29 EDT 2008
Matt Nordhoff wrote:
> Chris Rebert wrote:
>> I personally would probably do:
>>
>> from collections import defaultdict
>>
>> label2sum = defaultdict(lambda: 0)
>
> FWIW, you can just use:
>
> label2sum = defaultdict(int)
>
> You don't need a lambda.
Indeed, in this case, with two known keys, the defaultdict is not needed
either, since the following should work as well to initialize
label2sum = {'F1':0,'F2':0}
>> for r in rec:
>> for key, value in r.iteritems():
>> label2sum[key] += value
>>
>> ratio = label2sum["F1"] / label2sum["F2"]
More information about the Python-list
mailing list