Python_推导列表
推导列表
1、[表达式 for 变量 in 列表]
>>> [x for x in range(2,7,2)] [2, 4, 6] >>> [x*2 for x in range(2,7,2)] [4, 8, 12] >>> [x**2 for x in range(1,11)] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
2、 [表达式 for 变量 in 列表 if 条件]
>>> [x**2 for x in range(1,11) if x %2==0] [4, 16, 36, 64, 100] >>>