zoukankan      html  css  js  c++  java
  • 【python021-函数lambda表达式】

    一、匿名函数

    1、lambda表达式

    >>> g = lambda x:x*2+1
    >>> g(5)
    11
    >>>

    ---冒号前面的x是函数的参数,冒号后面是函数的返回值

    >>> g = lambda x,y:x*y
    >>> g(4,5)
    20
    >>>

    2、lambda表达式的作用:

    a、Python写一些执行脚本时,使用lambda就可以省下定义函数过程,比如说我们只是需要写个简单的脚本来管理服务器时间,我们就不需要专门定义一个函数然后在写调用,使用lambda就可以使用代码更加精简

    b、对于一些比较抽象并且整个程序执行下来只需要调用一两次的函数,有时候给函数起个名字也是比较头疼的问题,使用lambda就不需要考虑命名的问题了

    c、简化代码的可读性,由于普通的屌丝函数阅读经常要跳到开头def定义部分,使用lambda函数可以省去这样的步骤

    二、过滤器

    1、filter()函数:过滤掉任何非true的内容

    class filter(object)
     |  filter(function or None, iterable) --> filter object      filter(函数,迭代) 
     |  
     |  Return an iterator yielding those items of iterable for which function(item)
     |  is true. If function is None, return the items that are true.

    返回一个迭代器,产生那些函数(item)的迭代项
     | 是真的。 如果函数为None,则返回true的项目

    举例1、:

    >>> list(filter(None,[1,0,False,True]))
    [1, True]
    >>>

    ---过滤掉任何非true的内容

    举例2、

    #列表取余

    >>> def odd(x):
        return x % 2

    >>> temp = range(10)
    >>> show = filter(odd,temp)
    >>> list(show)
    [1, 3, 5, 7, 9]
    >>>

    利用lambda函数进行上续的操作

    >>> list(filter(lambda x : x%2,range(10)))
    [1, 3, 5, 7, 9]
    >>>

    2、map()函数:

     map(func, *iterables) --> map object
     |  
     |  Make an iterator that computes the function using arguments from
     |  each of the iterables.  Stops when the shortest iterable is exhausted.

    创建一个使用参数来计算函数的迭代器, 每个迭代。 当最短的迭代器耗尽时停止。

    >>> list (map(lambda x:x*2,range(10)))
    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    >>>

    三、练习题

    1、请使用lambda表达式将下边函数转换成匿名函数

    def fun_A(x, y=3):
            return x * y
    ---转换后:lambda x,y=3 : x*y

    2、请将下边的匿名函数转变为普通的屌丝函数:

    lambda x : x if x % 2 else None

    ---转换后:

    >>> def fun(x):
        if x%2:
            return x
        else:
            return None
      
    >>> fun(7)
    7

    3、利用filter()和lambda表达式快速求出100以内所有3的倍数

    >>> list(filter(lambda x : not(x%3),range(1,100)))
    [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

    >>> list(filter(lambda x : x%3==0,range(1,100)))
    [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

    4、还记得列表推导式吗?完全可以使用列表推导式代替filter()和lambda组合,你可以做到吗?

    举例:把第三题的list(filter(lambda x : not(x%3),range(1,100)))修改为列表推导式

    >>> [x for x in range(1,100) if x%3==0]
    [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

    5、使用zip会将两数以元组的形式绑定在一块,例如:

    >>> list(zip([1, 3, 5, 7, 9], [2, 4, 6, 8, 10]))
    [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
    但如果我希望打包的形式是灵活多变的列表而不是元组(希望是[1,2],[3,4],[5,6],[7,8],[9,10]这种新式),你能做到吗?(采用map和lambda表达式)

    list(map(lambda x,y:[x,y],[1,3,5,7,9],[2,4,6,8,10]))
    [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
    >>>

    ---强大的map()后边是可以接受多个序列作为参数的

  • 相关阅读:
    Apollo与ROS
    QT windeployqt
    自定义QGraphicsItem
    ROS与C++
    aptitude与apt-get
    解决tcp粘包问题
    网络中两台主机通信
    I/O多路复用之select、poll、epoll
    Nginx命令行控制
    C++11
  • 原文地址:https://www.cnblogs.com/frankruby/p/9187620.html
Copyright © 2011-2022 走看看