亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將字典列表與內部列表連接起來

如何將字典列表與內部列表連接起來

瀟湘沐 2022-01-05 10:49:31
我有一個帶有內部列表和字典的字典列表,我需要將它們連接到一個大字典中。名單是這樣的:[{'total': {'last_day': '7'}},  {'total': {'progress': '07/04'}},  {'total': {'piecies': '3008'}},  {'total': {'week': ['16.0']}},  {'total': {'week': ['17.0']}},  {'total': {'week': ['15.0']}},  {'total': {'week': ['17.0']}},  {'total': {'week': ['16.0']}},  {'total': {'week': ['13.0']}},  {'total': {'week': ['6.0']}},  {'tkts': [{'tktvalue': '13.5'}]},  {'tkts': [{'month': {'consuntivato_pezzi': '2346'}}]},  {'tkts': [{'month': {'consuntivato_euro': '31671.00'}}]},  {'tkts': [{'month': {'preventivato_pezzi': '9897'}}]}]我嘗試了一些 for 循環和遞歸函數,但沒有很好的結果for vars in temporary_var:    for title in vars:        try:            try:                table_var[title].update(vars[title])            except KeyError:                table_var[title] = vars[title]        except AttributeError:            table_var[title].append(vars[title][0])但我只能得到這個:{'total': {'last_day': '7', 'progress': '07/04', 'volumes': '3008', 'week': ['6.0']}, 'tkts': [{'service': 'SOSPC'}, {'tktvalue': '13.5'}, {'month': {'volumes1': '2346'}}, {'month': {'volumes2': '31671.00'}}, {'month': {'volumes3': '9897'}}]}但我需要這個:{'total': {'last_day': '7', 'progress': '07/04', 'volumes': '3008', 'week': ['16.0', '17.0', '15.0', '17.0', '16.0', '13.0','6.0']}, 'tkts': [{'service': 'SOSPC', 'tktvalue': '13.5', 'month': {'volumes1': '2346', 'volumes2': '31671.00', 'volumes3': '9897'}}]}
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

試試這個,它會得到你想要的輸出:



lst=[{'total': {'last_day': '7'}},

 {'total': {'progress': '07/04'}},

 {'total': {'piecies': '3008'}},

 {'total': {'week': ['16.0']}},

 {'total': {'week': ['17.0']}},

 {'total': {'week': ['15.0']}},

 {'total': {'week': ['17.0']}},

 {'total': {'week': ['16.0']}},

 {'total': {'week': ['13.0']}},

 {'total': {'week': ['6.0']}},

 {'tkts': [{'tktvalue': '13.5'}]},

 {'tkts': [{'month': {'consuntivato_pezzi': '2346'}}]},

 {'tkts': [{'month': {'consuntivato_euro': '31671.00'}}]},

 {'tkts': [{'month': {'preventivato_pezzi': '9897'}}]}

]


d={} # the dictionary that will hold the result


for dd in lst: # for each dictionary in the list of dictionaries

    for key,value in dd.items():

        if key not in d: # key does not exist in dictionary d

            d[key]=value


        else: # key exists in dictionary d

            if isinstance(value,dict): # check if the value is a dictionary or a list

                for key1,value2 in value.items():

                    if key1 not in d[key]:

                        d[key]={**d[key],**value} # combine the dictionaries

                    else:

                        d[key][key1].append(value2[0])


            elif isinstance(value,list): # check if the value is a  list

                if isinstance(value[0],dict): # check if the value is a dictionary or a list

                    for key1,value2 in value[0].items():

                        if key1 not in d[key][0]:

                            d[key][0]={**d[key][0],**value[0]}

                        else:

                            d[key][0][key1]={**d[key][0][key1],**value2}


print(d)




輸出:{'total': {'last_day': '7', 'progress': '07 / 04 ',' piecies ':' 3008 ',' week ': ['16 .0', '17 .0' , '15 .0', '17 .0', '16 .0', '13 .0',' 6.0 ']},' tkts ': [{' tktvalue ': '13 .5', '月' :{'consuntivato_pizzi':'2346','final_euro':'31671.00','quoted_pieces':'9897'}}]}


查看完整回答
反對 回復 2022-01-05
?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

嘗試在此處使用默認列表。您可以了解有關它們的更多信息collections.defaultdict


from collections import defaultdict

result = defaultdict(list)

for sequence in (yourdict):

    for keys,dictionary in sequence.items():

          result[keys].append(dictionary)



print(dict(result))

輸出 :


{'total': [{'last_day': '7'}, {'progress': '07/04'}, {'piecies': '3008'}, {'week': ['16.0']}, {'week': ['17.0']}, {'week': ['15.0']}, {'week': ['17.0']}, {'week': ['16.0']}, {'week': ['13.0']}, {'week': ['6.0']}], 'tkts': [[{'tktvalue': '13.5'}], [{'month': {'consuntivato_pezzi': '2346'}}], [{'month': {'consuntivato_euro': '31671.00'}}], [{'month': {'preventivato_pezzi': '9897'}}]]}



查看完整回答
反對 回復 2022-01-05
  • 2 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號