1. 高阶函数

函数在Python中的定位

  • 函数与在Python中是一等公民

  • 函数可以作为普通变量、参数与返回值

高阶函数的定义

在数学和计算机科学中,高阶函数是至少满足下列一个条件的函数:

  • 接受一个或多个函数作为输入

  • 输出一个函数

自己写一个sorted函数

def sort(iterable , * , key=None, reverse = False):
    newlist = []
    for x in iterable:
        cx = key(x) if key else x
        for i,y in enumerate(newlist):
            cy = key(y) if key else y
            flag = cx>cy if reverse else cx<cy
            if flag:
                newlist.insert(i,x)
                break
        else:
            newlist.append(x)
    return newlist

内建函数中的高阶函数

sorted

排序

filter

过滤数据

map

映射

Last updated