Python全栈开发——函数式

#map(方法(一般用lambda表达式,也可以是自定义的方法),可迭代对象(可以用for循环遍历))用list取出
#处理序列中的每个元素,得到的结果是一个列表,该列表元素个数及位置与原来一样
num=[1,2,34,223,1,3]
def c(d):
    return d**2
def text(list,h):
    tet=[]
    for e in list:
        res=h(e)
        tet.append(res)
    return tet

print(text(num,lambda x:x**2))
print(list(map(lambda x:x+1,num)))
print(list(map(c,num)))
#filter(方法(一般用lambda表达式,也可以是自定义的方法),可迭代对象(可以用for循环遍历))用list取出
#遍路序列中的每个元素,判断每个元素得到布尔值,如果TUre,保留下来
move_people=['sbZ_dgdrh','sb_jh','sb_jty','sbfs','hjf','hfj','dfjsb']

def filer(people,string):
    ret=[]
    for e in move_people:
        b=e.find(string)
        if b==-1:
            ret.append(e)
    return ret

print(filer(move_people,'_'))

print(list(filter(lambda n:not n.startswith('sb'),move_people)))
#reduce
#from functools import reduce
from functools import reduce
sum1=0
def jia(a):
    global sum1
    sum1+=a
    return sum1
def text(arrary,fun):
    s=0
    for e in arrary:
        s+=fun(e)
    return s

num=[1,2,4,5,4,6,8,7,6,4,12]
print(text(num,jia))

print(reduce(lambda x,y:x+y,num))
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/lujiacheng-Python/p/9612964.html