dictionary with tuple keys

B

Brandon Devine

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
 
C

Chris Rebert

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
 
P

Paul Rubin

Brandon Devine said:
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.
 
J

John Machin

Ben Finney said:
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)
 
B

Brandon Devine

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top