列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。
举个例子,要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]可以用list(range(1, 11)):
`>>> L = []
for x in range(1, 11):
... L.append(x * x)
...L
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]`
但是循环太繁琐,而列表生成式则可以用一行语句代替循环生成上面的list
>>> [x * x for x in range(1, 11)] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
扩展
import random
import numpy as np
import time
np.random.seed(random.seed(time.time()))
sites=['https://cn.bing.com/search?q={}
'.format(np.random.rand(1)) * x for x in range(200)]```