Duplicate keys in dict?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sun Mar 7 11:53:55 EST 2010
On Sun, 07 Mar 2010 08:23:13 -0800, vsoler wrote:
> Hello,
>
> My code snippet reads data from excel ranges. First row and first column
> are column headers and row headers respectively. After reding the range
> I build a dict.
>
> ................'A'..............'B'
> 'ab'............3................5
> 'cd'............7................2
> 'cd'............9................1
> 'ac'............7................2
>
> d={('ab','A'): 3, ('ab','B'): 5, ('cd','A'): 7, ...
>
> However, as you can see there are two rows that start with 'cd', and
> dicts, AFAIK do not accept duplicates.
> One of the difficulties I find here is that I want to be able to easily
> sum all the values for each row key: 'ab', 'cd' and 'ac'. However,
> using lists inside dicts makes it a difficult issue for me.
Given the sample above, what answer do you expect for summing the 'cd'
row? There are four reasonable answers:
7 + 2 = 9
9 + 1 = 10
7 + 2 + 9 + 1 = 19
Error
You need to decide what you want to do before asking how to do it.
--
Steven
More information about the Python-list
mailing list