python 高阶函数之 map

以例子来理解

用法1:如函数 f(x) = x * x,用python实现如下
>>> def f(x):
...     return x * x
>>> r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> list(r)
[1, 4, 9, 16, 25, 36, 49, 64, 81]
结果:数组个数不变

用法2:
把这个list所有数字转为字符串
>>> list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
['1', '2', '3', '4', '5', '6', '7', '8', '9']

用法3
dict_a = [{'name': 'python', 'points': 10}, {'name': 'java', 'points': 8}]
map(lambda x : x['name'], dict_a) 
输出  ['python', 'java']
用法4
list_a = [1, 2, 3] 
list_b = [10, 20, 30]
map(lambda x, y: x + y, list_a, list_b) 
输出 [11, 22, 33]

  

 

  

原文地址:https://www.cnblogs.com/wdw31210/p/10559952.html