zoukankan      html  css  js  c++  java
  • Python学习笔记——以函数为参数的内置函数

    1.用法

    一个参数

    def ds(x):
        return 2 * x + 1
    
    print(ds(5))
    
    11
    
    g = lambda x : 2 * x + 1
    print(g(5))
    
    11
    

    两个参数

    def add(x,y):
        return x + y
    
    add(3,4)
    
    7
    
    f = lambda x,y : x + y
    f(3,4)
    
    7
    

    2.filter()

    #过滤,剩下为ture的内容
    filter(None,[1,0,False,True])
    print(list(filter(None,[1,0,False,True])))
    
    [1, True]
    
    #过滤,剩下函数返回值为true的内容
    def odd(x):
        return x % 2
    temp = range(10)
    #将第二个参数列表中的数据一个个代入第一个函数中,若返回true,则留下。
    show = filter(odd,temp)
    print(list(show))
    
    [1, 3, 5, 7, 9]
    
    list(filter(lambda x : x % 2,range(10)))
    
    [1, 3, 5, 7, 9]
    

    3.map()

    #将第二个参数列表中的数据一个个代入lambda函数中
    list(map(lambda x : x * 2, range(10)))
    
    [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    
  • 相关阅读:
    HDU What Are You Talking About
    谷歌面试题
    POJ 2299 UltraQuickSort
    单链表排序
    HDU Hat’s Words
    C++ const关键字
    求二叉树任意两点间的距离
    HDU Phone List
    POJ 2352 Stars
    C++ volatile关键字
  • 原文地址:https://www.cnblogs.com/nigream/p/11251092.html
Copyright © 2011-2022 走看看