• Lambda Expression


    |--Lambda Expression
    #f(x) = x^2

        def f(x):
            return x * x
    
        print(f(5))
    
        f = lambda x: x*x
    
        print(f(5))
    
        def g(x, y):
            return x * y
    
        g = lambda x,y: x+y
    
        print(g(4, 5))
    

    eg:

        countries = []
            file = open("countries_zh.csv", "r")
            for line in file:
                line = line.strip()
                arr = line.split(",")
                name = arr[1]
                capt = arr[3]
                popu = int(arr[4])
                countries.append( (name, capt, popu) )
            countries.sort(key= lambda country: country[2])
            for country in countries:
                print(country)
    


    |--用于复合函数(一个函数嵌套一个函数)

        f(x) = ax^2 + bx + c
    
        def quadratic(a, b, c):
            return lambda x: a*x*x + b*x + c
    
        f = quadratic(1, -1, 2)
        print(f(5))
    
        print(quadratic(1, -1, 2)(5))
    
  • 相关阅读:
    装饰器的应用
    绑定路由关系
    基本使用
    numpy多项式拟合
    pandas空值处理与插值
    索引
    事务
    子查询
    视图
    自关联
  • 原文地址:https://www.cnblogs.com/xzpin/p/11185534.html
Copyright © 2020-2023  润新知