zoukankan      html  css  js  c++  java
  • Python高阶函数实战【自定义高阶函数、map、reduce、filter、sorted】

    一.概述

      高阶函数,就是一个函数可以接收另一个函数作为参数的函数,scala与之类似。

    二.导入基础包

    import random
    from functools import reduce

    三.自定义高阶函数  

    #定义普通函数,自动生成列表
    def getList():
        hList = list(range(5))
        return [key * random.randint(0,3) for key in hList] #生成随机数
    
    #定义普通函数,求不同列表中最大并集
    def getMaxSet(listA,listB):
        setA = set(listA) #转为set
        setB = set(listB)
        setAll = setA | setB #求并集
        return list(setAll)#转为list
    
    #定义高阶函数
    def highFunction(listA,listB,getMaxSet):
        return getMaxSet(listA,listB)

    四.自定义高阶函数应用

    # 获取两个集合
    listA = getList()
    print("集合A:{}".format(listA))
    listB = getList()
    print("集合B:{}".format(listB))
    
    # 获取两个集合的并集
    listC = getMaxSet(listA, listB)
    print("集合A和集合B的并集:{}".format(listC))
    
    #调用高阶函数
    values = highFunction(listA,listB,getMaxSet)
    print("调用高阶函数获取集合A和集合B的并集:{}".format(values))

    五.自定义高阶函数执行结果

      

    六.map实战及执行结果

    #map(f,Iterable):f;要执行的操作,Iterable:可循环
    def mapF(arg):
        return len(arg)
    
    # 调用内置高阶函数map,获取每个字符串的长度
    test = ['python', 'tensorflow', 'keris', 'tensorboard', 'tensorflow', 'tensorflow', 'keris']
    maps = list(map(mapF, test))
    print("各个字符串的长度为:{}".format(maps))

      

    七.reduce实战及执行结果

    #reduce(f,Iterable):f;要执行的操作,Iterable:可循环
    def reduceF(arg1,arg2):
        return arg1 + arg2
    
    # 调用内置高阶函数reduce,计算所有字符串的长度之和
    reduces = reduce(reduceF, maps)
    print("字符串的总长度为:{}".format(reduces))

      

    八.and运算符和strip()测试

    # 测试and运算符和strip()去除字符串空格算子
    a = " a" and " a".strip()
    b = "b " and "b ".strip()
    c = " c " and " c ".strip()
    print("测试and运算符和strip()去除字符串空格算子:a:{},b:{},c:{}".format(a,b,c))

      

    九.filter()过滤器

    # filter(f,Iterable):filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。
    #该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,
    #最后将返回 True 的元素放到新列表中。
    # 过滤为空的字符串,不修改满足条件的数据
    def del_empty(s):
        return s and s.strip() #strip()用于移除字符串头尾指定的字符(默认为空格或换行符)
    filterList = list(filter(del_empty, [" a", "", "b ", None, "v", " "]))
    print(filterList)

      

    十.sorted排序

    #基础sort()
    print(sorted([36, 5, -12, 9, -21])) #数字型按大小
    print(sorted(["36", "5", "-12", "9", "-21"])) #字符串按ASCII码
    #进阶
    print(sorted([36, 5, -12, 9, -21], key=abs))#先取绝对值,再排序(不影响原数据)
    print(sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower, reverse=True)) #先转成小写,按转置排序(不影响原数据)

      

    十一.运算符

      1.位运算符

        

      2.逻辑运算符

        

      3.成员运算符

        

      4.身份运算符

        

      这些运算符与其它编程语言中的用法是相似的,这里不再举例!

  • 相关阅读:
    凭这份pdf让我轻松拿下了蚂蚁金服、字节跳动、小米等大厂的offer
    阿里面试官看了我写的多线程博文,反问你也看过那本的(多线程)
    Docker简介及基本使用
    springboot常见问题之Rustful风格下,@PutMapping、@@DeleteMapping失效问题
    SpringBoot之国际化功能
    SpringBoot之Thymeleaf模板引擎
    SpringBoot之静态资源处理
    SpringBoot 配置原理
    SpringBoot: 配置文件用法
    SpringBoot版hello world
  • 原文地址:https://www.cnblogs.com/yszd/p/9140946.html
Copyright © 2011-2022 走看看