列表解析

使用一行代码就可以生产列表。

|

list= [ values *2 for value in range(1,11)]

这个会得到[2,4,6,8,10,12,14,16,18,20]


list = [ values *1 for value in range(1,11)]

这个会得到[1,2,3,4,5,6,7,8,9]


list = [ values *1 for value in range(1,11,2)]

这个会得到[1,3,5,7,9]


list = [value**2 for value in range(1,11)]

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


list = [value**3 for value in range(1,11)]

[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

|

发表评论