私は辞書のリストを持っています:
dictlist = [{'url': 'google.com', 'a': 10, 'content': 'google', 'd': 80, 'f': 1, 'lock': 'dd'}, {'url': 'fb.com', 'z': 25, 'content': 'google', 'd': 60, 'p': 1, 'a': 19}]
上記から新しい辞書を作成する必要がありますdictlist
。
newdict= {}
sumlist = ['a', 'z', 'd'] #Get values for these from dictlist
for dict in dictlist:
newdict['newurl'] = dict['url']
newdict['newtitle'] = dict['content']
newdict['sumvalue'] = ?????
#so that for 1st item its 'sumvalue'= a + z + d = 10 + 0 + 80 = 90 (zero for 'z')
#and 2nd item has 'sumvalue' = a + z + d = 19 + 25 + 60 = 104
print newdict[0] # should result {'newurl': 'google.com', 'newtitle': 'google', 'sumvalue' : 80 }
リストからすべての値の合計を取得するためにdict
ofを反復処理する方法がわかりませんdictlist
sumlist[]
それぞれの辞書項目の値の合計を取得する必要があります。
提案してください。