2015-03-25 72 views
-3

我需要從已導入的函數中找到某物的價格。如何遍歷列表中每個項目的函數?

因此,可以說,從被稱爲stock.py導入文件的功能是:

def stock_price(item): 
""" Returns the current price of an item """ 
return _ALL_PRICES[item] 

_ALL_PRICES [項目]是一個列表全名和價格

這裏是一個例子:

「賴斯:14.55,

'可樂':1.55,

項功能,

item = stock.stock_list(location_name) 
"""eg; bread, rice, coke""" 
for x in sorted(item): 
    print (x) 

和我需要的功能,這將使我的列表中的所有東西的價格。

但這樣做不起作用。

price = stock.stock_price(item) 
for x in price: 
    print (x) 

我被告知,stock_price()期望單個項目,比如'coke'。您需要循環訪問stock.stock_list()中的每個項目,併爲每個項目調用stock_price()。

能否請您用這個:(幫助

+1

'在排序(項目)X:打印(stock.stock_price (x))' – deceze 2015-03-25 07:56:29

+0

謝謝你,你天才:) – Tribetter 2015-03-25 08:03:25

回答

0

我認爲你需要這個

item = stock.stock_list(location_name) 
"""eg; bread, rice, coke""" 
for x in sorted(item): 
    print stock.stock_price(x) 

OR

item = stock.stock_list(location_name) 
complete_list = [stock.stock_price(x) for x in sorted(item)] 
相關問題