zoukankan      html  css  js  c++  java
  • Python Lambda Functions

    Useful lambda functions in python. refer to : https://www.geeksforgeeks.org/python-lambda-anonymous-functions-filter-map-reduce/


    In Python, an anonymous function means that a function is without a name. As we already know that the def keyword is used to define a normal function in Python. Similarly, the lambda keyword is used to define an anonymous function in Python. 

    • def() and lambda() comparison

    # Python code to illustrate cube of a number
    # showing difference between def() and lambda().
    def cube(y):
        return y*y*y
    
    lambda_cube = lambda y: y*y*y
    
    # using the normally
    # defined function
    print(cube(5)).  # 125
    
    # using the lambda function
    print(lambda_cube(5)).  # 125
    • Using lambda() Function with filter()

      The filter() function in Python takes in a function and a list as arguments. This offers an elegant way to filter out all the elements of a sequence “sequence”, for which the function returns True. Here is a small program that returns the odd numbers from an input list: 

    # Python code to illustrate
    # filter() with lambda()
    li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
    
    final_list = list(filter(lambda x: (x%2 != 0) , li))
    print(final_list)
    # [5, 7, 97, 77, 23, 73, 61]
    # Python 3 code to people above 18 yrs
    ages = [13, 90, 17, 59, 21, 60, 5]
    
    adults = list(filter(lambda age: age>18, ages))
    
    print(adults)
    # [90, 59, 21, 60]
    • Using lambda() Function with map()

      The map() function in Python takes in a function and a list as an argument. The function is called with a lambda function and a list and a new list is returned which contains all the lambda modified items returned by that function for each item. Example: 

    # Python code to illustrate
    # map() with lambda()
    # to get double of a list.
    li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
    
    final_list = list(map(lambda x: x*2, li))
    print(final_list)
    # [10, 14, 44, 194, 108, 124, 154, 46, 146, 122]
    # Python program to demonstrate
    # use of lambda() function
    # with map() function
    animals = ['dog', 'cat', 'parrot', 'rabbit']
    
    # here we intend to change all animal names
    # to upper case and return the same
    uppered_animals = list(map(lambda animal: str.upper(animal), animals))
    
    print(uppered_animals)
    # ['DOG', 'CAT', 'PARROT', 'RABBIT']
    • Using lambda() Function with reduce()

    The reduce() function in Python takes in a function and a list as an argument. The function is called with a lambda function and an iterable and a new reduced result is returned. This performs a repetitive operation over the pairs of the iterable. The reduce() function belongs to the  functools module. 

    # Python code to illustrate
    # reduce() with lambda()
    # to get sum of a list
    
    from functools import reduce
    li = [5, 8, 10, 20, 50, 100]
    sum = reduce((lambda x, y: x + y), li)
    print (sum)
    # 193
    Here the results of previous two elements are added to the next element and this goes on till the end of the list like (((((5+8)+10)+20)+50)+100).
    # python code to demonstrate working of reduce()
    # with a lambda function
    
    # importing functools for reduce()
    import functools
    
    # initializing list
    lis = [ 1 , 3, 5, 6, 2, ]
    
    # using reduce to compute maximum element from list
    print ("The maximum element of the list is : ",end="")
    print (functools.reduce(lambda a,b : a if a > b else b,lis))

    #output:
    The maximum element of the list is : 6
  • 相关阅读:
    ASP.NET MVC 中将FormCollection与实体间转换方法
    MVC Actionlink 参数说明
    mvc中Scripts.Render、Styles.Render
    MVC项目内无法添加System.Web.Optimization
    NUGET命令
    Entity Framwork(EF) 7——在Controller内获取指定字段的值
    学习Django
    windows下上传文件到VWware的centos
    Python、PIP环境变量的配置
    对已经发布订阅的sqlserver进行修改-添加新的表
  • 原文地址:https://www.cnblogs.com/LilyLiya/p/14807000.html
Copyright © 2011-2022 走看看