Accumulating values in dictionary
Zack
zhalbrecht at gmail.com
Tue May 20 16:05:53 EDT 2008
On May 20, 10:38 am, Thomas Bellman <bell... at lysator.liu.se> wrote:
> Arnaud Delobelle <arno... at googlemail.com> wrote:
> > from collections import defaultdict
> > d = defaultdict(int) # That means the default value will be 0
> > for person in people:
> > d[person.fav_food] += 1
>
> Ah! I didn't think of using int as the factory function. If you
> use this, then I believe the warning I gave about performance
> does not apply; my understanding is that calling built-in functions
> (like the int constructor) is fast.
>
> --
> Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
> "Beware of bugs in the above code; I have ! bellman @ lysator.liu.se
> only proved it correct, not tried it." ! Make Love -- Nicht Wahr!
Arnaud, that is so badass re: int
More information about the Python-list
mailing list