ValueError:太多的值无法用Python中的元组列表进行解包(应为2)



我在用Python创建代码时遇到了这个问题。我传递了一个元组列表,但在拆包时,使用map函数,然后使用列表。我得到这个错误:

值错误:太多的值无法解压缩(应为2(

知道如何克服这个问题吗?我还找不到与元组列表相关的合适答案:-(

这是代码

stock_markets = [('AAPL','200'),('MSFT','780'),('ORCL','650'),('CISC','350')]
def analyze_stocks(stock_markets):
current_max = 0
stock_name = ''
for company,price in stock_markets:
if int(price) > current_max:
current_max = int(price)
stock_name = company
else:
pass
return (stock_name, current_max)
list(map(analyze_stocks,stock_markets))

您已经在用map迭代您的列表了。不需要分析函数中的for循环(因为您已经使用map逐个传递股票(,这是错误的来源。正确的版本应该是这样的:

stock_markets = [('AAPL','200'),('MSFT','780'),('ORCL','650'),('CISC','350')]
def analyze_stocks(stock_markets):
current_max = 0
stock_name = ''
company, price = stock_markets
if int(price) > current_max:
current_max = int(price)
stock_name = company
return (stock_name, current_max)
list(map(analyze_stocks,stock_markets))

相关内容

最新更新