推导式

通过一行循环判断遍历出一些列数据的方法叫做推导式

语法:
  val for val in iterable

 

1.推导式基本语法

lst = []
for i in range(1,51):
    lst.append(i)
print(lst)

改写推导式

lst = [  i for i in range(1,51)  ]
print(lst)

小练习

# 1.[1,2,3,4,5] => [2,4,6,8,10]
lst = [ i*2 for i in range(1,6) ]
print(lst)

 

2.带有判断条件的推导式

注意点:for后面紧跟的判断条件只能是单项分支.
[1,2,3,4,5,6,7,8,9,10] => [1,3,5,7,9 … ]

lst = [1,2,3,4,5,6,7,8,9,10]
lst_new = []
for i in lst:
    if i % 2 == 1:
        lst_new.append(i)
print(lst_new)

改写推导式

lst = [ i for i in lst if i % 2 == 1 ]
print(lst)

 

3.多循环推导式 # 谁谁

lst1 = ["孙杰龙","陈露","曹静怡"]
lst2 = ["王志国","邓鹏","合理"]
lst_new = []
for i in lst1:
    for j in lst2:
        lst_new.append(i+""+j)
print(lst_new)

改写推导式

lst = [ i+""+j for i in lst1 for j in lst2 ]
print(lst

 

4.带有判断条件的多循环推导式

lst_new = []
for i in lst1:
    for j in lst2:
        if lst1.index(i) == lst2.index(j):
            lst_new.append(i+""+j)
print(lst_new)

改写推导式

lst = [i+""+j for i in lst1 for j in lst2 if lst1.index(i) == lst2.index(j)]
print(lst)

 

集合推导式

案例:
  满足年龄在18到21,存款大于等于5000 小于等于5500的人,
  开卡格式为:尊贵VIP卡老x(姓氏),否则开卡格式为:抠脚大汉卡老x(姓氏)
  把开卡的种类统计出来

lst = [
    {"name":"赵沈阳","age":18,"money":3000},
    {"name":"赵万里","age":19,"money":5200},
    {"name":"赵蜂拥","age":20,"money":100000},
    {"name":"赵世超","age":21,"money":1000},
    {"name":"王志国","age":18,"money":5500},
    {"name":"王永飞","age":99,"money":5500}
]
setvar = set()
for i in lst:
    print(i) # {'name': '赵沈阳', 'age': 18, 'money': 3000}
    if 18 <= i["age"] <= 21 and 5000 <= i["money"] <= 5500:
        res = "尊贵VIP卡老{}".format(i["name"][0])
    else:
        res = "抠脚大汉卡老{}".format(i["name"][0])
    # 添加到集合中
    setvar.add(res)
print(setvar)

{ 三元运算符 + 推导式 }

setvar = { "尊贵VIP卡老{}".format(i["name"][0]) if 18 <= i["age"] <= 21 and 5000 <= i["money"] <= 5500 else "抠脚大汉卡老{}".format(i["name"][0]) for i in lst }
print(setvar)

 

字典推导式

一.enumerate

enumerate(iterable,[start=0])
功能:枚举 ; 将索引号和iterable中的值,一个一个拿出来配对组成元组,通过迭代器返回
参数:
  iterable: 可迭代性数据 (常用:迭代器,容器类型数据,可迭代对象range)
  start: 可以选择开始的索引号(默认从0开始索引)
返回值:迭代器

 

基本语法

from collections import Iterator,Iterable
lst =["王文","吕洞宾","何仙姑","铁拐李","张国老","曹国舅","蓝采和","韩湘子"]
it = enumerate(lst)
it = enumerate(lst,start=100)
print(isinstance(it,Iterator))

next

print( next(it) )

for + next (推荐,数据较大时使用)

for i in range(3):
    print(next(it))

for

for i in it:
    print(i)

list 强转迭代器

print(list(it))

 

(1) 字典推导式 配合 enumerate 来实现

dic = {k:v for k,v in enumerate(lst,start=100)}
print(dic)
"""
(100, '王文')
(101, '吕洞宾')
(102, '何仙姑')
(103, '铁拐李')
(104, '张国老')
(105, '曹国舅')
(106, '蓝采和')
(107, '韩湘子')
"""

(2) 使用dict强转迭代器,瞬间得到字典

dic = dict( enumerate(lst,start=100) )
print(dic)

 

二.zip

特点:按照索引配对
zip(iterable, … …)
  功能: 将多个iterable中的值,一个一个拿出来配对组成元组,通过迭代器返回
  iterable: 可迭代性数据 (常用:迭代器,容器类型数据,可迭代对象range)
返回: 迭代器

 

基本语法

# lst1 = ["孙开启","王永飞","于朝志"]
# lst2 = ["薛宇健","韩瑞晓","上朝气"]
# lst3 = ["刘文博","历史园","张光旭"]

# 在索引下标同时存在时,才会进行配对,否则舍弃.
lst1 = ["孙开启","王永飞","于朝志"]
lst2 = ["薛宇健","韩瑞晓"]
lst3 = ["刘文博"]

it = zip(lst1,lst2,lst3)
print(list(it))

 

(1) 字典推导式 配合 zip 来实现

lst_key = ["ww","axd","yyt"]
lst_val = ["王维","安晓东","杨元涛"]

# ('ww', '王维'), ('axd', '安晓东'), ('yyt', '杨元涛')
dic = {k:v   for k,v in zip(lst_key , lst_val) }
print(dic)

(2) 使用dict强转迭代器,瞬间得到字典

dic = dict( zip(lst_key , lst_val) )
print(dic)