美文网首页
2019-01-05 day10function2 函数作为函数

2019-01-05 day10function2 函数作为函数

作者: woming | 来源:发表于2019-01-05 18:26 被阅读0次

05 函数的参数是函数

3. 变量作为函数的实参

函数1作为函数2的实参 -- 函数2就是一个高阶函数

a = 10
def func4(n: int):
    print(n + n - 1)

func4(a)

def func5(x):
    print(x)
    x(12)

func5(func4)
func5(lambda x: x*2)

非常重要!!!

函数作为参数的应用:sort函数
列表.sort(key=None, reverse=False)
参数key - 要求是一个带有一个参数并且返回值是比较对象。这儿的参数指向的是列表中元素。
            确定按照元素的什么值进行排序
list1 = [1, 23, 45, 6]
list1.sort()
print(list1)

all_students = [
    {'name': '张三', 'age': 19, 'score': 94},
    {'name': '李四', 'age': 34, 'score': 70},
    {'name': '地方三', 'age': 12, 'score': 45},
    {'name': '范甘迪三', 'age': 45, 'score': 76},
]

# 这儿的item是需要排序的列表元素
# def func(item):
#     return item['age']
# all_students.sort(key=func)
all_students.sort(key=lambda item: item['score'])      # 按成绩从小到大排序
all_students.sort(key=lambda item: item['age'], reverse=True)  # 按年龄从大到小排序
print(all_students)

tuple1 = (
    (10, 23),
    (23, 45),
    (30, 34)
)

new_tuple = sorted(tuple1, key=lambda item: sum(item))
print(new_tuple)

极其重要!!!

sorted() 内部原理!!!

def my_sorted(iterable, key=None, reverse=False):
    list1 = list(iterable)
    if key:
        for x in range(len(iterable) - 1):
            for y in range(x + 1, len(iterable)):
                item1 = list1[x]
                item2 = list1[y]
                if key(item1) > key(item2):
                    list1[x], list1[y] = list1[y], list1[x]
    else:
        # 快速排序
        for x in range(len(iterable) - 1):
            for y in range(x + 1, len(iterable)):
                if list1[y] < list1[x]:
                    list1[x], list1[y] = list1[y], list1[x]
    if not reverse:
        # 从小到大
        return list1
    else:
        # 从大到小
        return list1[::-1]


print(my_sorted([1, 23, 9, 10]))
print(my_sorted(all_students, key=lambda x: x['age']))
# print(my_sorted(all_students))

练习:按学生的平均分排序

all_students = [
    {'name': '张三', 'age': 19, 'score': {'c': 87, 'm': 56, 'e': 98}},
    {'name': '李四', 'age': 34, 'score': {'c': 66, 'm': 88, 'e': 67}},
    {'name': '地方三', 'age': 12, 'score': {'c': 76, 'm': 65, 'e': 87}},
    {'name': '范甘迪三', 'age': 45, 'score': {'c': 78, 'm': 90, 'e': 40}},
]

# 我的解
new = sorted(all_students, key=lambda x: sum(x['score'].values()) / len(x['score']))
print(new)


print('=========老师的解=================')
# 老师的解
def average(student):
    scores = student['score']
    sum1 = 0
    for key in scores:
        sum1 += scores[key]
    return sum1/4

all_students.sort(key=average)
print(all_students)

4. 变量作为函数的返回值

函数1作为函数2的返回值 - 函数2是返回值高阶函数

def operatioin(char):
    if char == '+':
        def func1(*nums):
            return sum(nums)
        # 将函数作为函数的返回值
        return func1
    elif char == '-':
        def func1(*nums):
            # (12, 32, 3, 4)
            # 如果没有传参
            if not nums:
                return 0

            sum1 = nums[0]
            for index in range(1, len(nums)):
                sum1 -= nums[index]
            return sum1
        return func1

print(operatioin('+')(1, 2, 3, 4))
print(operatioin('-')(10, 3, 4))

相关文章

  • 2019-01-05 day10function2 函数作为函数

    05 函数的参数是函数 3. 变量作为函数的实参 函数1作为函数2的实参 -- 函数2就是一个高阶函数 非常重要...

  • Scala 高级函数(一)

    一、高阶函数 定义函数,调用函数 函数作为值进行传递 函数作为参数进行传递 函数可以作为函数返回值进行返回 二、匿...

  • 2018-08-31day10-函数和文件操作

    一、函数作为变量 1.函数给其他变量赋值 2.函数作为列表的元素 3.将函数作为字典的值 4.函数作为函数的参数 ...

  • 高阶函数

    高阶函数 函数作为参数 函数作为返回值的时候 作为参数 回调函数 回调函数就是一个参数,将这个函数作为参数传到另一...

  • 返回函数

    返回函数 函数作为返回值: 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回。 比如求和函数: de...

  • 函数

    函数类型作为函数参数 函数类型作为函数返回值 返回值是函数类型的函数,叫做高阶函数(Higher-Order Fu...

  • Kotlin函数式编程 (1)高级函数

    函数式编程简介高级函数函数类型函数字面量函数作为另一个函数返回值使用函数作为参数使用 一、函数式编程简介   函数...

  • 函数式编程2

    返回函数 函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回。求和的函数是这样定义的:...

  • 高阶函数与闭包

    高阶函数 当 函数 A 接收函数 B 作为参数,或者把函数 C 作为返回值输出时,我们称 函数 A 为高阶函数。通...

  • Python常用高阶函数map、sorted、filter及生成

    先说什么是高阶函数,高阶函数是指可以接受函数作为参数、或者可以把函数作为结果返回的函数,这种函数就是高阶函数。使用...

网友评论

      本文标题:2019-01-05 day10function2 函数作为函数

      本文链接:https://www.haomeiwen.com/subject/wsrdrqtx.html