插值表达式(也叫列表推导式)是表达式不是语句
-
list01=[i for i in range(1,31)] print(list01) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
-
list02=[i for i in range(1,31,2)] print(list02) # [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
-
list03=[i for i in range(1,31) if i%2==0] print(list03) # [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]
-
list04=[x**2 for x in range(10)] print(list04) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
-
print ([(x, x**2) for x in range(6)]) # [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
-
list05=list(map(lambda x: x**2, range(10))) print(list05) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
-
二维列表降维:从左往右执行