zoukankan      html  css  js  c++  java
  • 内置函数:zip、filter、map、stored

    zip:多个可迭代对象,从开始,一对一返回一个元组

    a = ('a','b','c ','c')
    b = ('a','b','c','c')
    ret = zip(a,b)
    for i in ret:
        print(i)
    print(list(zip(a,b)))
    
    #打印
    ('a', 'a')
    ('b', 'b')
    ('c ', 'c')
    ('c', 'c')
    [('a', 'a'), ('b', 'b'), ('c ', 'c'), ('c', 'c')]

     filter:筛选,过滤掉不符合条件的元素,返回由符合条件元素组成的迭代器对象。 
    该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判断,然后返回 True 或 False,最后将返回 True 的元素放到新对象中

    l = [1,2,3,-1,-5,0,51]
    func = filter(lambda x : x > 0,l)
    for i in func:
        print(i)
    
    #打印:
    1
    2
    3
    51

    map:map()函数接收两个参数,一个是函数,一个是序列,将传入的函数依次作用到序列的每个元素,并把结果作为新的l对象中返回,map()为迭代器。

    l = [1,2,3,-1,-5,0,51]
    func = map(lambda x : x + 1,l)
    for i in func:
        print(i)
    
    #打印
    2
    3
    4
    0
    -4
    1
    52

    sorted:排序,与列表的内置方法sort不同,sorted对所有可迭代对象进行排序,是新建一个内存进行排序,不会影响原有列表

    l = [1,2,3,-1,-5,0,51]
    info_sort = sorted(l,key= abs)
    print(info_sort)
    
    #打印:
    [0, 1, -1, 2, 3, -5, 51]
  • 相关阅读:
    Bootstrap_警示框
    Bootstrap_标签
    Bootstrap_分页
    Bootstrap_导航条
    Bootstrap_导航
    Bootstrap_按钮工具栏
    Bootstrap_下拉菜单
    Bootstrap_网格系统
    Bootstrap_表单_图标
    统计学习方法 李航---第12章 统计学习方法总结
  • 原文地址:https://www.cnblogs.com/aizhinong/p/11402754.html
Copyright © 2011-2022 走看看