dictionary with tuple keys

Discussion in 'Python' started by Brandon Devine, Dec 15, 2009.

  1. Hi all,

    I am probably not thinking straight anymore about this problem. I
    have a dictionary with tuple keys in the format (a, b, A, B) and float
    values. I want to collect all the keys with identical (a, b...),
    disregarding whatever (... A, B) might be. Specifically, I want to
    sum the float values once I've collected these keys. I am staring at
    my screen, pondering ugly things, and I just know I must be missing a
    Pythonic solution. Any suggestions?

    Thanks for any help,

    Brandon
     
    Brandon Devine, Dec 15, 2009
    #1
    1. Advertising

  2. Brandon Devine

    Chris Rebert Guest

    On Mon, Dec 14, 2009 at 9:49 PM, Brandon Devine <> wrote:
    > Hi all,
    >
    > I am probably not thinking straight anymore about this problem.  I
    > have a dictionary with tuple keys in the format (a, b, A, B) and float
    > values.  I want to collect all the keys with identical (a, b...),
    > disregarding whatever (... A, B) might be.  Specifically, I want to
    > sum the float values once I've collected these keys.  I am staring at
    > my screen, pondering ugly things, and I just know I must be missing a
    > Pythonic solution.  Any suggestions?


    from collections import defaultdict

    new_dict = defaultdict(int)
    for tupkey in your_dict:
    new_key = tupkey[:2]
    new_dict[new_key] += your_dict[tupkey]

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Dec 15, 2009
    #2
    1. Advertising

  3. Brandon Devine

    Paul Rubin Guest

    Brandon Devine <> writes:
    > I am probably not thinking straight anymore about this problem. I
    > have a dictionary with tuple keys in the format (a, b, A, B) and float
    > values. I want to collect all the keys with identical (a, b...),
    > disregarding whatever (... A, B) might be. Specifically, I want to
    > sum the float values once I've collected these keys. I am staring at
    > my screen, pondering ugly things, and I just know I must be missing a
    > Pythonic solution. Any suggestions?


    a,b are the first two elments of the tuple and d is the dict? I.e.
    I think you want

    interesting_keys = [k for k in d.keys() if k[:2] == (a,b)]
    sum_of_values = sum(d[k] for k in interesting_keys)

    You could write it a little differently to make fewer intermediate
    results and so forth, but the above shows the idea.
     
    Paul Rubin, Dec 15, 2009
    #3
  4. Brandon Devine

    John Machin Guest

    Ben Finney <ben+python <at> benfinney.id.au> writes:

    > In this case, I'll use ‘itertools.groupby’ to make a new sequence of
    > keys and values, and then extract the keys and values actually wanted.


    Ah, yes, Zawinski revisited ... itertools.groupby is the new regex :)

    > Certainly it might be clearer if written as one or more loops, instead
    > of iterators. But I find the above relatively clear, and using the
    > built-in iterator objects will likely make for a less buggy
    > implementation.


    Relative clarity like relative beauty is in the eye of the beholder,
    and few parents have ugly children :)

    The problem with itertools.groupby is that unlike SQL's "GROUP BY"
    it needs sorted input. The OP's requirement (however interpreted)
    can be met without sorting.

    Your interpretation can be implemented simply:

    from collections import defaultdict
    result = defaultdict(list)
    for key, value in foo.iteritems():
    result[key[:2]].append(value)
     
    John Machin, Dec 15, 2009
    #4
  5. So grateful! Thanks to all. The breadth of Python continues to amaze
    me, as does your generosity.

    ("Relative clarity like relative beauty is in the eye of the
    beholder,
    and few parents have ugly children"... fantastic!)

    Brandon
     
    Brandon Devine, Dec 15, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    812
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    961
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    615
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    828
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    970
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page