One - One Code All

Blog Content

python快速生成列表

Python   2009-08-01 17:07:20

python 快速生成一个列表。

列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。

>>> a = [x for x in range(1,9)]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8]
>>> b = [x * x for x in range(1, 11) if x % 2 == 0]
>>> b
[4, 16, 36, 64, 100]



上一篇:python查看安装包的版本
下一篇:python寻找list中最大最小元素对应的索引

The minute you think of giving up, think of the reason why you held on so long.