Accumulating values in dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zack

    Accumulating values in dictionary

    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 have that food as their
    favorite as the value.

    d = {}
    for person in People:
    fav_food = person.fav_food
    if d.has_key(fav_f ood):
    d[fav_food] = d[fav_food] + 1
    else:
    d[fav_food] = 1

    d ends up being something like: {'pie': 400, 'pizza': 200, 'burgers':
    100} if 400 people like pie, 200 like pizza and 100 like burgers.

    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!
  • Zack

    #2
    Re: Accumulating values in dictionary

    On May 20, 12:26 pm, Zack <zhalbre...@gma il.comwrote:
    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 have that food as their
    favorite as the value.
    >
    d = {}
    for person in People:
    fav_food = person.fav_food
    if d.has_key(fav_f ood):
    d[fav_food] = d[fav_food] + 1
    else:
    d[fav_food] = 1
    >
    d ends up being something like: {'pie': 400, 'pizza': 200, 'burgers':
    100} if 400 people like pie, 200 like pizza and 100 like burgers.
    >
    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!
    Er. OK so I realize now that I could have just done this:

    d = {}
    for person in people:
    fav_food = person.fav_food
    d[fav_food] = d.get(fav_food, 0) + 1

    Comment

    • Arnaud Delobelle

      #3
      Re: Accumulating values in dictionary

      Zack <zhalbrecht@gma il.comwrites:
      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 have that food as their
      favorite as the value.
      >
      d = {}
      for person in People:
      fav_food = person.fav_food
      if d.has_key(fav_f ood):
      d[fav_food] = d[fav_food] + 1
      else:
      d[fav_food] = 1
      This is fine. If I wrote this, I'd write it like this:

      d = {}
      for person in people:
      fav_food = person.fav_food
      if fav_food in d:
      d[fav_food] += 1
      else:
      d[fav_food] = 1

      You can use d.get() instead:

      d = {}
      for person in people:
      fav_food = person.fav_food
      d[fav_food] = d.get(fav_food, 0) + 1

      Or you can use a defaultdict:

      from collections import defaultdict
      d = defaultdict(int ) # That means the default value will be 0
      for person in people:
      d[person.fav_food] += 1

      HTH

      --
      Arnaud

      Comment

      • Thomas Bellman

        #4
        Re: Accumulating values in dictionary

        Zack <zhalbrecht@gma il.comwrote:
        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.def aultdict(lambda : 0)
        for person in People:
        fav_food = person.fav_food
        d[fav_food] += 1

        New in Python 2.5, so if you require older Python versions, you
        can't use that. Note also that this can become slow if most keys
        are unique; the function given to defaultdict will be called the
        first time a key is mentioned, and if the keys are mostly unique,
        that will be the majority of the times, and calling a pure Python
        function is fairly slow in CPython. (It probably won't matter
        unless you have many thousands of unique keys, though.)


        --
        Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
        "If you ignore performance problems long enough, they ! bellman @
        somehow seem to go away." -- Barry A Warsaw ! lysator.liu.se

        Comment

        Working...