python3中map函数的改动

来源:互联网 发布:爆破数据库密码 编辑:程序博客网 时间:2024/06/09 19:04

其实在What's new in python3 中都写了。

map() and filter() return iterators. If you really need a list and the input sequences are all of equal length, a quick fix is to wrap map() in list(), e.g. list(map(...)), but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (since creating a list would just be wasteful).


大意就是,不会返回list而是返回iterator了。这样可以懒惰计算balabala。可以list(map()..)来解决,不过推荐使用列表解析,或者不用列表。

0 0
原创粉丝点击