1

I am a beginner in Python and am struggling on retrieving an element out of a tuple from a list. What I am trying to do is to get the value of a fruit and multiply it by the quantity needed. An example below will show you what I mean. I can not figure out how to get the second element in the tuple.

##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

It is mostly in my else statement that I can not get it working. All help is greatly appreciated!

4

4 回答 4

2

Why are you looping over the dictionary? Loop over your list instead, and add to totalCost accordingly.

for fruit, n in orderList:
    if fruit in fruitPrices:
        totalCost += fruitPrices[fruit] * n
    else:
        print fruit, 'not here!'

You can simplify all this and do something like

sum(fruitPrices.get(fruit, 0) * n for fruit, n in orderList)

Note that fruitPrices.get(fruit, 0) will return fruitPrices[fruit] if fruit is in fruitPrices and 0 otherwise.

于 2013-01-10T03:41:58.170 回答
0

Could get this down to one line, but I dont think it will help. You are looping over the prices dictionary, but should be looping over the orderList, and then looking up the fruit in the dictionary.

def buyLotsOfFruit(orderList):
    totalCost = 0.0 
    for fruit, quantity in orderList:
       if fruit not in fruitPrices:
           print 'not here!'
       else:
           totalCost = totalCost +fruitPrices[fruit]* quantiy            
    return totalCost
于 2013-01-10T03:40:23.200 回答
0
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            
            totalCost = totalCost +fruitPrices[fruit] * price

    return totalCost
于 2013-01-10T03:41:06.960 回答
0

NOTE:

You can put this entire function into a one-liner like this:

buyLotsOfFruit = lambda ol: sum(fruitPrices[f] * p for f, p in ol if f in fruitPrices)

Or this other way:

def buyLotsOfFruit(orderList):
    ##    orderList: List of (fruit, numPounds) tuples        
    ## Returns cost of order

    totalCost = 0.0 
    for fruit, pounds in orderList:
        if fruit not in fruitPrices:
            print 'not here!'
        else:
            totalCost += fruitPrices[fruit] * pounds
    return totalCost

To retrieve a key from a dictionary all you need is this: dictionary[key] and it returns the value

于 2013-01-10T03:43:11.583 回答