Python的 enumerate zip map reduce filter
来源:互联网 发布:mac版ps cs6序列号 编辑:程序博客网 时间:2024/05/16 10:41
enumerate
功能:遍历一个序列的元素以及其索引号
例子:
>>> list(enumerate('abc'))[(0, 'a'), (1, 'b'), (2, 'c')]>>> >>> for index, element in enumerate('abc'):... print index, element... 0 a1 b2 c
zip
输入:序列组。
输出:一个元组列表, 其中每个元组包含了各个序列中相对位置的一个元素。
例子:
>>> a = 'abc'>>> b = [1,2,3]>>> c = ['a','b','c']>>> >>> zip(a,b,c)[('a', 1, 'a'), ('b', 2, 'b'), ('c', 3, 'c')]>>> >>> for i,j,k in zip(a,b,c):... print i,j,k... a 1 ab 2 bc 3 c
map
参数:一个是函数,一个是序列。
返回:新的list。将传入的函数依次作用到序列的每个元素。
例子:
>>> map(str, [1, 2, 3])['1', '2', '3']
reduce
参数:一个是函数,一个是序列。
返回:
把一个函数作用在一个序列 [x1, x2, x3...]上,这个函数必须接收两个参数,
reduce把结果继续和序列的下一个元素做累积计算:
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
例子:
要把序列[1, 3, 5, 7, 9]变换成整数13579:
>>> def fn(x, y):... return x * 10 + y>>> reduce(fn, [1, 3, 5, 7, 9])13579字符串‘123’,转换成数字 123 的函数 str2int
def char2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]def str2int(s): return reduce(lambda x,y: x*10+y, map(char2num, s))
filter
参数:一个是函数,一个是序列。
返回:过滤后的序列。
把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。
例子:
求100以内的素数def is_ss(n): for i in range(2, n): if n % i == 0: return False return Trueprint filter(is_ss, [x for x in range(1, 100)])
阅读全文
0 0
- Python的 enumerate zip map reduce filter
- python的filter,map,zip,reduce,apply
- map,enumerate,zip,filter
- python的lambda、filter、map、reduce、zip函数
- python常用函数(zip,map,filter,reduce)
- python中map,reduce,filter,zip
- Python 中的 map, reduce, zip, filter, lambda基本使用方法
- [Python] 内置序列函数之 filter, map, reduce, zip
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python函数式编程-map()、zip()、filter()、reduce()、lambda()
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python内置高阶函数map,reduce,filter,sorted,zip
- Python中的map、zip、filter、reduce函数使用
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python中的zip与map和filter与reduce函数的区别
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- mydumper工具介绍
- Python基础-常用内建模块 datetime
- web.xml(WEB-INF)
- H5页面中有关视频的一些问题
- react-native的state
- Python的 enumerate zip map reduce filter
- Vector 中内存分配的问题( resize、reserve )
- Capstone日志2017-12-18
- 7-1 两个有序链表序列的合并(20 分)
- Android系统四大组件
- android 打造真正的下拉刷新上拉加载recyclerview(二):添加删除头尾部
- 杭电acm1232 畅通工程
- Android 安装assets目录下的其他apk
- JavaScript ---- 一些简单使用