2017-10-22 37 views
0

說我有這個字典,使用id_num作爲重點從列表中添加項目到現有的基於字典的關鍵

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

而且我有了含有[ID_NUM,日期,數據列表的另一個列表... ..]計數。

[[['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)} 
+0

如何做到這一點,確實如此。什麼是人?串?自定義對象? Namedtuple?什麼是總結? –

+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]} 
+0

OP提到他們有一個現有的字典,這應該以某種方式來(我不能肯定,因爲可憐的措辭)。 –

+0

對不起,我添加了更多的細節到OP –

+0

我想他的意思是,'c = sum(map(int,c.split(「,」)))' –

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) 
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 
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]) 
+0

當我嘗試在我的場景中出現錯誤「Person '對象不支持項目分配' –

+0

@JayDilla:那麼如果你需要*那*的幫助,你將不得不提供完整的代碼。見[SSCCE](http://sscce.org)。 –

+0

@Jay Dilla,好吧,我認爲你應該簡化你的問題沒有類Person來獲得想法 – Transhuman

相關問題