Re: dictionary with tuple keys
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary with tuple keys
Ben Finney 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) -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary with tuple keys
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary with tuple keys
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? The Schwartzian transform, though primarily designed for unusual sorting requirements, seems applicable here. It's a good tool to reach for in many situations. In other words: make a new dictionary, based on the key function you are interested in. 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. >>> import pprint >>> pprint.pprint(foo) {('a', 'b', 'A', 'B'): 1.1001, ('a', 'b', 'X', 'Y'): 2.2002, ('c', 'd', 'A', 'B'): 3.2998, ('e', 'f', 'P', 'Q'): 4.4004, ('e', 'f', 'R', 'S'): 5.5, ('e', 'f', 'T', 'U'): 6.5996} >>> import itertools >>> pprint.pprint( ... dict( ... (key, list( ... x[1] for x in items)) ... for (key, items) in ... itertools.groupby( ... sorted([ ... (k[0:2], value) ... for (k, value) in foo.items() ... ]), ... lambda v: v[0] ... ) ... ) ... ) {('a', 'b'): [1.1001, 2.2002], ('c', 'd'): [3.2998], ('e', 'f'): [4.4004, 5.5, 6.5996]} Possibly someone could remove some of the processing in the middle there, but I couldn't quickly see a way. 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. -- \“The whole area of [treating source code as intellectual | `\property] is almost assuring a customer that you are not going | _o__) to do any innovation in the future.” —Gary Barnett | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary with tuple keys
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 -- http://mail.python.org/mailman/listinfo/python-list