热线电话:13121318867

登录
2020-05-06 阅读量: 695
为什么 python3 里的 map 对象读取时需要先 list

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]`

py3 的 map 返回的是生成器,节省内存

一边循环一边计算的机制,称为生成器(Generator)

15.7570
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子