作用于一串数字的python映射函数
我一直在使用Python中的map函数,并希望获得一些帮助来理解以下行为:作用于一串数字的python映射函数,python,string,map,Python,String,Map,我一直在使用Python中的map函数,并希望获得一些帮助来理解以下行为: foo="12345" print map(int,foo) 为您提供[1,2,3,4,5]。显然int(foo)会吐出12345。那么到底发生了什么?既然字符串可以通过字符进行iterable,那么上面的两行是同义的吗 print [int(x) for x in foo] 我知道他们会输出相同的结果,但幕后有什么不同吗?一个比另一个更有效率还是更好?还有一个“蟒蛇”吗 非常感谢 map可以被认为是这样工作的: d
foo="12345"
print map(int,foo)
为您提供[1,2,3,4,5]
。显然int(foo)
会吐出12345
。那么到底发生了什么?既然字符串可以通过字符进行iterable,那么上面的两行是同义的吗
print [int(x) for x in foo]
我知道他们会输出相同的结果,但幕后有什么不同吗?一个比另一个更有效率还是更好?还有一个“蟒蛇”吗
非常感谢
map
可以被认为是这样工作的:
def map(func, iterable):
answer = []
for elem in iterable:
answer.append(func(elem))
return answer
>>> int('Hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Hello'
>>> def silly(x):
... pass
...
>>> map(silly,'Hello')
[None, None, None, None, None]
def verbose_map(some_function,something):
results = []
for i in something:
results.append(some_function(i))
return results
基本上,它返回一个列表L
,使得L
的第i个元素是对iterable的第i个元素计算func
的结果
因此,对于int
和一个int
s字符串,在for循环的每次迭代中,元素是一个特定的字符,当赋予int
时,它会作为实际的int
返回。对此类字符串调用map
的结果是一个列表,其元素对应于字符串中相应字符的int
ed值
所以是的,如果L=“12345”
,那么map(int,L)
就是[int(x)表示L中的x]
希望这能有所帮助是的,幕后有巨大的不同。如果你
打印(地图)
你会看到它是一个内置的。内置函数的执行速度比用python编写的函数快,或者比大多数基于语言解析方式的函数快,map使用fast iter方法,列表理解则不使用。其他人认为没有区别
map(int, '1'*1000000)
vs
使用CPython和unix时间程序,map在3秒内完成,列表理解在5秒内完成
哦,有一点需要注意,这只适用于传递给map的函数是用C编写的
将函数应用于iterable的每个项,并返回
结果
从
int()
def map(func, iterable):
answer = []
for elem in iterable:
answer.append(func(elem))
return answer
>>> int('Hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Hello'
>>> def silly(x):
... pass
...
>>> map(silly,'Hello')
[None, None, None, None, None]
def verbose_map(some_function,something):
results = []
for i in something:
results.append(some_function(i))
return results
这是一种短而有效的方法,可以做这样的事情:
def map(func, iterable):
answer = []
for elem in iterable:
answer.append(func(elem))
return answer
>>> int('Hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Hello'
>>> def silly(x):
... pass
...
>>> map(silly,'Hello')
[None, None, None, None, None]
def verbose_map(some_function,something):
results = []
for i in something:
results.append(some_function(i))
return results
map()
在某些情况下可能比使用列表理解快一些,在某些情况下,map比列表理解慢一些
使用内置功能时:
python -mtimeit -s'xs=xrange(1000)' 'map(int,"1234567890")'
10000 loops, best of 3: 18.3 usec per loop
python -mtimeit -s'xs=xrange(1000)' '[int(x) for x in "1234567890"]'
100000 loops, best of 3: 20 usec per loop
使用lambda
,map()
会变慢:
python -mtimeit -s'xs=xrange(1000)' '[x*10 for x in "1234567890"]'
100000 loops, best of 3: 6.11 usec per loop
python -mtimeit -s'xs=xrange(1000)' 'map(lambda x:x*10,"1234567890")'
100000 loops, best of 3: 11.2 usec per loop
但是,在python中,3xmap()
返回一个map对象,即迭代器“更高效”是一个蠕虫的罐头。在这台计算机上,将map与CPython一起使用会更快,但是pypy的列表理解速度更快
foo="12345"
In [507]: dis.dis('map(int,foo)')
0 <109> 28769
3 STORE_SLICE+0
4 LOAD_ATTR 29806 (29806)
7 <44>
8 BUILD_TUPLE 28527
11 STORE_SLICE+1
def map(func, iterable):
answer = []
for elem in iterable:
answer.append(func(elem))
return answer
dis.dis('map(int,foo)')
0 <109> 28769
3 STORE_SLICE+0
4 LOAD_ATTR 29806 (29806)
7 <44>
8 BUILD_TUPLE 28527
11 STORE_SLICE+1
dis.dis('[int(x) for x in foo]')
0 DELETE_NAME 28265 (28265)
3 LOAD_GLOBAL 30760 (30760)
6 STORE_SLICE+1
7 SLICE+2
8 BUILD_TUPLE 29295
11 SLICE+2
12 SETUP_LOOP 26912 (to 26927)
15 JUMP_FORWARD 26144 (to 26162)
18 JUMP_IF_FALSE 23919 (to 23940)
$ python -mtimeit 'map(int,"1234567890")'
100000 loops, best of 3: 8.05 usec per loop
$ python -mtimeit '[int(x) for x in "1234567890"]'
100000 loops, best of 3: 9.33 usec per loop
$ pypy -mtimeit 'map(int,"1234567890")'
1000000 loops, best of 3: 1.18 usec per loop
$ pypy -mtimeit '[int(x) for x in "1234567890"]'
1000000 loops, best of 3: 0.938 usec per loop
Python3显示map()
更快,即使需要额外调用list()
$ python3 -mtimeit 'list(map(int,"1234567890"))'
100000 loops, best of 3: 11.8 usec per loop
$ python3 -mtimeit '[int(x) for x in "1234567890"]'
100000 loops, best of 3: 13.6 usec per loop
这属于意见范围。观点:它们在功能上相似,这是用户的偏好<代码>地图
时钟快一点;列表理解对于第三方来说更为明显。我指的是列表理解,map反复调用int,因此在这两种情况下都存在一个,区别在于传递给map的iterable是通过cpython快速循环函数传递的,而另一个是通过较慢的方法进行解析的。在我的机器上用[int(i)for i in s]在长度为10e7的字符串上迭代大约需要5秒,map(int,s)需要3秒。你应该澄清你的答案。当然,这样更好吗?我正试图找出如何更好地解释它,最好不要在C源代码中翻找。你似乎有点困惑<代码>映射在这种情况下并不是更快,因为map是一个内置的,这是因为int是一个内置的。对于最新版本的Python来说,性能提升非常小,因此不值得单独使用map