python学习笔记三之函数(基础篇)
2016-01-17 11:08
656 查看
函数
内置函数
View Code
一些功能函数
[b]lambda 函数
filter 函数
过滤函数 对于序列中的元素进行筛选,最终获取符合条件的序列
map 函数
将序列中的元素全部传递给一个函数
内置函数
def haha(*arg,**args): print(arg,type(arg)) print(args,type(args)) haha(11,22,33,44,name='doubi') (11, 22, 33, 44) <class 'tuple'> {'name': 'doubi'} <class 'dict'> li1= [11,22,33,44] name = {'haha':123,'xixi':456} haha(*li1,**name) (11, 22, 33, 44) <class 'tuple'> {'haha': 123, 'xixi': 456} <class 'dict'> #星号只有在定义(允许使用不定数目的参数)或者调用("分割"字典或者序列)的时候才有效。 #字典元素必须放在后面,否则将报错 haha(name='test',12,13,14) haha(name='test',12,13,14) ^ SyntaxError: positional argument follows keyword argument
View Code
一些功能函数
[b]lambda 函数
#普通函数 def func1(arg): return arg + 1 #匿名函数,执行简单语句 func2 = lambda arg: arg + 1 print(func2(12)) #匿名函数也可以像普通函数一样,收集多个参数 fun3 = lambda a,b,*arg,**args: print(a,b,arg,args) fun3(2,3,4,5,6,name='koka')
filter 函数
过滤函数 对于序列中的元素进行筛选,最终获取符合条件的序列
#filter(function_or_None, iterable) seq = [11,22,33,44,55] filter(lambda a:a>33,seq) #返回迭代器,可以基于一个返回布尔值的函数对元素进行过滤
map 函数
将序列中的元素全部传递给一个函数
#map(func, *iterables) l1 = [11,22,33] l2 = [44,55,66] def func1(a1,a2): return a1 + a2 map(func1,l1,l2) [55,77,99]
相关文章推荐
- 1、Python django 框架下的word Excel TXT Image 等文件的上传
- python入门笔记(Day7)--slots,@property,MixIn,定制(str,iter,getitem,getattr,call)枚举(Enum)元type,metaclass,ORM
- 【Python】Matplotlib绘图库初探
- python小点心--__call__
- Python中*args 和**kwargs
- python pip源
- 【python学习笔记】使用类封装实现贪吃蛇
- python读取word
- Python 调用自定义包
- python学习笔记三之深浅copy,扩展数据类型(基础篇)
- Python基础知识随手记
- Python 4.2 访问限制
- Python+django实现文件上传
- Python+django实现文件下载
- windows7上面安装opencv python开发环境,并处理cv2.VideoCapture()打不开视频文件的问题
- python字典
- python字符串
- [Leetcode] 15.3Sum @python
- Python 实例一 统计指定文件的行数或读取指定文件指定行数的内容
- Python 第三篇(上):python文件基础操作、json模块、lambda、map、filter、reduce和函数位置参数