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

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

如何在Python中將字典列表中的鍵值向上移動一級

如何在Python中將字典列表中的鍵值向上移動一級

慕桂英3389331 2023-08-22 10:26:51
使用Python3,我試圖將字典列表中的鍵值對向上移動。我有一個名為 Product 的變量,其中包含以下內容:    [     {'color': 'red',      'shape': 'round',      'extra': {'price': 'large',                'onsale': 'yes',               'instock: 'yes'}    },     {'color': 'blue',      'shape': 'square',      'extra': {'price': 'small',                'onsale': 'no',               'instock: 'yes'}    }    ]我想將 extra 內的“instock”鍵值對移至上一級,以與顏色、形狀、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"]})這對我不起作用。我可以在網上找到的任何幫助或其他文獻將不勝感激!
查看完整描述

3 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

非常簡單:遍歷列表。對于每個字典,將“extra”.“instock”復制到上一層并刪除原始的:


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'}



查看完整回答
反對 回復 2023-08-22
?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

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為默認值)no


查看完整回答
反對 回復 2023-08-22
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

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'}]

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


查看完整回答
反對 回復 2023-08-22
  • 3 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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