Re: Accumulating values in dictionary

2008-05-20 Thread Zack
On May 20, 10:38 am, Thomas Bellman <[EMAIL PROTECTED]> wrote: > Arnaud Delobelle <[EMAIL PROTECTED]> 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

Re: Accumulating values in dictionary

2008-05-20 Thread Thomas Bellman
Arnaud Delobelle <[EMAIL PROTECTED]> 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 wa

Re: Accumulating values in dictionary

2008-05-20 Thread Thomas Bellman
Zack <[EMAIL PROTECTED]> wrote: > There's nothing wrong (that I know of) by doing it as I have up there, > but is there a simpler, easier way? Looking forward to hearing about > how much of a n00b I am. Thanks in advance! You want the defaultdict type: import collections d = collections.

Re: Accumulating values in dictionary

2008-05-20 Thread Arnaud Delobelle
Zack <[EMAIL PROTECTED]> writes: > Given a bunch of objects that all have a certain property, I'd like to > accumulate the totals of how many of the objects property is a certain > value. Here's a more intelligible example: > > Users all have one favorite food. Let's create a dictionary of > favor

Re: Accumulating values in dictionary

2008-05-20 Thread Zack
On May 20, 12:26 pm, Zack <[EMAIL PROTECTED]> wrote: > Given a bunch of objects that all have a certain property, I'd like to > accumulate the totals of how many of the objects property is a certain > value. Here's a more intelligible example: > > Users all have one favorite food. Let's create a di

Accumulating values in dictionary

2008-05-20 Thread Zack
Given a bunch of objects that all have a certain property, I'd like to accumulate the totals of how many of the objects property is a certain value. Here's a more intelligible example: Users all have one favorite food. Let's create a dictionary of favorite foods as the keys and how many people hav