python怎样把两个dictionary中的value相乘并相加?

python怎样把两个dictionary中的value相乘并相加?
如题,要把所有的价格和数量相乘再相加得到总价,然后再放到total中,最后面for那一段不太会写,(如果有错还望指出,
prices = {
"banana" :4,
"apple" :2,
"orange" :1.5,
"pear" :3,
}
stock = {
"banana" :6,
"apple" :0,
"orange" :32,
"pear" :15,
}
for key in prices:
print key
print "price:%s" % prices[key]
print "stock:%s" % stock[key]
total=0
for value in prices:
看不下去了9 1年前 已收到1个回答 举报

我跑我跑-扑通 花朵

共回答了19个问题采纳率:89.5% 举报

total = sum([prices[fruit] * stock[fruit] for fruit in prices]) 是这样吗

1年前

4
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.060 s. - webmaster@yulucn.com