1.Python2中的使用
Python2中的map函数返回一个list,例如:
如果函数有多个参数, 但每个参数的序列元素数量不一样, 会根据最少元素的序列进行:
>>> listx = [1,2,3,4,5,6,7] # 7 个元素
>>> listy = [2,3,4,5,6,7] # 6 个元素
>>> listz = [100,100,100,100] # 4 个元素
>>> list_result = map(lambda x,y,z : x**2 + y + z,listx, listy, listz)
>>> print(list_result)
[103, 107, 113, 121]
明显可以看出是由于 lambda 中的 z 参数,实际是使用了 listz, 而 listz 里面只有 4 个元素, 所以即使 listx 有 7 个元素, listy 有 6 个元素,也不会继续执行了,只执行了 4 个元素的的计算。
2.python3中的使用
map(func, *iterables) --> map object
|
| Make an iterator that computes the function using arguments from
| each of the iterables. Stops when the shortest iterable is exhausted.
Python3中map函数返回的是一个迭代器