3

I have a dictionary of names and ages. Some of the names are identical. I want to sum the age of the names that are identical.

My fake data looks like this:

pete: 33
ann: 7
ruth: 3
ann: 5
austin: 90

In the examples there are two anns. So I want to sum the ages of the two anns. Currently I have a dictionary:

dict = {'pete':33,'ann':7,'ruth':3,'ann':5,'austin':90}

My result should look like this

dict = {'pete':33,'ann':12,'ruth':3,'austin':90}

pete: 33
ann: 12
ruth: 3
austin: 90

I think to put the data in a dictionary like this isn't the best solution. What is a good other solution to store the data and process them into the output?

4

3 回答 3

4

你的假数据不可能是那样的。字典中不可能有两个具有相同键的条目,也许您打算使用不同的数据结构?(不是字典)。但是,如果您的数据如下所示:

input = [('pete', 33), ('ann',7), ('ruth',3), ('ann',5), ('austin',90)]

那么adefaultdict将是一个好主意:

from collections import defaultdict
d = defaultdict(int)

for k, v in input:
    d[k] += v

d
=> defaultdict(<type 'int'>, {'pete': 33, 'ann': 12, 'ruth': 3, 'austin': 90})

或使用Counter

from collections import Counter
d = Counter()

for k, v in input:
    d.update({k:v})

d
=> Counter({'austin': 90, 'pete': 33, 'ann': 12, 'ruth': 3})

还有另一个解决方案,无需导入额外的库:

d = {}
for k, v in input:
    if k in d:
        d[k] += v
    else:
        d[k] = v

d
=> {'pete': 33, 'ann': 12, 'ruth': 3, 'austin': 90}
于 2013-07-30T22:51:16.517 回答
3
data = [('pete', 33), ('ann', 7), ('ruth', 3), ('ann', 5), ('austin', 90)]

由于 dicts 不能包含重复的键,因此您可以从元组列表开始。

from collections import defaultdict
combined = defaultdict(int)

for name, age in data:
    combined[name] += age

然后构建dict使用defaultdict. 诀窍是defaultdict(int)创建一个字典,其条目默认为 0,因此您不必处理不存在的键。

于 2013-07-30T22:54:45.213 回答
3

您需要使用元组列表,而不是使用字典

pairs = [ ('pete', 33), ('ann', 7), ('ruth', 3), ('ann', 5), ('austin', 90) ]

然后您可以使用 defaultdict 计算总和:

from collections import defaultdict
answer = defaultdict(int)
for name, number in pairs:
    answer[name] += number

print(answer)

defaultdict 通过调用给定函数 (int) 来为任何不存在的键提供默认值(int() 方便地返回 0);然后对于每次迭代,将数字添加到其中。

于 2013-07-30T22:56:50.453 回答