场景重现
data = map(lambda x: x ** 2, [1, 2, 3, 4, 5])
print(len(list(data)))
print(len(list(data)))

如上图所示代码,打印结果分别为5和0。
这是因为map函数处理的结果是迭代器,而且只能迭代一次,如果需要多次使用,需要提前保存。
python3的map使用时需要包在list()中。Wraps map() in a list call. It also changes map(None, x) to list(x). Using from future_builtins import map disables this fixer.
除了map函数之外,python3相较于python2还有其他函数也改为迭代器了,详见Views And Iterators Of Lists
最后
以上就是冷艳黑裤最近收集整理的关于python3内置map后结果是迭代器,只能迭代一次的全部内容,更多相关python3内置map后结果是迭代器内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复