python的各种推导式(列表推导式、字典推导式、集合推导式)
#### 列表推导式
#!/usr/bin/python3 # -*- coding: utf-8 -*- # @Time : 2020/11/22 14:34 # @Author : XiaoWei # @Email : 1464819144@qq.com # @FileName : 推导式.py # @Project : script # 推导式 """ 通过一行循环判断,遍历一系列数据的方式是推导式 语法: val for val in Iterable (把想要的值卸载 for的左侧) 里面是一样循环判断!根据套在推导式外层的符号判断具体是什么类型的推导式 推导式有三种: 列表推导式: [val for val in Iterable] 集合推导式: {val for val in Iterable} 字典推导式: [k:v for k:v in Iterable] """ # 1、普通 list_a = [1, 2, 3, 4] list_b = [] list_c = [] # 普通写法 for i in list_a: list_b.append(i) print(list_b) # 推导式写法 res = [i for i in list_a] print(res) # 普通修改 for i in list_a: i = i*2 list_c.append(i) print(list_c) # 推导式写法 res = [str(i * 2) for i in list_a] print(res) # 2、带判断条件 list_a = [1, 2, 3, 4] list_b = [] # 普通 for i in list_a: if i %2 ==0: list_b.append(i) print(list_b) # 推导式写法 res = [i for i in list_a if i % 2 == 0] print(res) # 3、双循环 list_a = ["a1", "a2", "a3"] list_b = ["b1", "b2", "b3"] list_c = [] # 常规写法 for i in list_a: for j in list_b: list_c.append(i + " ❤❤ " + j) print(list_c) # 推导式写法 res = [i + " ❤❤ " + j for i in list_a for j in list_b] print(res) print("<========>") # 带有判断条件的双循环 list_a = ["a1", "a2", "a3"] list_b = ["b1", "b2", "b3"] # 常规写法 list_c = [] for i in list_a: for j in list_b: if list_a.index(i) == list_b.index(j): list_c.append(i + " ❤❤ " + j) print(list_c) # 推导式写法 res = [i + " ❤❤ " + j for i in list_a for j in list_b if list_a.index(i) == list_b.index(j)] print(res)
#### 字典与列表推导式
# ### 集合推导式 """ 案例: 满足年龄在18到21,存款大于等于5000 小于等于5500的人, 开卡格式为:尊贵VIP卡老x(姓氏),否则开卡格式为:抠脚大汉卡老x(姓氏) 把开卡的种类统计出来 """ listvar = [ {"name":"王家辉","age":18,"money":10000}, {"name":"王水机","age":19,"money":5100}, {"name":"王鹏","age":20,"money":4800}, {"name":"李站","age":21,"money":2000}, {"name":"李小龙","age":180,"money":20} ] """ 可哈希数据:不可变数据 Number(int float complex bool) str tuple 不可哈希数据:可变数据 list set dict 如果是字典的键或者是集合的值,数据类型必须可哈希 """ """ 三目运算符: True if 条件表达式 else False 如果条件表达式成立,执行True 如果条件表达式不成立,执行False """ # 基本语法 setvar = set() for i in listvar: if 18<=i['age']<=21 and 5000 <= i['money'] <= 5500: strvar = "尊贵VIP卡老" + i['name'][0] else: strvar = "抠脚大汉卡老" + i['name'][0] setvar.add(strvar) print(setvar) # 集合推导式 # lst = [15 if i % 2 == 1 else 16 for i in range(3)]# 16 15 16 例子 """ # 分解: { 左手边: "尊贵VIP卡老" + i['name'][0] if 18<=i['age']<=21 and 5000 <= i['money'] <= 5500 else "抠脚大汉卡老" + i['name'][0] 右手边: for i in listvar } """ setvar = {"尊贵VIP卡老" + i['name'][0] if 18<=i['age']<=21 and 5000 <= i['money'] <= 5500 else "抠脚大汉卡老" + i['name'][0] for i in listvar} print(setvar) # ### 字典推导式 """ ### (1)enumerate enumerate(iterable,[start=0]) 功能:枚举 ; 将索引号和iterable中的值,一个一个拿出来配对组成元组放入迭代器中 参数: iterable: 可迭代性数据 (常用:迭代器,容器类型数据,可迭代对象range) start: 可以选择开始的索引号(默认从0开始索引) 返回值:迭代器 """ from collections import Iterator,Iterable listvar = ['罗淞峰',"李祥海","银燕","赖廷"] it = enumerate(listvar) # print(it) # print(isinstance(it,Iterator)) # print(list(it)) """ [里面可以是列表,元组,字符串] (里面可以是列表,元组,字符串) {里面是元组} [('a',1),('b',"sdfsdfsdfsdfsd")] (['a',1],('b',"sdfsdfsdfsdfsd")) {('a',1),("b",909090909090909090900909)} """ # dict 强转迭代器变成字典 # {0: '罗淞峰', 1: '李祥海', 2: '银燕', 3: '赖廷'} res = dict(it) print(res) # 字典推导式的写法变成字典 it = enumerate(listvar) # 重置迭代器 dic = {k:v for k,v in it} #k接受的是012..345 v接受的是列表中的值 print(dic) it = enumerate(listvar,start= 10) dic = {k:v for k,v in it} print(dic) ### (2)zip """ zip(iterable, ... ...) 功能: 将多个iterable中的值,一个一个拿出来配对组成元组放入迭代器中 iterable: 可迭代性数据 (常用:迭代器,容器类型数据,可迭代对象range) 返回: 迭代器 正常按照索引进行配对,放到元组中,如果找不到配对选项,直接舍弃. """ lst1 = ["吴波","帅乐","温素文"] # lst2 = ["夜光花","吴耀橘","王照"] lst2 = ["夜光花","吴耀橘","王照"] lst3 = ["温红杰","刘璐","陈凤杰"] it = zip(lst1,lst2,lst3) print(it) print(isinstance(it,Iterator)) print(list(it)) # dict强转迭代器变成字典 lst2 = ["夜光花","吴耀橘","王照"] lst3 = ["温红杰","刘璐","陈凤杰"] res = dict(zip(lst2,lst3)) print(res) # 字典推导式 dic = {k:v for k,v in zip(lst2,lst3) } print(dic)