How is map() implemented internally in Python?
2016-04-14 01:37
585 查看
Python里面的map函数是怎么实现的?
How is map() implemented internally in Python?
2 AnswersBob Ippolito, CTO at Fig, Mission Bit board member, founder of Mochi Media. Python, JavaScr...1.9k Views • Bob has 6 endorsements in Python (programming language)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(…)).Updated 24 Jul 2015 • View Upvotes
Related Questions
More Answers BelowHow can lazy importing be implemented in Python?How are coroutines implemented in python?
How is set implemented internally in python?
How are dictionaries implemented internally in Python?
Can you implement Monads in Python?
Gurshabad Grover, pseudorandom642 ViewsFrom 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. Iffunction is None, the identity function is assumed; if there are multiple arguments, map() 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]
2. Built-in Functions
Understanding the Map function. pythonWritten 24 Oct 2013 • View Upvotes
FROM: https://www.quora.com/How-is-map-implemented-internally-in-Python
相关文章推荐
- Python语言进行机器学习的开源项目
- 随机验证码生成(python实现)
- python利用wmi修改IP、DNS
- Python爬虫爬验证码实现功能详解
- 十个python程序小技巧
- python 爬虫试手 requests+BeautifulSoup
- python函数
- Python中os及os.path的相关方法
- Python命令Tab自动补全脚本
- Python学习路程day11
- Python简介
- python分析mysql-binlog,统计时间段内各表的操作次数_2016041302
- 几个Python中的基础算法
- 利用Python进行数据分析笔记(一
- python_发送邮件
- Python 爬验证码
- python集合(set)
- Python dict del
- 9.Python笔记之面向对象高级部分
- Python 中enumerate的使用