python的几个内建函数:apply,filter,map,reduce
来源:互联网 发布:改外网ip地址软件 编辑:程序博客网 时间:2024/05/09 00:54
例如:
>>> filter(lambda x : x%2 == 0,[1,2,3,4,5])
[2, 4]
filter内建函数的python实现:
>>> def filter(bool_func,seq):
filtered_seq = []
for eachItem in seq:
if bool_func(eachItem):
filtered_seq.append(eachItem)
return filtered_seq
2、map(func,seq1[,seq2...]):将函数func作用于给定序列的每个元素,并用一个列表来提供返回值;如果func为None,func表现为身份函数,返回一个含有每个序列中元素集合的n个元组的列表。
例如:
>>> map(lambda x : None,[1,2,3,4])
[None, None, None, None]
>>> map(lambda x : x * 2,[1,2,3,4])
[2, 4, 6, 8]
>>> map(lambda x : x * 2,[1,2,3,4,[5,6,7]])
[2, 4, 6, 8, [5, 6, 7, 5, 6, 7]]
>>> map(lambda x : None,[1,2,3,4])
[None, None, None, None]
map内建函数的python实现:
>>> def map(func,seq):
mapped_seq = []
for eachItem in seq:
mapped_seq.append(func(eachItem))
return mapped_seq
3、reduce(func,seq[,init]):func为二元函数,将func作用于seq序列的元素,每次携带一对(先前的结果以及下一个序列的元素),连续的将现有的结果和下一个值作用在获得的随后的结果上,最后减少我们的序列为一个单一的返回值:如果初始值init给定,第一个比较会是init和第一个序列元素而不是序列的头两个元素。
4、apply(lambda x,y : x*y,(2,3))
>>>6
- python的几个内建函数:apply(),filter(),map(),reduce()
- python的几个内建函数:apply(),filter(),map(),reduce()
- python的几个内建函数:apply(),filter(),map(),reduce(
- python的几个内建函数:apply,filter,map,reduce
- python的几个内建函数:apply(),filter(),map(),reduce()
- Python 几个内建函数: map, reduce,filter,sorted 整理
- Python 内建函数,filter,map, reduce
- python的lambda表达式 内建函数filter map reduce
- python的filter,map,zip,reduce,apply
- filter,map,reduce,apply函数
- Python内建函数之——filter,map,reduce
- Python内建函数之——filter,map,reduce
- Python内建函数之——filter,map,reduce
- Python内建函数之——filter,map,reduce
- Python内建函数之——filter,map,reduce
- Python内建函数之——filter,map,reduce
- Python中reduce、map、filter、apply、tuple函数
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- 一道简单的多线程题
- MVC3下利用Infrastics Jquery Grid+JPaginate 制作Grid
- android 系统 makefile文件(Android.mk)组织结构 .
- Oracle加入NoSQL阵营
- Android开发多线程断点续传下载器
- python的几个内建函数:apply,filter,map,reduce
- 实战WebService I: XML-PRC篇(基于php)
- 简单GUI编程题目
- '用户 'sa' 登录失败。该用户与可信 SQL Server 连接无关联' 解决方案
- 在Apaceh XML-RPC获取客户端的ip
- 关于文件过滤的编程题
- 关于JNDI and EJB
- TCP三次握手/四次挥手详解
- Extreme Programming