zoukankan      html  css  js  c++  java
  • day 14

    day 14

    01.匿名函数

    没有函数名的函数

    函数通过函数名加括号来调用,没有函数名也就是说无法被调用,无法单独使用。

    # 匿名函数构成
    lambda 参数:返回值
    # 如果要调用的话
    f=lambda 参数:返回值
    f(参数)# 将其变为有名函数
    

    其实用通常与max/min/filter/map/sorted连用,这与这五个内置方法的使用方法有关

    1. max 返回最大值 括号里加同种类型元素(min)用法相同
    max(参数,key=函数对象(默认型参)) 
    
    res = max([1, 2, 3, 4, 5])
    print(res) # 返回5
    res = max([1, 2, 3, 4, 5],[1]) # 当后面列表中只有0,1时只打印前面的列表
    print(res) # 返回[1,2,3,4,5]
    res = max([1, 2, 3, 4, 5],[2])
    print(res) # 返回[2]
    res = max(1, 2, 3, 4, 5, 6, )
    print(res) # 返回6
    # 当我们把字典当作参数传进去时就只会按照key的大小排列
     salary_dict = {
         'nick': 3000,
         'jason': 100000,
         'tank': 5000,
         'sean': 2000,
         'z': 1000
     }
    res=max(salary_dict)
    print(res) # z , 这样使用只会对所有的key进行排序取出最大的那一个
    def func(k):
        return salary_dict[k]
    res=max(salary_dict,key=func) # max提供了这样一种用法(参数,key=函数名)会将参数内的每一个元素当作实参传入该函数内并接收返回值,在对返回值进行排序判断
    # # key=func默认做的事情
    # # 1. 循环遍历salary_dict,会取到所有的key值
    # # 2. 然后把所有的key值依次丢入func中,返回薪资
    # # 3. 通过返回的薪资排序,得到最大值
    print(res)
    # 但这样写就很繁琐,匿名函数提供了这样一种方法
    res=max(salary_dict,key=lambda k:salary_dict[k])
    print(res)
    
    1. fileter 筛选
    filter(函数名,参数) # 通过函数内条件对参数内元素进行筛选,输出结果为对象
    
    salary_dict = {
        'nick': 3000,
        'jason': 100000,
        'tank': 5000,
        'sean': 2000,
        'z': 1000
    }
    
    res = filter(lambda item: salary_dict[item] > 3000, salary_dict)
    print(list(res))
    
    1. map 映射
    map(函数名,参数) # 将参数内元素通过函数进行处理
    
    def function1(item):
        return item + 2
    
    
    res = map(function1, [1, 2, 3, ])
    print(res)
    print(list(res))
    
    
    1. sorted 排序
    sorted(参数,key=函数对象,reverse=任意数据类型),reverse= 决定正序还是到序,默认为None,布尔值为False则正序,为True则到序;用法与max相同
    

    02.内置函数

    python解释器内置的方法

    1. enumerate 获取索引和值
    lt=['a','b','c']
    dic={}
    for k,v in enumerate(lt):
      print(k,v)
      dic[k]=v
    print(dic)
    
    1. eval 把字符串的引号去掉,留下来的是什么就是什么
    s='vv'
    vv='sl'
    print(eval(s))  # sl ;可应用于从文件中提取出的字符串转换
    
    1. bytes 非ascll字符编码格式的字符变为二进制格式
    res = bytes('中国', encoding='gbk')
    print(res)
    
    1. chr/ord 转换为二进制相映的字符/转换为相应二进制的十进制

      print(chr(88))
      print(ord('a'))
      
      
    2. divmod 取整,取余

    print(divmod(10,2))  # (5,0)
    
    
    1. hash 打印哈希值(整形为其本身)
    print(hash(-125.123))
    
    
    1. abs 为整数
    print(abs(-123))
    
    
    1. all 可迭代对象内的元素全部为True则为True
    print(all([1,2,3,0]))
    
    
    1. any 可迭代对象内的元素全部为False则为False
    print(any([0, 0,0 ]))
    
    
    1. bin/oct/hex 二进制/八进制/十六进制
    print(bin(123)) 
    print(oct(123))
    print(hex(123))
    
    
    1. dir 列出模块的所有方法
    import time
    print(dir(time))
    
    
    1. frozenset 不可变化的集合,类似于元组 # 将可迭代对象的迭代器对象写成一个不可变化的集合
    s = frozenset({1:2,3:4})
    print(s)
    
    
    1. gloabals/locals
    print(globals())  # 列出所有全局变量
    print(locals())  # 	列出当前所在位置的所有变量
    
    
    1. pow 求幂
    print(pow(2,3))  # 2的3次幂
    
    
    1. round
    print(round(-123.1234))  # -123
    
    
    1. slice # 与索引切片相同
    s = slice(1, 5, 2)  
    lt = [1, 2, 3, 4, 5, 6, 7]
    print(lt[s])
    print(lt[1:5:2])
    
    
    1. sum # 求和
    print(sum([1, 2, 3, 4, 5]))  # 15
    
    
    1. __import__ # 通过字符串来到如模块·
    time = __import__('time') # 等于 import time
    
    
  • 相关阅读:
    nowcoderD Xieldy And His Password
    Codeforces681D Gifts by the List
    nowcoder80D applese的生日
    Codeforces961E Tufurama
    Codeforces957 Mahmoud and Ehab and yet another xor task
    nowcoder82E 无向图中的最短距离
    nowcoder82B 区间的连续段
    Codeforces903E Swapping Characters
    Codeforces614C Peter and Snow Blower
    Codeforces614D Skills
  • 原文地址:https://www.cnblogs.com/luocongyu/p/11587668.html
Copyright © 2011-2022 走看看