我是一名Python初学者,正在努力从列表中检索元组。我想要做的是获得一个水果的价值,并乘以所需的数量。下面的例子会告诉你我的意思。我无法弄清楚如何获取元组中的第二个元素。Python - 从列表中检索元组中的元素
##Cost of [('apples', 2.0), ('pears', 3.0), ('limes', 4.0)] is 12.25
fruitPrices = {'apples':2.00, 'oranges': 1.50, 'pears': 1.75,'limes':0.75,
'strawberries':1.00}
def buyLotsOfFruit(orderList):
## orderList: List of (fruit, numPounds) tuples
## Returns cost of order
totalCost = 0.0
for fruit,price in fruitPrices.items():
if fruit not in fruitPrices:
print 'not here!'
else:
totalCost = totalCost +fruitPrices[fruitPrices.index(fruit)].key() * price
return totalCost
这是主要在我的其他声明,我不能得到它的工作。所有帮助非常感谢!
'buyLotsOfFruit'的缩进是正确的? –
也许,对不起,这是我把它粘贴到这里的时候 –