我想循环浏览这些词典,以便弄清楚如果你卖掉所有库存食物你会赚多少钱。将该值打印到控制台中!
price = { “香蕉”:4, “苹果”:2, “橙色”:1.5, “梨”:3 }
stock = { “香蕉”:6, “apple”:0, “橙色”:32, “梨”:15 } all_total =
答案 0 :(得分:1)
像这样:
sum(prices[key]*value for key,value in stock.iteritems())
答案 1 :(得分:0)
只要词典具有相同的键,您就可以使用:
all_total = 0
for item in prices:
all_total += prices[item] * stock[item]