I'm trying to add value to multiple keys from a function, preferbely through a method. However so far I haven't found a method for what I want to do, nor have I come across a solution to my problems.
I know that the d.update()
works for just updating the value, but that just overwrite the old result whereas I want to add the values to the key.
Below is the code I am working on:
def split(query):
query = query.split()
total = dict()
for item in query:
total.update(process(item)) # <-- I want to modify this bit
print(total)
process(item)
returns a dict
with some number of keys and values.
For example:
process(item)
might return something like this:
{
"Tim": 12.5,
"Brad": 15,
"Amy": 10
}
For the second iteration it might return:
{
"Tim": 3,
"Brad": 7,
"Amy": 8
}
Right now, total
would become the second iteration. However, I want it to add the values to the names so the new list would be:
total = {
"Tim": 15.5,
"Brad": 22,
"Amy": 18
}
If it only were 3 keys, I could just hardcode the keys. The number of keys can vary, but no new keys will be added once the code is executed.
The class collections.Counter
implements exactly what you want with its update
method:
from collections import Counter
...
def split(query):
total = Counter()
for item in query.split():
total.update(process(item))
return total
Counter
is a subclass of dict
, so you can either return it as-is or wrap it in a dict
before returning. Either way, the update
method will add together the values for any matching keys. The values can be of any type, as long as what is already in the mapping and what you pass in for a given key support the +
operator between them.
If you wanted to use only built-ins (no imports), your best bet is probably to use dict.get
to add values that might not exist yet together:
def split(query):
total = {}
for item in query.split():
for k, v in process(item).items():
total[k] = total.get(k, 0) + v
return total
you can write a loop through a dictionary like this:
for key, value in dic.iteritems():
and add new value to your dictionary.
you can write a function like this:
def update(d1, d2):
result = dict()
for key1, value1 in d1.iteritems():
for key2, value2 in d2.iteritems():
if key1 == key2:
result[key1] = str( int(value1) + int(value2) )
return result
and the use it like this:
new_dict = update(d1, d2)
Loop through the d2
, check if any of its elem
is in d1
, for each of them, add the values to d2
, wrap it all in a method that you can call anytime you need to:
def add_values(d1, d2):
for elem in d2:
if elem in d1:
d2[elem] = float(d2[elem]) + float(d1[elem])
return {**d1, **d2}
d1 = {
"Tim": "12.5",
"Brad": "15",
"Amy": 10
}
d2 = {
"Tim": "3",
"Brad": "7",
"Amy": "8"
}
print(add_values(d1, d2))
OUTPUT :
{'Tim': 15.5, 'Brad': 22.0, 'Amy': 18.0}
Can you try the following:
for key, value in item.items():
if key in total:
total[key] += value
else:
total[key] = value
A simple nested for-loop should be enough to do what you need. You can then package it up in a method:
for key in total:
if key in new_values:
total[key] += new_values[key]
This outputs:
{'Amy': 18, 'Brad': 22, 'Tim': 15.5}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.