博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How is map() implemented internally in Python?
阅读量:4178 次
发布时间:2019-05-26

本文共 2448 字,大约阅读时间需要 8 分钟。

Python里面的map函数是怎么实现的?

How is map() implemented internally in Python?

2 Answers
CTO at Fig, Mission Bit board member, founder of Mochi Media. Python, JavaScr...
1.9k Views
 •   has   in 
The implementation differs greatly depending on which Python you're talking about. In Python 2.x it's written in C but the code is roughly equivalent to the following:
from itertools import izip 
def map(f, lst, *lsts):
result = []
for args in izip(lst, *lsts):
result.append(f(*args))
return result
In Python 3.x the implementation is also written in C, but it returns an iterable instead of a list, so the work doesn't happen eagerly. It is equivalent to the following (since zip is also lazy in Python 3, this is a pretty good approximation of what goes on under the hood):
def map(f, lst, *lsts): 
for args in zip(lst, *lsts):
yield f(*args)
To get the Python 3 behavior in Python 2, you can use 
itertools.imap. To get Python 2 behavior in Python 3, you could use 
list(map(…)).
pseudorandom
642 Views
From the official Python documentation:
map(function, iterable, ...)
Apply 
function to every item of 
iterable and return a list of the results.
If a function definition makes anything clear, it would look something like this:
def map(my_function, my_list): 
result = []
for i in my_list:
result.append(my_function(i))
return result
But this is just simplifying since the map() function can process more than one iterable.
If additional 
iterable arguments are passed, 
function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with None items. If
function is None, the identity function is assumed; if there are multiple arguments, 
 returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The 
iterable arguments may be a sequence or any iterable object; the result is always a list.
I'd just like to add that using the map() function seems obsolete because list comprehensions do the same work in a more comprehensible (ha!) and faster manner.
my_list = [ my_function(x) for x in my_list]

转载地址:http://vktai.baihongyu.com/

你可能感兴趣的文章
为小米4与小米3 Mi3 Mi4编译Cyanogenmod 12.1与13.0 (CM12与CM13) 的步骤以及错误解决
查看>>
原生Android系统的第一次开机google验证的解决
查看>>
S5P4418与S5P6618的Android boot.img的解压与压缩, Sparse ext4文件系统
查看>>
【EVB-335X-II试用体验】 u-boot与kernel的编译以及本地repo的建立
查看>>
【EVB-335X-II试用体验】 上手试用与资源使用
查看>>
【EVB-335X-II试用体验】 Yocto环境的建立及Rootfs的构建与使用
查看>>
<<C++程序设计原理与实践>>粗读--chapter0 chapter1 chapter2
查看>>
<<C++程序设计原理与实践>>粗读--chapter3 chapter4 Chapter5
查看>>
<<C++程序设计原理与实践>>粗读 -- chapter8 Chapter9
查看>>
Linux Qt程序打包成一个可执行文件
查看>>
DragonBoard 410C中的Fastboot与调试串口注意事项
查看>>
跨系统的录音格式兼容性问题: iOS Android
查看>>
JVM 的垃圾回收器
查看>>
Mybatis的缓存
查看>>
@validated注解异常返回JSON值
查看>>
@JsonFormat注解使用
查看>>
Spring boot集成jxls实现导入功能
查看>>
Spring boot读取配置的方式(指定配置文件)
查看>>
Spring Boot切换不同环境配置
查看>>
Spring cloud之Ribbon搭建
查看>>