python3中map函数的改动

本文讨论了Python 3中map和filter函数的行为变化,不再直接返回列表而是返回迭代器,这有助于节省内存并实现懒加载。文章推荐使用列表解析或重新考虑是否真的需要列表,以更高效地进行编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实在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()..)来解决,不过推荐使用列表解析,或者不用列表。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值