使用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"]})
这对我不起作用。
我可以在网上找到的任何帮助或其他文献将不胜感激!
撒科打诨
aluckdog
qq_花开花谢_0
相关分类