小编Diz*_*ral的帖子

地图功能返回的列表在一次使用后消失

我是Python的新手.我正在使用Python 3.3.2并且我很难弄清楚为什么以下代码:

strList = ['1','2','3']
intList = map(int,strList)
largest = max(intList)
smallest = min(intList)
Run Code Online (Sandbox Code Playgroud)

给我这个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: min() arg is an empty sequence
Run Code Online (Sandbox Code Playgroud)

但是这段代码:

strList = ['1','2','3']
intList = list(map(int,strList))
largest = max(intList)
smallest = min(intList)
Run Code Online (Sandbox Code Playgroud)

完全没有错误.

我的想法是,当intList被赋值给map函数的返回值时,它根据文档变成迭代器而不是列表.也许作为调用的副作用max(),迭代器已经迭代到列表的末尾,导致Python认为列表是空的(我在这里从C知识中提取,我不熟悉迭代器的真正工作方式) Python.)我必须支持的唯一证据是,对于第一个代码块:

>>> type(intList)
<class 'map'>
Run Code Online (Sandbox Code Playgroud)

而对于第二个代码块:

>>> type(intList)
<class 'list'>
Run Code Online (Sandbox Code Playgroud)

有人可以帮我确认一下吗?

python string dictionary list python-3.x

19
推荐指数
2
解决办法
2911
查看次数

标签 统计

dictionary ×1

list ×1

python ×1

python-3.x ×1

string ×1