Python中reduce、map、filter、apply、tuple函数
来源:互联网 发布:淘宝助理很卡 编辑:程序博客网 时间:2024/05/22 02:10
#_*_coding:UTF-8_*_# tuple函数def operat (x,y,z): x=x+5 y=y+5 z=z+5 oper = [x,y,z] #使用oper=[x,y,x]将这三个参数“打包”到一个名为oper的列表中 numbers = tuple(oper) #用tuple()函数将列表装到元组中 return numbersx,y,z=operat(1,2,3)print x,y,z# apply(function_name [, args [, kwargs]])# 函数可以实现调用可变参数列表的功能,把函数的参数存放到一个元组或序列中,元组中的参数是有序的。# function_name所表示的函数的返回值就是apply函数的返回值# functon_name:该参数是必须的,表示自定义函数的名称# args:该参数是可选的他是一个包含了自定义函数额列表和元组。如果不指定该参数,则表示被执行的函数没有任何参数# kwargs:该参数是可选的,它是一个字典类型的参数,字典中key的值为函数的参数的名称,value值为实际参数的值。# 定义登录函数login(),该函数有两个参数,分别为username和passworddef login(username,password): msg='' # 记录返回结果的字符串 if(username=='admin') and (password=='admin'): #验证用户名和密码 msg='登录成功' else: msg='登录失败' return msg # 返回登录信息#使用apply()函数实现调用可变参数列表print apply(login,('admin','admin')) #登录成功# filter(function_name,sequence)# filter函数可以对序列做过滤处理,简单的说就是用函数来过滤一个序列,把序列的每一项传递到过滤函数。# 对自定义函数的参数返回的结果是否为True做过滤,并一次性的返回处理结果。# 如果过滤函数返回的结果为Fasle,那么该元素将从列表中删除该项。# function_name:该参数是必须的,他是自定义函数,在函数function_name()中定义过滤的规则。如果function_name()函数的返回值为None#则表示sequence序列中的一项都为True,从而返回左右的序列元素。# sequence:该参数也还是必须的。表示需要过滤的序列。# 定义 验证用户名的函数dalidate(),长度在4-12之间def validate(usernames): if(len(usernames) > 4) and (len(usernames) < 12): return usernames# 调用filter(),过滤validate函数中元组参数print filter(validate,('admin','maxianglin','mxl','adm','wanglili')) #输出结果为元组('admin', 'maxianglin', 'wanglili')# reduce(function_name,sequence[, initial])# reduce可以实现连续处理功能# function_name:该参数是必须的,他是自定义函数,在函数function_name()中实现对参数sequence的连续操作。# sequence:该参数也是必须的,表示待处理的序列。# initial:该参数是可选的,如果指定了该参数的值,则initial所指定的值将首先被传入fucntion_name函数中进行计算。如果sequence参数的值为空,则对initial所指定的值进行处理。# 定义计算两个数相乘的函数operat()def operat(x,y): return x*y# 使用reduce()函数,对元组的每一项进行计算,最后将计算的结果返回print reduce(operat,(1,2,3,4,5,6)) #720(1×2×3×4×5×6)print reduce(operat,(7,8,9),5) #2520(5×7×8×9)5为第三个参数,所以5被指定# map(function_name,sequence[,sequence,...])# function_name:该参数是必须的,他是自定义函数的名称,实现对序列中每一个元素的操作。sequence参数表示待处理的序列,参数sequence的个数可以是多个。# 如果传给map()的函数参数接受多个参数,那么就可以map传递多个序列,如果这些传经来的序列长度不一,那么会在短序后面补None.# 定义函数def add1(a): return a+1def add2(a,b): return a+bdef add3(a,b,c): return a+b+ca1=[1,2,3,4,5]a2=[1,2,3,4,5]a3=[1,2,3,4,5] b=map(add1,a1)print b #[2, 3, 4, 5, 6]b=map(add2,a1,a2)print b #[2, 4, 6, 8, 10]b=map(add3,a1,a2,a3)print b #[3, 6, 9, 12, 15]
0 0
- Python中reduce、map、filter、apply、tuple函数
- filter,map,reduce,apply函数
- 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常用内置函数介绍【filter,map,reduce,apply,zip】
- python中的函数编程:lambda,apply, filter,map,reduce
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- python中map、filter、reduce 函数使用方法
- Python中filter、map、reduce函数
- Python中map,filter和apply函数
- Python 中的 lambda,filter,map,reduce,apply
- python的filter,map,zip,reduce,apply
- python map函数,filter,reduce
- UVA 1160 X-Plosives
- JavaScript闭包
- 10、大数,高精度计算---百位大数
- 进程内和进程外Session
- 2014-06-20 日志
- Python中reduce、map、filter、apply、tuple函数
- Java 值传递与引用传递
- IllegalStateException: Can not perform this action after onSaveInstanceState
- 11、素数算法逐步优化
- 磁盘读写过程总结+debugfs总结
- poi操作doc、docx、xls、xlsx文档总结
- androd 调用系统拍照功能
- 关于android app跳转和从web跳转的问题(转载修正)
- 12、 枚举法---八皇后