推荐学习书目
› 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 · Oct 4, 2015 · 3349 views
    This topic created in 4023 days ago, the information mentioned may be changed or developed.

    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 replies  •  2015-10-04 18:37:09 +08:00
    SErHo
        1
    SErHo  
       Oct 4, 2015
    range 和 xrange 的区别。
    raptor
        2
    raptor  
       Oct 4, 2015
    py3 的 map 返回的是迭代器。
    myjiayan
        3
    myjiayan  
       Oct 4, 2015
    生成器,节省内存
    WhiteSaber
        4
    WhiteSaber  
       Oct 4, 2015
    @myjiayan 感谢你的解答,生成器真是一个好东西
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5812 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 01:39 · PVG 09:39 · LAX 18:39 · JFK 21:39
    ♥ Do have faith in what you're doing.