zoukankan      html  css  js  c++  java
  • python中几个常用函数

    # zip 将两个列表,对应位置的值拼接成元组,最后结果是一个列表套元组,注:不对应的值,会被舍弃.
    l1 = [1,1,3,6,7]
    l2 = [1,2,3,4]
    print(list(zip(l1,l2))) # [(1, 1), (2, 2), (3, 3), (6, 4)]
    
    # filter 将列表内的元素,一一传入指定函数,过滤后,获得一个列表类型的结果
    l1 = [1,2,3,6,7]
    print(list(filter(lambda a:a>2,l1))) # [3, 6, 7]
    
    # map 将列表内的值,一一传入指定函数,计算后,获得一个列表类型的结果
    l1 = [1,2,3,6,7]
    print(list(map(lambda a:a+1,l1))) # [2, 3, 4, 7, 8]
    
    # sorted 对列表内的值按升序排列
    l1 = [1,2,3,6,7]
    print(sorted(l1)) # [1, 2, 3, 6, 7]
    
    # reversed 对列表内的值按降序排列
    l1 = [1,2,3,6,7]
    print(list(reversed(l1))) # [7, 6, 3, 2, 1]
    
    # reduce求和
    from functools import reduce
    print(reduce(lambda x,y:x+y,l1)) # 19

    如果错误望务必指出.谢谢!

  • 相关阅读:
    编译nginx
    MVPN技术原理
    python中_, __, __foo__区别及使用场景
    https双向认证(python)
    http keepalive test code(python)
    压缩 KVM 的 qcow2 镜像文件
    nohup python程序,print无输出
    Less(51)
    Less(50)
    Less(49)
  • 原文地址:https://www.cnblogs.com/hellozizi/p/11983139.html
Copyright © 2011-2022 走看看