map()、filter()和reduce()

map()、filter()和reduce()是python提供的几个常用的函数式编程接口,性能好,直接由C语言写的,运行时不需要通过python解释器间接调用,并且内部做了诸多优化。

map()
map(function, iterable)函数,它表示,对iterable中的每个元素,都运用function这个函数,最后返回一个新的可遍历的集合:
l = [1, 2, 3, 4, 5]
new_list = map(lambda x: x * 2, l) # [2, 4, 6, 8, 10]
python3 -mtimeit -s'xs=range(1000000)' 'map(lambda x: x*2, xs)'
2000000 loops, best of 5: 171 nsec per loop

python3 -mtimeit -s'xs=range(1000000)' '[x * 2 for x in xs]'
5 loops, best of 5: 62.9 msec per loop

python3 -mti

map()、filter()和reduce()最先出现在Python成神之路

版权声明:
作者:zhangchen
链接:https://www.techfm.club/p/19668.html
来源:TechFM
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>