python的 map,filter, reduce, enumerate
一, map #基本的map运用都可以用解析去替代,复杂的仍然需要定义函数,利用map去做
map(函数, 序列) 将序列的各项经过函数处理, 然后返回到一个新列表中。 #itertools.imap()
>>> s
['a', 'b', 'c', 'd']
>>> exp1 = map(ord, s) #s 也可以是字符串, 元组, 字典
>>> exp1
[97, 98, 99, 100]
序列的个数根据前面的函数而定, ord()一次接受一个,所以后面只有一个序列。
>>> add = lambda x, y: x+y
>>> map(add, {'a':'djsfld', 'b': 'dgfasg'}, 'fg') #自定义了一个add函数,接受两个参数,所以后面是两个可迭代的对象。
['af', 'bg']
注意,后面的两个对象长度必须一致, map不具有不短和截长的功能。
而zip会将长的多余部分给截去, 如:
>>> zip( {'a':'djsfld', 'b': 'dgfasg'}, 'fgf')
[('a', 'f'), ('b', 'g')] #可以一次循环多个对象,itertools.izip()
在python3* 中,返回的不是列表,而是可迭代对象,可以通过list()函数得到以上结果。
>>> def xixi(x,y):
... if x>y: return x+y
... else: return y
...
>>> map(xixi, 'abcdefg', ['e','a','c','b','f','t','j'])
['e', 'ba', 'c', 'db', 'f', 't', 'j']
二, filter #基本的filter运用都可以用解析去替代, 复杂的仍然要自定义函数,利用filter去做,这也许是map和filter依然存在的原因。
filter(过滤函数, 列表) 将列表中的元素经过函数,如果返回True, 就放到一个新的列表中,如果返回False, 就不放进去。#itertools.dropwhile
filter只能一次处理一个元素。所以后面只有一个序列。
>>> def hehe(x):
... if ord(x)>100:return True
... else: return False
...
>>> filter(hehe, 'av')
'v'
>>> filter((lambda x:x>0), range(-5,5))
[1, 2, 3, 4]
在python3*中返回的不直接是列表,而是一个迭代器。可以用list()查看结果。
三, reduce
对每队元素都应用函数并运行到最后结果。
>>> reduce((lambda x, y: x+y),range(-5,5))
-5
算法是这样的:(((((((((-5+-4)+-3)+ -2)+ -1)+ 0)+ 1)+ 2)+ 3)+ 4) = -5
四, enumerate
s='SAASDFASDF\n'
>>> for i,j in enumerate(s.strip()): 同时返回这个元素以及元素的index
... print i,j
...
0 S
1 A
2 A
3 S
4 D
5 F
6 A
7 S
8 D
9 F
注意:返回的index是整型!!!!!!!!
by freemao
FAFU
free_mao@qq.com
python的 map,filter, reduce, enumerate的更多相关文章
- Python map filter reduce enumerate zip 的用法
map map(func, list) 把list中的数字,一个一个运用到func中,常和lambda一起用. nums = [1, 2, 3, 4, 5] [*map(lambda x: x**2, ...
- Python中map,filter,reduce,zip的应用
事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', ...
- Python【map、reduce、filter】内置函数使用说明(转载)
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- 【转】Python 中map、reduce、filter函数
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- python常用函数进阶(2)之map,filter,reduce,zip
Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...
- Python【map、reduce、filter】内置函数使用说明
题记 介绍下Python 中 map,reduce,和filter 内置函数的方法 一:map map(...) map(function, sequence[, sequence, ...]) -& ...
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- 如何在python3.3用 map filter reduce
在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0 ...
- Swift map filter reduce 使用指南
转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...
- 数组的高阶方法map filter reduce的使用
数组中常用的高阶方法: foreach map filter reduce some every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...
随机推荐
- bzoj 2132: 圈地计划
#include<cstdio> #include<iostream> #include<cstring> #define M 100009 #define inf ...
- treap codevs 4543普通平衡树
#include<cstdio>#include<ctime>#include<cstdlib>struct shu{ int l,r,sum1,zhi,dui,s ...
- POJ 1328 Radar Installation 贪心 难度:1
http://poj.org/problem?id=1328 思路: 1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解 2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆 ...
- TaskTracker启动过程源码级分析
TaskTracker也是作为一个单独的JVM来运行的,其main函数就是TaskTracker的入口函数,当运行start-all.sh时,脚本就是通过SSH运行该函数来启动TaskTracker的 ...
- SQL Server 自定义字符串分割函数
一.按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果(标量值函数) create function Func_StrArrayL ...
- eclipse 提交作业到JobTracker Hadoop的数据类型要求必须实现Writable接口
问:在eclipse中的写的代码如何提交作业到JobTracker中的哪?答:(1)在eclipse中调用的job.waitForCompletion(true)实际上执行如下方法 connect() ...
- vmware 下的linux的host only上网配置
1.首先在Vm中将网络设置为Host-only. 2.在windows下,打开网络邻居,会见到如下界面,其中负责联网的是本地连接,Vm1是host-only连接,VM2是Nat连接方式,首先将VM1. ...
- poj1179
//Accepted 244 KB 0 ms //区间dp //石子合并模型 #include <cstdio> #include <cstring> #include < ...
- matlab的正则表达式讲解[转]
引言.啥是正则表达式?正则表达式是干啥的?我理解就和我们在word或者其他编辑软件里点的查找.替换的作用是差不多的,不过功能要强大的多,当然使用起来也稍微复杂一些.书上的定义差不多是这样的:正则表达式 ...
- RTL2832U+R820电视棒跟踪飞机轨迹教程(ADS-B)
Ubuntu 14.04.3 amd64 apt-get install git apt--dev 安装rtl-sdr git clone git://git.osmocom.org/rtl-sdr. ...