Python-打印地图对象问题 [英] Python - Printing Map Object Issue

查看:67
本文介绍了Python-打印地图对象问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩地图对象,并发现如果事先执行list(),则该对象不会打印.当我只查看地图时,打印就可以了.为什么?

I was playing with the map object and noticed that it didn't print if I do list() beforehand. When I viewed only the map beforehand, the printing worked. Why?

推荐答案

地图返回一个迭代器,并且您只能使用一次迭代器.

map returns an iterator and you can consume an iterator only once.

示例:

>>> a=map(int,[1,2,3])
>>> a
<map object at 0x1022ceeb8>
>>> list(a)
[1, 2, 3]

>>> next(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

>>> list(a)
[]

另一个示例,我使用第一个元素并创建一个包含其余元素的列表

Another example where I consume the first element and create a list with the rest

>>> a=map(int,[1,2,3])
>>> next(a)
1 
>>> list(a)
[2, 3]

这篇关于Python-打印地图对象问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆