Python 3.x 在嵌套字典中查找特定键的值
我有一本嵌套字典Python 3.x 在嵌套字典中查找特定键的值,python-3.x,dictionary,nested,Python 3.x,Dictionary,Nested,我有一本嵌套字典 customer_order = {order0 {'Orientation': what_orientation, 'Size': what_size, 'sizecost': size_cost, 'eyelets': how_many_eyelets, 'eyeletcost': eyelet_cost, 'material': what_material, 'materialcost': material_cost, 'ropes': need_ropes,
customer_order = {order0
{'Orientation': what_orientation, 'Size': what_size, 'sizecost': size_cost,
'eyelets': how_many_eyelets, 'eyeletcost': eyelet_cost, 'material': what_material,
'materialcost': material_cost, 'ropes': need_ropes, 'ropecost': rope_cost,
'image': need_image, 'imagecost': 0, 'wording': what_wording, 'wordcost':word_cost}
order1{'Orientation': what_orientation, 'Size': what_size, 'sizecost': size_cost,
'eyelets': how_many_eyelets, 'eyeletcost': eyelet_cost, 'material': what_material,
'materialcost': material_cost, 'ropes': need_ropes, 'ropecost': rope_cost,
'image': need_image, 'imagecost': 0, 'wording': what_wording, 'wordcost':word_cost}}
我需要做的是获取以下键的值
sizecost
eyeletcost
materialcost
ropecost
wordcost
如何循环获取这些值并将其添加到运行总数中
谢谢
我尝试了下面的代码,但得到了错误
for key, value in cust_details:
ValueError:要解压缩的值太多(应为2个)
对于客户订单,客户订单中的客户详细信息。项目()
打印(“\nOrder:”,客户订单)
对于键,客户详细信息中的值:
如果(键==“sizecost”):
总成本+=价值
if (key == "eyeletcost"):
totalcosts += value
if (key == "materialcost"):
totalcosts += value
if (key == "ropecost"):
totalcosts += value
if (key == "wordcost"):
totalcosts += value
totalcost+=值您可以使用
递归
def look(key,d,val = None):
if val is None:
val = []
if key in d.keys():
val.append(d.get(key))
else:
for i,j in d.items():
if isinstance(j,dict):
look(key,j,val)
return val
现在尝试调用:
look(“sizecost”,客户订单)
首先,您的字典不是有效的python对象。你能让它成为一个有效的python对象吗?谢谢你把它放好。我已经把它正确地放入了模板中,但是当我发布它时,它的效果相当糟糕。