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
  • 相关阅读:
    Python开发【第二十一篇】:Web框架之Django【基础】
    梳理
    Python Day15 jQuery
    day12 html基础
    Python day11 Mysql
    锻炼马甲线
    第一章:软件性能测试基本概念
    第4关—input()函数
    第3关—条件判断与嵌套
    第2关—数据类型与转换
  • 原文地址:https://www.cnblogs.com/pdun/p/11303145.html
Copyright © 2011-2022 走看看