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
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
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.
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
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
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