第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在Python中將字典列表中的鍵值向上移動(dòng)一級(jí)

如何在Python中將字典列表中的鍵值向上移動(dòng)一級(jí)

慕桂英3389331 2023-08-22 10:26:51
使用Python3,我試圖將字典列表中的鍵值對(duì)向上移動(dòng)。我有一個(gè)名為 Product 的變量,其中包含以下內(nèi)容:    [     {'color': 'red',      'shape': 'round',      'extra': {'price': 'large',                'onsale': 'yes',               'instock: 'yes'}    },     {'color': 'blue',      'shape': 'square',      'extra': {'price': 'small',                'onsale': 'no',               'instock: 'yes'}    }    ]我想將 extra 內(nèi)的“instock”鍵值對(duì)移至上一級(jí),以與顏色、形狀、extra 保持一致 - 所以這樣:    [     {'color': 'red',      'shape': 'round',      'extra': {'price': 'large',                'instock: 'yes'},     'onsale': 'yes'    },     {'color': 'blue',      'shape': 'square',      'extra': {'price': 'small',                'onsale': 'no'},     'instock: 'yes'    }    ]我嘗試使用在這里找到的以下代碼:    result = {}    for i in products:        if i["href"] not in result:            result[i["selection_id"]] = {'selection_id': i["selection_id"], 'other_data':                         i["other_data"], 'value_dict': []}        result[i["selection_id"]]["value_dict"].append({'value': i["value"], "value_name": i["value_name"]})這對(duì)我不起作用。我可以在網(wǎng)上找到的任何幫助或其他文獻(xiàn)將不勝感激!
查看完整描述

3 回答

?
撒科打諢

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊

非常簡(jiǎn)單:遍歷列表。對(duì)于每個(gè)字典,將“extra”.“instock”復(fù)制到上一層并刪除原始的:


for outer_dict in product:

    outer_dict["instock"] = outer_dict["extra"]["instock"]

    del outer_dict["extra"]["instock"]


for outer_dict in product:

    print(outer_dict)

輸出:


{'color': 'red', 'shape': 'round', 'extra': {'price': 'large', 'onsale': 'yes'}, 'instock': 'yes'}

{'color': 'blue', 'shape': 'square', 'extra': {'price': 'small', 'onsale': 'no'}, 'instock': 'yes'}



查看完整回答
反對(duì) 回復(fù) 2023-08-22
?
aluckdog

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊

lst = [ 

    {'color': 'red', 

     'shape': 'round', 

     'extra': {'price': 'large', 

               'onsale': 'yes',

               'instock': 'yes'}

    }, 

    {'color': 'blue', 

     'shape': 'square', 

     'extra': {'price': 'small', 

               'onsale': 'no',

               'instock': 'yes'}

    }

]


for d in lst:

    d['instock'] = d['extra'].pop('instock')


# pretty print on screen:

from pprint import pprint

pprint(lst)

印刷:


[{'color': 'red',

  'extra': {'onsale': 'yes', 'price': 'large'},

  'instock': 'yes',

  'shape': 'round'},

 {'color': 'blue',

  'extra': {'onsale': 'no', 'price': 'small'},

  'instock': 'yes',

  'shape': 'square'}]

或者你可以使用:


d['extra'].pop('instock', 'no')

如果沒有鍵(在這種情況下instock為默認(rèn)值)no


查看完整回答
反對(duì) 回復(fù) 2023-08-22
?
qq_花開花謝_0

TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊

products = [

    {'color': 'red', 

     'shape': 'round', 

     'extra': {'price': 'large', 

               'onsale': 'yes',

               'instock': 'yes'}

    }, 

    {'color': 'blue', 

     'shape': 'square', 

     'extra': {'price': 'small', 

               'onsale': 'no',

               'instock': 'yes'}

    }

    ]

    

    

result_list = []    

result = {}

    

for item in products:

    for key,values in item.items():

        if isinstance(values,dict):

            for inner_key, inner_value in values.items():

                #remove me if you want all of the inner items to level-up

                if inner_key == "instock":

                    result[inner_key] = inner_value

        else:

            result[key] = values

            

    result_list.append(result)



print (result_list)

輸出:


[{'color': 'blue', 'shape': 'square', 'instock': 'yes'}, {'color': 'blue', 'shape': 'square', 'instock': 'yes'}]

添加注釋以澄清在哪里修改,以防您也希望其他鍵升級(jí)


查看完整回答
反對(duì) 回復(fù) 2023-08-22
  • 3 回答
  • 0 關(guān)注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)