zoukankan      html  css  js  c++  java
  • day16——函数式编程和内置函数

    编程的方法论

    面向过程:找到问题的 

    函数式:不可变、不用变量保存状态、不修改变量

    面向对象: 

    高阶函数:

    满足俩个特性任意一个即为高阶函数

    1.函数的传入参数是一个函数名

    2.函数的返回值是一个函数名

    append() 方法用于在列表末尾添加新的对象。

    map函数:

    num_l=[1,2,10,5,3,7]# 计算该列表中数的平方值
    方法一:
    # ret=[]
    # for i in num_l:
    #     ret.append(i**2)
    # print(ret)
    方法二:
    def map_test(array):
        ret=[]
        for i in num_l:
            ret.append(i**2)   #
    return ret
    ret=map_test(num_l)
    print(ret)
    方法三:
    num_l=[1,2,10,5,3,7]
    #lambda x:x+1
    def add_one(x):
    return x+1
    #lambda x:x-1
    def reduce_one(x):
    return x-1
    #lambda x:x**2
    def square_one(x):
    return x**2
    def map_test(func,array):
    ret=[]
    for i in num_l:
    res=func(i)
    ret.append(res)
    return ret
    print(map_test(add_one,num_l))
    print(map_test(reduce_one,num_l))
    #print(map_test(lambda x:x**2,num_l))
    print(map_test(square_one,num_l))
    #终极版本
    num_l=[1,2,10,5,3,7]
    def map_test(func,array):
    ret=[]
    for i in num_l:
    res=func(i) #add_one
    ret.append(res)
    return ret

    print(map_test(lambda x:x+1,num_l))
    res=map(lambda x:x+1,num_l)
    print('内置函数map,处理结果',res)
    # for i in res:
    # print(i)
    print(list(res))
    print('传的是有名函数',list(map(reduce_one,num_l)))

    #大写转换
    msg='wuxiping'
    print(list(map(lambda x:x.upper(),msg)))

     filter函数:

    # movie_people=['sb_alex','sb_wupeiqi','linhaifeng','sb_yuanhao']
    # def filter_test(array):
    #     ret=[]
    #     for p in array:
    #         if not p.startswith('sb'):
    #             ret.append(p)
    #     return ret
    # print(filter_test(movie_people))
    
    # movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
    # def sb_show(n):
    #     return n.endswith('sb')
    # def filter_test(func,array):
    #     ret=[]
    #     for p in array:
    #         if not func(p):
    #             ret.append(p)
    #     return ret
    # res=filter_test(sb_show,movie_people)
    # print(res)
    #终极版本
    movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
    # def sb_show(n):
    #     return n.endswith('sb')
    #lambda n:n.endswith('sb')
    def filter_test(func,array):
        ret=[]
        for p in array:
            if not func(p):
                ret.append(p)
        return ret
    res=filter_test(lambda n:n.endswith('sb'),movie_people)
    print(res)
    #filter函数
    movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
    #print(filter(lambda n:n not n.endswith('sb',movie_people)))
    res=filter(lambda n:not n.endswith('sb'),movie_people)
    print(list(res))

     reduce函数:

    from functools import reduce
    # num_l=[1,2,3,100]
    # res=0
    # for num in num_l:
    #     res+=num
    # print(res)
    
    #相加
    # num_l=[1,2,3,100]
    # def reduce_test(array):
    #     res=0
    #     for num in num_l:
    #         res+=num
    #     return res
    # print(reduce_test(num_l))
    
    #相乘
    # num_l=[1,2,3,100]
    # def reduce_test(func,array):
    #     res=array.pop(0)
    #     for num in array:
    #         res=func(res,num)
    #     return res
    # print(reduce_test(lambda x,y:x*y,num_l))
    
    # 指定初始值
    # num_l=[1,2,3,100]
    # def reduce_test(func,array,init=None):
    #     if init is None:
    #         res=array.pop(0)
    #     else:
    #         res=init
    #     for num in array:
    #         res=func(res,num)
    #     return res
    # print(reduce_test(lambda x,y:x*y,num_l,100))
    
    #reduce函数
    num_l=[1,2,3,100]
    print(reduce(lambda x,y:x+y,num_l,1))
    print(reduce(lambda x,y:x+y,num_l,))

    总结:

    #总结:
    # map函数:处理序列中的每个元素,得到的结果是一个‘列表’,该‘列表’元素个数和位置与原来一样
    #filter函数:遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下来得到的结果是一个‘列表’
    # people=[{'name':'alex','age':10000},
    #         {'name': 'wupeiqi', 'age': 10000},
    #         {'name': 'yuanhao', 'age': 9000},
    #         {'name': 'linhaifeng', 'age': 18},]
    # print(list(filter(lambda p:p['age']<=18,people)))
    #reduce函数:处理 一个序列,把序列进行合并操作
    # num_l=[1,2,3,100]
    # print(reduce(lambda x,y:x+y,num_l,1))
    # print(reduce(lambda x,y:x+y,num_l,))

    内置函数:

    # print(abs(-1))
    # print(all([1,2,'1']))#做布尔运算(所有的都是True才返回True)
    # name='你好'
    # print(bytes(name,encoding='utf-8'))   #编码,三个字节
    # print(bytes(name,encoding='utf-8').decode('utf-8'))# 解码
    # print(bytes(name,encoding='gbk'))#两个字节
    # print(bytes(name,encoding='gbk').decode('gbk'))
    # #print(bytes(name,encoding='ascii'))#ascii 不能编码中文
    # print(chr(100))
    # print(dir(all))
    # print(divmod(10,3))
    # dic={'name':'alex'}
    # dic_str=str(dic)
    # print(dic_str)
    # print(eval(dic_str))
    eval()函数:以Python的方式解析并执行字符串,并将返回的结果输出
    # d1=eval(dic_str)#把字符串中的数据结构给提取出来 # print(d1['name']) # a='1+2*(3/3-1)-2'#把字符串中的表达式进行运算 # print(eval(a)) #可hash的数据类型即不可变数据类型,不可hash的数据类型即可变数据类型 # print(hash('dhfsaklefownfs2134254')) # print(hash('-03thjsdnfkgqopw3utjlsdfml;')) # name='alex' # print(hash(name)) # print(hash(name)) # print(hash(name)) # print(help(dir)) # print(bin(10)) #10进制转换成2进制 # print(hex(12)) #16 # print(oct(12)) #8 # print(isinstance(1,int)) # print(isinstance('abc',int)) # print(isinstance('abc',str)) name='哈哈哈哈' print(globals()) print(__file__) print(locals())
  • 相关阅读:
    查看kafka在zookeeper中节点信息和查看方式
    安装单机版redis
    一 Redis 简介 和存储
    Spark消费kafka的直连方式
    Streaming 累加器和广播变量 和sql
    sparkStreaming转换算子02
    DStreams输入 从kafka消费数据 直连
    关于上下文图
    2018年春季个人阅读计划
    问题账户需求分析
  • 原文地址:https://www.cnblogs.com/wuxiping2019/p/10586793.html
Copyright © 2011-2022 走看看