zoukankan      html  css  js  c++  java
  • 内置函数

    abs

    #abs() 函数返回数字的绝对值。
    
    print (abs(-1))
    1
    View Code

    filter筛选

    filter(function,iderable)
    我们自己筛选方式,把需要筛选的对象给他,filter就能帮我们筛选
    
    names=['a_sb','b_sb','c','d','yeee']
    res=filter(lambda name:name.endswith('sb'),names)
    print(list(res))
    
    ['a_sb', 'b_sb']
    View Code

    map映射

    #对可迭代对象中的每一个元素进行映射,分别去执行function
    
    
    def func(x):
        return x*x
    res=map(func,[1,2,3,4,5,6,7,8,9])
    print(list(res))
    
    ------------------------------------------------
    
    print(list(map(lambda x:x*x,[1,2,3,4,5])))
    
    ------------------------------------------------
    res=map(str,[1,2,3,4,5])
    print(list(res))
    
    
    [1, 4, 9, 16, 25, 36, 49, 64, 81]
    [1, 4, 9, 16, 25]
    ['1', '2', '3', '4', '5']
    View Code

    sorted排序

    sorted(iderable,key,reverse)   #支持倒序
    
    把可迭代对象中的每一个元素取出来,交给后边的key,计算出数字,作为当前对象的权重,根据权重排序
    
    li=['123','三国演义','1','七夕']
    
    def func(x):
        return len(x)
    
    res=sorted(li,key=func)
    print(res)
    
    ['1', '七夕', '123', '三国演义']
    ---------------------------------------------------
    
    res=sorted(li,key=lambda li:len(li),reverse=True)
    print(res)
     
    ['三国演义', '123', '七夕', '1']
    View Code
  • 相关阅读:
    [手游新项目历程]-36- error: stray ‘357’ in program
    广告学(一)
    VMware的Unity模式
    poj3709
    poj1849
    bzoj2007
    bzoj3209
    bzoj2466,poj1222
    bzoj1016
    bzoj2186
  • 原文地址:https://www.cnblogs.com/pdun/p/11303145.html
Copyright © 2011-2022 走看看