V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
xyok
V2EX  ›  Python

为什么 python3 里的 map 对象读取时需要先 list,这样设计有什么好处

  •  
  •   xyok · 2015-10-04 17:56:40 +08:00 · 2384 次点击
    这是一个创建于 3134 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    a= [1,2,2,3]
    m = map(lambda x:x**2,a)
    print m
    >>>[1, 4, 4, 9]


    Python 3.5.0 (default, Oct 3 2015, 07:03:10)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    a= [1,2,2,3]
    m = map(lambda x:x**2,a)
    print(m)
    >>><map object at 0x7f25b7ebce10>
    list(m)
    >>>[1, 4, 4, 9]

    4 条回复    2015-10-04 18:37:09 +08:00
    SErHo
        1
    SErHo  
       2015-10-04 18:03:04 +08:00
    range 和 xrange 的区别。
    raptor
        2
    raptor  
       2015-10-04 18:28:52 +08:00
    py3 的 map 返回的是迭代器。
    myjiayan
        3
    myjiayan  
       2015-10-04 18:29:31 +08:00
    生成器,节省内存
    WhiteSaber
        4
    WhiteSaber  
       2015-10-04 18:37:09 +08:00
    @myjiayan 感谢你的解答,生成器真是一个好东西
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2377 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 108ms · UTC 09:36 · PVG 17:36 · LAX 02:36 · JFK 05:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.