我在迭代字典的键值和执行数学操作时遇到了困难,其中每个键都有不同的值:fruits = {
"banana": [4,5],
"apple": 2,
"orange":1.5,
"pear": 3
}
我想从代码中得到什么(即我的理想产品):banana [8, 10]
apple 4
orange 3.0
pear 6
我想要多次(*)每个键内的每个整数乘以2。我试过以下几种方法,但我无法正确地做到这一点:for fruit, price in fruits.items():
print(fruit, price*2)
for i in price:
print(fruit, i*2)
然后输出了:banana [4, 5, 4, 5]
banana 8
banana 10
apple 4
我也试过:for fruit, price in fruits.items():
#print(type(price))
if len(fruit)>0:
print(price*2)
elif len(fruit) == 0:
print(price*2)
它向其提供了以下内容:[4, 5, 4, 5]
4
3.0
6