I am trying to add values for duplicate keys in a dictionary. I am not sure why my code isn't working.
d = {'inds': [], 'vals': []}
d['inds'] = [0, 3, 7, 3, 3, 5, 1]
d['vals'] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
I would like to get a list with the added values as result at index 3 and 0 for an index that doesn't exist:
desired_output=[1.0, 7.0, 0.0, 11.0, 0.0, 6.0, 0.0, 3.0]
My code: I am making list of zeros and adding dictionary in a loop.
d = {'inds': [], 'vals': []}
d['inds'] = [0, 3, 7, 3, 3, 5, 1]
d['vals'] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
length=len(d['inds'])
a=[0]*(length)
for key,val in d.items():
a[key]= a[key] + d[val]
It's throwing an error: TypeError: list indices must be integers or slices, not str
Appreciate your help.
you don't even need a dictionary here (but I'll leave it in). just zip indices & values and add to your zeroed list. Also the length of your result list is incorrect. Compute it using max of the indices plus one:
d = {}
d['inds'] = [0, 3, 7, 3, 3, 5, 1]
d['vals'] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
result = [0]*(max(d["inds"])+1)
for a,b in zip(d['inds'],d['vals']):
result[a] += b
result:
[1.0, 7.0, 0, 11.0, 0, 6.0, 0, 3.0]
for key, val in d.items():
This loops over the items at the top-level of d: first key='inds' and val=[0, 3, 7, 3, 3, 5, 1], and then key='vals' and val=[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]. But you're looking for key to take on the values 0, 3, 7, 3, etc., and for val to be 1.0, 2.0, 3.0, etc.
To loop over the indices in d['inds'] and the values in d['vals'], use zip. It will pull one item at a time from each sublist in parallel.
for key, val zip(d['inds'], d['vals']):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With