推导式:使用推导式可以快速生成列表、元组、字典以及集合类型的数据
1.列表推导式
①:基本格式
格式:【exp for x in list】
for--in-- 用于遍历列表(或者其他可迭代对象)
exp 用于对每层循环中的列表元素进行运算
# 用循环在列表打印1~100: list = [] for i in range(101): list.append(i) print(list) # 用列表推导式打印1~100: a = [o for o in range(101)] print(a) # 以上两种1~100方式打印结果相同 b = [a ** 2 for a in range(101)] print(b)
②:if语句的列表推导式
格式:【exp for x in list if cond】
功能:遍历列表,若列表中的元素符合条件cond,按表达式exp对齐进行运算
""" 带有if语句的列表推导式:[变量 for 变量 in list if cond] """ # 打印偶数even c = [even for even in range(101) if even % 2 == 0] print(c)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
③:嵌套for循环语句的列表推导式
格式:【exp for x in list_1 for y in list_2】
功能:for语句从左到右分别是外层循环和内层循环
""" 嵌套for循环语句的列表推导式:[x for x in list_1 for y in list_2] """ ls_1 = [1, 2, 3] ls_2 = [4, 5, 6] ls_3 = [x + y for x in ls_1 for y in ls_2] print(ls_3)
[5, 6, 7, 6, 7, 8, 7, 8, 9]
case:打印乘法口诀表
# 嵌套for循环打印九九乘法表 for i in range(1, 10): for j in range(1, i + 1): print(f'{i}*{j}={i * j}\t', end=' ') print() # 使用推导式打印九九乘法表 ls_5 = [f'{w}*{q}={q * w}' for q in range(1, 10) for w in range(1, q + 1)] print(ls_5)
1 1*1=1 2 2*1=2 2*2=4 3 3*1=3 3*2=6 3*3=9 4 4*1=4 4*2=8 4*3=12 4*4=16 5 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 10 ['1*1=1', '1*2=2', '2*2=4', '1*3=3', '2*3=6', '3*3=9', '1*4=4', '2*4=8', '3*4=12', '4*4=16', '1*5=5', '2*5=10', '3*5=15', '4*5=20', '5*5=25', '1*6=6', '2*6=12', '3*6=18', '4*6=24', '5*6=30', '6*6=36', '1*7=7', '2*7=14', '3*7=21', '4*7=28', '5*7=35', '6*7=42', '7*7=49', '1*8=8', '2*8=16', '3*8=24', '4*8=32', '5*8=40', '6*8=48', '7*8=56', '8*8=64', '1*9=9', '2*9=18', '3*9=27', '4*9=36', '5*9=45', '6*9=54', '7*9=63', '8*9=72', '9*9=81']
④:带有if语句和嵌套for循环语句的列表推导式
格式:【exp for x in list_1 [if cond]
for y in list_2 [if cond]
~~~~~~
for n in list_n [if cond] 】
1 """ 2 带有if语句和嵌套for循环语句的列表推导式: 3 """ 4 5 y = ['零' if i == 0 else '三' if i == 3 else '五' if i == 5 else i for i in range(10)] 6 print(y) 7 8 l = ['敲桌子' if e == 7 else '敲桌子' if e == 17 else '敲桌子' if e == 27 9 else e for e in range(30)] 10 print(l) 11 12 ls_6 = [f'{ab}*{bc}={ab * bc}' for ab in range(1, 10) 13 if ab > 3 14 for bc in range(1, ab + 1) 15 if bc >= 3] 16 print(ls_6) 17 18 l1 = [1, 2, 3] 19 l2 = [2, 3, 4] 20 for i in l1: 21 for j in l2: 22 if i == j: 23 print(i) 24 25 s = [(i, j) for i in l1 for j in l2 if i == j] 26 print(s)
结果:
1 ['零', 1, 2, '三', 4, '五', 6, 7, 8, 9] 2 [0, 1, 2, 3, 4, 5, 6, '敲桌子', 8, 9, 10, 11, 12, 13, 14, 15, 16, '敲桌子', 18, 19, 20, 21, 22, 23, 24, 25, 26, '敲桌子', 28, 29] 3 ['4*3=12', '4*4=16', '5*3=15', '5*4=20', '5*5=25', '6*3=18', '6*4=24', '6*5=30', '6*6=36', '7*3=21', '7*4=28', '7*5=35', '7*6=42', '7*7=49', '8*3=24', '8*4=32', '8*5=40', '8*6=48', '8*7=56', '8*8=64', '9*3=27', '9*4=36', '9*5=45', '9*6=54', '9*7=63', '9*8=72', '9*9=81'] 4 2 5 3 6 [(2, 2), (3, 3)]
2.元组推导式
基本格式:(表达式 for 迭代变量 in 可迭代对象 [if 条件表达式] )
1 """ 2 元组推导式: 3 基本格式:(表达式 for 迭代变量 in 可迭代对象 [if 条件表达式] ) 4 """ 5 6 # 使用 tuple() 函数,可以直接将生成器对象转换成元组 7 a = (x for x in range(1, 10)) 8 print(tuple(a)) 9 # 结果: (1, 2, 3, 4, 5, 6, 7, 8, 9) 10 11 # 直接使用 for 循环遍历生成器对象,可以获得各个元素 12 abc = (x for x in range(1, 10)) 13 for i in abc: 14 print(i, end=' ') 15 # 结果: 1 2 3 4 5 6 7 8 9
3.集合推导式
基本格式:{exp for x in set if cond}
与列表推导式相似,【】换成{ }
1 """ 2 集合推导式:基本格式:{exp for x in set if cond} 3 与列表推导式相似,【】换成{ } 4 """ 5 a = {x for x in range(50) if x % 5 == 0} 6 print(a) 7 # 结果:{0, 35, 5, 40, 10, 45, 15, 20, 25, 30} 8 9 abc = (1, 1, 2, 3, 4, 5, 6, 6) 10 efg = {x ** 2 for x in abc if x % 2 == 0} 11 print(efg) 12 # 结果: {16, 4, 36}
4.字典推导式
基本格式:{表达式 for 变量 in 对象 【if cond】}
""" 字典推导式 基本格式: {表达式 for 迭代变量 in 可迭代对象 [if 条件表达式]} """ a = ['Jack', 'Tom'] # 将列表中各字符串值为键,各字符串的长度为值,组成键值对 b = {key: len(key) for key in a} print(b) # 结果:{'Jack': 4, 'Tom': 3}
标签:10,推导,list,列表,python07,range,print From: https://www.cnblogs.com/smll1/p/17869929.html