根据键将列表中的项添加到现有字典中

时间:2017-10-22 03:38:33

标签: python dictionary

假设我有一个使用id_num作为键

的字典
{1665845 : Person(1665845, stuff)} {1294919 : Person(1294919, stuff)}

我还有另一个包含[Id_num,date,data .....]计数的列表。

[[['1665845', '2001-01-06', '28,448,615,27,705'], ['1294919', '2001-01-04', '639,118,328,413,222,491,738,389,11,372,183,650,281,643,26,398,685,171'], ['1294919', '2001-01-03', '771,373']]]

如何总结属于id_num的所有数据并将其作为字典中的新项目添加

{1294919 : Person(1294919, stuff, sum)}

4 个答案:

答案 0 :(得分:0)

我相信你的意思是这样的:

s = [[['1665845', '2001-01-06', '28,448,615,27,705'], ['1294919', '2001-01-04', '639,118,328,413,222,491,738,389,11,372,183,650,281,643,26,398,685,171'], ['1294919', '2001-01-03', '771,373']]]

final_data = {int(a):Person(int(a), b, c) for a, b, c in s[0]}

答案 1 :(得分:0)

假设现有字典为X,列表为Y,

for i in Y:
    if i[0] in Y:
        X[i[0]].sumofdata+=sum(split(i[2]))
    else:
        sum_of_data=sum(split(i[2]))
        X[i[0]]=Person(i[0], i[1],sum_of_data)

答案 2 :(得分:0)

d = {1665845: Person(1665845, stuff, 0), 1294919: Person(1294919, stuff, 0)}
l_key = [['1665845', '2001-01-06', '28,448,615,27,705'], ['1665845', '2001-01-10', '218,37,356,621,466,319,147,774,231,167,399,150,417,34,3'], ['1294919', '2001-01-04', '639,118,328,413,222,491,738,389,11,372,183,650,281,643,26,398,685,171'], ['1294919', '2001-01-10', '314,611,485,208,515,240,586,511,713,58,28,392,140,529,353,489,375,412,596'], ['1665845', '2001-01-04', '670,665,681,184,22,752,390,523,507,171,467,19,296,720,58,230,721,686'], ['1665845', '2001-01-02', '432,210,732,204,771,555,448,82,343'], ['1294919', '2001-01-06', '113,225,564,554,392,544,313'], ['1294919', '2001-01-05', '437,219,239,545,588,303,477,384,87,254,429,635,188,372,572,712,383'], ['1294919', '2001-01-03', '771,373'], ['1294919', '2001-01-08', '650,679,492,524,202,689,224,268,195,455,400,235,518,505']]

key_sum = {} # construct a empty dictionary
for k in l_key:
    values_sum = sum([int(val) for val in k[2].split(',')]) # sum of values stored in string in your case
    if not k[0] in key_sum:
        key_sum[k[0]] = values_sum # add key to temp dictionary if key not exist
    else:
        key_sum[k[0]] = key_sum[k[0]] + values_sum # add values in key if key exist

# key_sum will be {'1294919': 31022, '1665845': 17701} after execution of above loop

for element in key_sum:
    if int(element) in d:
        d[element] = Person(int(element), stuff, key_sum[element]) # in this case d[element will store the result which is returneed by Person

答案 3 :(得分:0)

看起来你可以通过collections.defaultdict

实现这一点
person_dict = {1665845 : Person(1665845,  stuff), 1294919 : Person(1294919, stuff)}  # existing dictionary
l = [['1665845', '2001-01-06', '28,448,615,27,705'], ['1665845', '2001-01-10', '218,37,356,621,466,319,147,774,231,167,399,150,417,34,3'], ['1294919', '2001-01-04', '639,118,328,413,222,491,738,389,11,372,183,650,281,643,26,398,685,171'], ['1294919', '2001-01-10', '314,611,485,208,515,240,586,511,713,58,28,392,140,529,353,489,375,412,596'], ['1665845', '2001-01-04', '670,665,681,184,22,752,390,523,507,171,467,19,296,720,58,230,721,686'], ['1665845', '2001-01-02', '432,210,732,204,771,555,448,82,343'], ['1294919', '2001-01-06', '113,225,564,554,392,544,313'], ['1294919', '2001-01-05', '437,219,239,545,588,303,477,384,87,254,429,635,188,372,572,712,383'], ['1294919', '2001-01-03', '771,373'], ['1294919', '2001-01-08', '650,679,492,524,202,689,224,268,195,455,400,235,518,505']]

from collections import defaultdict
d_dict = defaultdict(int)
for k,_,v in l:
    d_dict[int(k)]+=sum(int(i) for i in v.split(','))
#dict(d_dict)
#{1294919: 31022, 1665845: 17701} 

for k in person_dict:
    person_dict[k] = Person(k, stuff, d_dict[k])