python几个内建函数
2015-10-09 21:54
639 查看
今天看了几个python的几个内建函数,也都是大家熟悉的。不过因为我用的是Python3.4,所以有了一点新的发现。毕竟大部分文档都是2.7的。
第一个lambda,这个相比之前没有太大的变化。等几天写一个关于C++11 的lambda表达式的博客,大家等着哈。
python中的lambda格式是这样的:
举个例子
这个函数在3.0以后的版本里已经不是内建函数了,需要
这个reduce函数和Google的Map Reduce框架没有什么关系
原型:
reduce函数的官方解释如下:
我的理解就是每次选取序列中的两个元素,代入到函数中。至于具体意思,你们看英文吧。我翻译烂的一B。
举个例子,如果要计算连续n项的和,可以写成如下的形式:
参数列表中的那个initializer类似于初始值
这个有点类似于C++中的
和reduce函数类似,对每个可迭代对象调用function,可迭代对象可以不只一个
python3中,map返回的是一个
看一个有多个可迭代对象的例子
同时,数列长度也可以不必全部都相同
在2.7中map中的function可以是None类型,这时python会做什么?请参考这个作者的文章
python map函数小讲
在3中
错误如下:
代替的,可以用zip_longest(这个函数也需要导入,同样在functools包里
使用
不使用
暂时就到这里吧。。
第一个lambda,这个相比之前没有太大的变化。等几天写一个关于C++11 的lambda表达式的博客,大家等着哈。
python中的lambda格式是这样的:
lambda arguments : expr
举个例子
f=lambda x,y:x+y f(2,3) #output 5
reduce函数
这个函数在3.0以后的版本里已经不是内建函数了,需要from functools import reduce
这个reduce函数和Google的Map Reduce框架没有什么关系原型:
functools.reduce(function, iterable[, initializer])
reduce函数的官方解释如下:
Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.
我的理解就是每次选取序列中的两个元素,代入到函数中。至于具体意思,你们看英文吧。我翻译烂的一B。
举个例子,如果要计算连续n项的和,可以写成如下的形式:
f = lambda x,y:x+y print(reduce(f,range(1,11)) /**output**/ 55
参数列表中的那个initializer类似于初始值
f = lambda x,y:x+y print(reduce(f,range(1,11),100) /**Ouutput***/ 155
这个有点类似于C++中的
std::accmulate
map函数
这个是和2.7中还是有一些区别的。map(function, iterable, ...) Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, se
和reduce函数类似,对每个可迭代对象调用function,可迭代对象可以不只一个
def abc(a): return a+1; list2 = [1,2,3] t=map(abc,list2) print(list(t)) /**Output***/ [2, 3, 4]
python3中,map返回的是一个<class 'map'>
所以需要用list()函数,同时也需要注意print函数在3和2里面的不同
看一个有多个可迭代对象的例子def abc(a,b,c): return a+b+c+3 list1=[1,2,3] list2=[10,20,30] list3=[100,200,300] t=map(abc,list1,list2,list3) print(list(t)) /*Output*/ [114, 225, 336]
同时,数列长度也可以不必全部都相同
list1=[1,2,3] list2=[10,20] list3=[100,200,300] t=map(abc,list1,list2,list3) print(list(t)) /*Output*/ [114, 225]
在2.7中map中的function可以是None类型,这时python会做什么?请参考这个作者的文章
python map函数小讲
在3中
map(None,arg)这样的写法编译器会报错。
错误如下:
#TypeError: 'NoneType' object is not callable
代替的,可以用zip_longest(这个函数也需要导入,同样在functools包里
itertools.zip_longest(*iterables[, fillvalue]) #Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.
使用
fillvalue的情况
from functools import zip_longest list1=[1,2,3] list2=[10,20] list3=[100,300] t=zip_longest(list1,list2,list3,fillvalue=0) #output [(1, 10, 100), (2, 20, 300), (3, 0, 0)]
不使用
fillvalue的情况
from functools import zip_longest list1=[1,2,3] list2=[10,20] list3=[100,300] t=zip_longest(list1,list2,list3) #output [(1, 10, 100), (2, 20, 300), (3, None,None)]
暂时就到这里吧。。
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法