首页 > 代码库 > python几个内置函数
python几个内置函数
filter
故名思意,是过滤的意思
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> def f(x):
if x%2 == 0:
return True>>> f(4)
True
>>> filter(f,l)[0, 2, 4, 6, 8]
2. zip
>>> name=[‘xiaoming‘,‘xiaoli‘,‘xiaolan‘]
>>> age=[12,23,34]
>>> tel=[‘133‘,‘134‘,‘156‘]
>>> zip(name,age,tel)
[(‘xiaoming‘, 12, ‘133‘), (‘xiaoli‘, 23, ‘134‘), (‘xiaolan‘, 34, ‘156‘)]>>> test=[1,2]
>>> zip(name,age,tel,test)
[(‘xiaoming‘, 12, ‘133‘, 1), (‘xiaoli‘, 23, ‘134‘, 2)]
3.map
>>> map(None,name,age,tel)
[(‘xiaoming‘, 12, ‘133‘), (‘xiaoli‘, 23, ‘134‘), (‘xiaolan‘, 34, ‘156‘)]
>>> map(None,name,age,tel,test)
[(‘xiaoming‘, 12, ‘133‘, 1), (‘xiaoli‘, 23, ‘134‘, 2), (‘xiaolan‘, 34, ‘156‘, None)]
>>> a=[1,3,4]
>>> b=[5,6,7]
>>> def f(x,y):
return x*y
>>> map(f,a,b)
[5, 18, 28]
>>> map(None,a,b)
[(1, 5), (3, 6), (4, 7)]
>>> map(f,a,b)
[5, 18, 28]
4.reduce
l=range(1,101)
>>> def f(x,y):
return x+y
>>> reduce(f,l)
5050
>>> reduce(lambda x,y:x+y,l)
5050
>>>
通过实例可以看出这几个函数还是很有用处的。
python几个内置函数