我有一本類似于 json 格式的字典。如下圖:-
dito1 ={'result2':[{'math':20,'science':22,'english':31},{'math':26,'science':27,'english':33},
{'math':15,'science':55,'english':44}],'pet2':0,
'result':[{'rt':66,'io':49,'op':10},{'rt':24,'io':25,'op':5}],'pet':20}
現在我想使用上述值創建一個新字典,新字典必須如下所示:-
{'type': 'etc', 'scores': [{'math': 20, 'science': 22, 'english': 31}, {'math': 26, 'science': 27, 'english': 33}, {'math': 15, 'science': 55, 'english': 44}], 'math total': 61,'scores2':[{'math': 66, 'science': 49, 'english': 10}, {'math': 24, 'science': 25, 'english': 5}],'math total':90}
在第一個字典中有兩個鍵 result2,result 具有串列的分數資訊。通過使用這些資訊,它們必須按上述方式進行轉換,第二個字典中的數學總分是每個串列中數學的總分。我試圖獲得所需的輸出,但我無法根據它們所屬的鍵將它們分成兩部分。我試過的代碼如下:-
dito1 ={'result2':[{'math':20,'science':22,'english':31},{'math':26,'science':27,'english':33},
{'math':15,'science':55,'english':44}],'pet2':0,
'result':[{'rt':66,'io':49,'op':10},{'rt':24,'io':25,'op':5}],'pet':20}
reslt=[]
math_total=[]
for key,value in dito1.items():
if key == 'result' or key == 'result2':
for i in value:
for k,v in i.items():
if k == 'math':
t=v
if k == 'rt':
t=v
if k == 'science':
r=v
if k == 'io':
r=v
if k=='op':
e=v
if k=='english':
e=v
rest ={'math':t,'science':r,'english':e}
math_total.append(t)
reslt.append(rest)
final_dict={'type':'etc','math total':reslt,'scores':sum(math_total)}
print(final_dict)
我得到的輸出是:-
{'type': 'etc', 'scores': [{'math': 20, 'science': 22, 'english': 31}, {'math': 26, 'science': 27, 'english': 33}, {'math': 15, 'science': 55, 'english': 44}, {'math': 66, 'science': 49, 'english': 10}, {'math': 24, 'science': 25, 'english': 5}], 'math total': 151}
例外輸出:-
{'type': 'etc', 'scores': [{'math': 20, 'science': 22, 'english': 31}, {'math': 26, 'science': 27, 'english': 33}, {'math': 15, 'science': 55, 'english': 44}], 'math total': 61,'scores2':[{'math': 66, 'science': 49, 'english': 10}, {'math': 24, 'science': 25, 'english': 5}],'math total2':90}
uj5u.com熱心網友回復:
順便說一句,我建議重新考慮 this 的結構/命名方案final_dict;使用像'scores'和這樣的鍵'scores2'表示您可能需要一個list結構而不是一個dict結構。
dito1 = {'result2': [{'math': 20, 'science': 22, 'english': 31}, {'math': 26, 'science': 27, 'english': 33}, {'math': 15, 'science': 55, 'english': 44}], 'pet2': 0, 'result': [{'rt': 66, 'io': 49, 'op': 10}, {'rt': 24, 'io': 25, 'op': 5}], 'pet': 20}
translate = {'rt':'math','io':'science','op':'english'}
final_dict = {'type':'etc'}
for key,list_of_dict in dito1.items():
if key in ['result', 'result2']:
score_key = key.replace('result','scores')
final_dict[score_key] = []
math_total_key = key.replace('result','math_total')
final_dict[math_total_key] = 0
for d in list_of_dict:
# translate to the intended score names as needed
for k in ['rt','io','op']:
if k in d:
d[translate[k]] = d.pop(k)
# append the scores to the appropriate nested dict
final_dict[score_key].append(dict(d))
# and increment the math_total for that set of scores
final_dict[math_total_key] = d['math']
print(final_dict)
結果是
for key,value in final_dict.items():
print(f'{key}: {value}')
# Output:
type: etc
scores2: [{'math': 20, 'science': 22, 'english': 31}, {'math': 26, 'science': 27, 'english': 33}, {'math': 15, 'science': 55, 'english': 44}]
math_total2: 61
scores: [{'math': 66, 'science': 49, 'english': 10}, {'math': 24, 'science': 25, 'english': 5}]
math_total: 90
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/340165.html
上一篇:在Python中創建嵌套字典
