• python 匿名函数lambda和内置函数(sorted, filter, map, reduce)


    '''匿名函数
    lambda 参数:返回值
    
    通常配合内置函数一起使用(sorted, map, filter, reduce)
    
    sorted(Iterable, key=None,reverse=False)
        key是关键,排序规则,sorted会自动把可迭代对象中的每一个传递给key.
        并根据key返回的内容进行排序。
        
    map(function, iterable)
        可以对可迭代对象中的每一个元素进行映射,分别执行function
        
    filter(function, iterable)
        function:用来筛选的函数,在filter中会自动的把iterable中的元素传递给function.
        然后根据function返回的True或者False来判断是否保留此项数据。
    
    reduce(function, iterable, initial)
        reduce函数会自动把可迭代对象中的每一个拿出来传递给function
        第一次传递两个值进去,然后把函数的返回值和下一个元素继续作为参数传递给function
        以此类推,最终得到function计算的结果。
    
    '''

    lst = [1, 5, 3, 4, 6]
    lst2 = sorted(lst)
    print(lst)  # [1, 5, 3, 4, 6]
    print(lst2)  # [1, 3, 4, 5, 6]
    
    dic = {1:"A", 3:"C", 2:"B"}
    print(sorted(dic))  # 如果是字典,则返回排序过后的key 结果:[1, 2, 3]
    lst = ["apple", "banana", "peach", "pear", "watermelon"]
    lst2 = sorted(lst, key=lambda s: len(s))  # 计算字符串长度排序
    print(lst2)  # ['pear', 'apple', 'peach', 'banana', 'watermelon']
    lst = [{"id": 1, "name": "lily", "age": 18},
          {"id": 2, "name": "lucy", "age": 20},
          {"id": 3, "name": "tom", "age": 19}]
    lst2 = sorted(lst, key=lambda dic: dic["age"])  # 根据年龄排序
    print(lst2)  # [{'id': 1, 'name': 'lily', 'age': 18}, {'id': 3, 'name': 'tom', 'age': 19}, {'id': 2, 'name': 'lucy', 'age': 20}]
    lst = [1, 2, 3, 4, 5, 6, 7]
    lst2 = filter(lambda s : s%2==0, lst)  # 筛选所有的偶数
    print(lst2)  # <filter object at 0x0000000005E94898>
    print(list(lst2))  # [2, 4, 6]
    lst = [{"id": 1, "name": "lily", "age": 18},
          {"id": 2, "name": "lucy", "age": 16},
          {"id": 3, "name": "tom", "age": 20}]
    lst2 = filter(lambda dic: dic["age"] >16, lst)  # 筛选年龄大于16的数据
    print(lst2)  # <filter object at 0x00000000059BC6A0>
    print(list(lst2))  # [{'id': 1, 'name': 'lily', 'age': 18}, {'id': 3, 'name': 'tom', 'age': 20}]
    lst = [1, 2, 3, 4, 5]
    lst2 = map(lambda i: i**2, lst)  # 求列表中每个元素的平方
    print(lst2)  # <map object at 0x0000000005E94278>
    print(list(lst2))  # [1, 4, 9, 16, 25]
    lst1 = [1, 2, 3, 4, 5]
    lst2 = [2, 4, 6, 8, 10]
    lst3 = map(lambda x,y: x+y, lst1, lst2)  # 求两个列表索引位置相同的元素的和
    print(lst3)  # <map object at 0x00000000059BC748>
    print(list(lst3))  # [3, 6, 9, 12, 15]
    from functools import reduce
    
    lst = [1, 2, 3, 4, 5]
    def func(x, y):
        return x + y
    
    lst2 = reduce(func, lst)
    print(lst2)  # 15
    
    lst3 = reduce(lambda x, y: x + y, lst)
    print(lst3)  # 15
    lst = [1, 2, 3, 4, 5]
    lst2 = reduce(lambda x, y: x + y, lst, 50)  # 这里的50是起始值
    print(lst2)  # 65
  • 相关阅读:
    LInux下几种定时器的比较和使用
    R中字符串操作
    GIS基本概念
    特征选择实践
    xcrun: error: invalid active developer path (/Applications/Xcode.app/Contents/Developer)解决办法
    mac os idea的快捷键
    python代码打包发布
    机器学习之聚类
    机器学习之决策树
    机器学习之逻辑回归
  • 原文地址:https://www.cnblogs.com/lilyxiaoyy/p/11899417.html
Copyright © 2020-2023  润新知