python3-cookbook icon indicating copy to clipboard operation
python3-cookbook copied to clipboard

1.8节关于zip()函数的说明有误

Open lszxw1234 opened this issue 6 years ago • 1 comments

原文指出:执行这些计算的时候,需要注意的是 zip() 函数创建的是一个只能访问一次的迭代器。 比如,下面的代码就会产生错误:

prices_and_names = zip(prices.values(), prices.keys()) print(min(prices_and_names)) # OK print(max(prices_and_names)) # ValueError: max() arg is an empty sequence

在Python3.5的环境中运行,发现prices_and_names的类型是list。可以运行多次。

lszxw1234 avatar Jun 06 '18 02:06 lszxw1234

在命令行运行Python确定你的环境是否正确,Python3中zip返回的是迭代器了。我测试没有问题,print(max(prices_and_names)) 会报错序列为空,因为迭代器里的元素被前一个语句消耗光了

yim7 avatar Sep 28 '18 05:09 yim7