python中的map方法
2018-02-28 22:03
253 查看
map函数接受两个参数,一个是函数,一个是序列,map对items中的每个元素依次执行function,并把结果作为新的list返回
语法:
map(function_to_apply, list_of_inputs)
返回一个列表中数字的平方,通常我们会这么做
运行结果:
如果我们使用Map方法呢
运行结果:
map()除了接受一个列表,还可以接受一个列表的函数
运行结果:
语法:
map(function_to_apply, list_of_inputs)
返回一个列表中数字的平方,通常我们会这么做
items = [1,2,3,4,5] squared = [] for i in items: squared.append(i**2) print squared
运行结果:
[1, 4, 9, 16, 25]
如果我们使用Map方法呢
items = [1,2,3,4,5] lamb'd x:x**2为一个表达式,接受一个参数x并返回x的平方 squared=map(lamdba x:x**2, items)
运行结果:
[1, 4, 9, 16, 25]
map()除了接受一个列表,还可以接受一个列表的函数
def multiply(x): return x*x def add(x): return x+x func = [multiply, add] for i in range(10): value = map(lambda x:x(i), func) print func
运行结果:
[0, 0] [1, 2] [4, 4] [9, 6] [16, 8] [25, 10] [36, 12] [49, 14] [64, 16] [81, 18]
相关文章推荐
- Python中map,reduce,filter和sorted函数的使用方法
- Pool的四种方法比较:Python multiprocessing.Pool: Difference between map, apply, map_async, apply_async
- python map方法
- Python 装逼手机号码方法 低配版 map方法解析
- [python]map方法与并行执行
- Python中的map(function,iterable, ...)方法
- 那些Python方法---map()
- Pythont特殊语法filter,map,reduce,apply使用方法
- python中map、filter、reduce 函数使用方法
- 决策树c4.5(python3字典各种方法,map(),集合set()等,python树节点)
- python中map多进程使用方法
- Python 中 map,reduce,和filter 内置函数的方法
- python3中内建函数map()与reduce()的使用方法
- 记录一个python公式罗列的方法 join()方法和map()方法的妙用
- python 中的filter, map, reduce方法解释
- Python Map()方法的用法
- Python 中的 map, reduce, zip, filter, lambda基本使用方法
- python用reduce和map把字符串转为数字的方法
- Python:map()的使用方法
- python用reduce和map把字符串转为数字的方法