zoukankan      html  css  js  c++  java
  • 8.字典推导式,集合推导式,匿名函数

    两种模式: 循环模式,筛选模式

    l1 = ['小潘', '怼怼哥','西de', 'c']
    {0: '小潘', 1: '怼怼哥', 2: '西de'}
    dic = {}
    for index in range(len(l1)):
        dic[index] = l1[index]
    print(dic)
    print({i:l1[i] for i in range(len(l1))})
    
    1~100
    print({i for i in range(1, 101)})
    

    匿名函数
    匿名函数:没有名字的函数

    匿名函数只能构建简单的函数,一句话函数。

    def func(x,y):
         return x+y
    print(func(1,2))
    匿名函数构建
    func2=lamda x,y:x+y
    print(func2(1,2))
    匿名函数最常用的就是与内置函数结合使用
    写匿名函数:接收一个可切片的数据,返回索引为0与2的对应的元素(元组形式)
    func=lambda x,y:(x[0],x[2])
    print(func('太白金星'))
    写匿名函数:接收两个int函数,将较大的数据返回。
    func1=lambda x,y:x if x>y else y
    print(func1(100,2))
    无形参匿名函数
    func2=lambda :3
    print(func2())
    '''
    3
    '''
  • 相关阅读:
    webpack的安装与配置
    npm初始化
    gitignore的配置
    git本地已有文件夹和远程仓库对应
    git 配置
    开发环境和开发工具
    git 码云使用教程
    递归
    LeetCode 392. 判断子序列
    MongoDB基本操作
  • 原文地址:https://www.cnblogs.com/pythonblogs/p/11089073.html
Copyright © 2011-2022 走看看