Python可以用一行代码做很多事情,其中包括生成一个符合特定规则的列表,如下所示:
下面的代码用到了:**运算符,for in关键词,range函数,if not关键词
>>> aListOf2 = [2**x for x in range(10)] >>> aListOf2 [1, 2, 4, 8, 16, 32, 64, 128, 256, 512] >>> aListOf2 = [2**x for x in range(10) if x > 5] >>> aListOf2 [64, 128, 256, 512] >>> aListOf2 = [2**x for x in range(10) if not x <= 4] >>> aListOf2 [32, 64, 128, 256, 512] >>>