我有一本嵌套字典
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)
对于cust_order,cust_details在customer_order.items():print(“\n nOrder:",cust_order)中表示key,value in cust_details: if (key == "sizecost"):+=值
if (key == "eyeletcost"):
totalcosts += value
if (key == "materialcost"):
totalcosts += value
if (key == "ropecost"):
totalcosts += value
if (key == "wordcost"):
totalcosts += value总成本+=值
发布于 2019-10-20 05:56:07
您可以使用recurssion
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", customer_order )
https://stackoverflow.com/questions/58470272
复制相似问题