# _Author: lWX548594
# _Date: 2018/7/10 0010
# _SctiptName: build-in
str = ['a', 'b', 'c', 'd']
print("filter内置函数")
def fun1(s):
if s != 'a' and s != 'd':
return s
ret = filter(fun1, str) # 返回的是一个filter对象, 需要循环一下才能得到里面的值.
for i in ret:
print(i)
输出结果:
b
c
print("map函数, 批量对列表的内容进行修饰")
def fun2(s):
return s + 'alven'
ret = map(fun2, str)
for i in ret:
print(i)
# 输出结果:
aalven
balven
calven
dalven
print("reduce函数, 和map, filter的区别是, 它的返回值不再是迭代器了.")
from functools import reduce
def add(x, y):
return x + y
print(reduce(add, range(1, 101)))
输出结果:
5050
print("lambda函数, 匿名函数")
add = lambda a, b: a + b
print(add(3, 4))
输出结果:
7
from functools import reduce
print(reduce(lambda x, y: x * y, range(1, 10)))
输出结果:
362880
squeres = map(lambda x: x * x, range(9))
for i in squeres:
print(i)
输出结果:
0
1
4
9
16
25
36
49
64
from functools import reduce
print(reduce(lambda a, b: a * b, range(1, 9)))
输出结果:
40320
squere = map(lambda x: x*x, range(1, 5))
for i in squere:
print(i)
输出结果:
1
4
9
16
转载请注明:liutianfeng.com » Python内置函数
发表回复
要发表评论,您必须先登录。