Python 如何为列表中的每个项目循环函数?
我需要从一个已经导入的函数中找到一些东西的价格 因此,假设导入文件stock.py中的函数是:Python 如何为列表中的每个项目循环函数?,python,Python,我需要从一个已经导入的函数中找到一些东西的价格 因此,假设导入文件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)
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()只需要一种商品,比如“可口可乐”。您需要遍历stock.stock\u list()中的每个项目,对于其中的每个项目,调用stock\u price()
你能帮个忙吗我想你需要这个
item = stock.stock_list(location_name)
"""eg; bread, rice, coke"""
for x in sorted(item):
print stock.stock_price(x)
或
对于已排序(项目)中的x:打印(stock.stock\u price(x))
谢谢你,你这个天才:)
item = stock.stock_list(location_name)
complete_list = [stock.stock_price(x) for x in sorted(item)]