标签:
常见到有
>>> range(1,100,2)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
>>>
但是当我们遇到稍微复杂一点的怎么办,求1到10的平方,我们可以使用下面的额方法
>>> [x*x for x in range(1,11)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
添加条件判断
>>> [x*x for x in range(1,10) if x%2==0]
[4, 16, 36, 64]
>>>
循环加循环:
>>> [m + n for m in range(1,10,2) for n in range(1,3) if m%2==1]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>>
标签:
原文地址:http://www.cnblogs.com/smail-bao/p/5633979.html