[英]Replace for loops by list comprehension
在这里向python的新手问好...所以如果这是一个基本问题,请原谅我
我想提出我的代码很短,发现有简单的方法,通过把要经过某种形式的名单for
方括号的内部
例如
x = []
for i in range(10):
x.append(i)
print x
等效于:
y[i for i in range(10)]
print y
所以我想以类似的方式缩短以下内容
import matplotlib as plt
import numpy as np
y = []
for i in xrange(499):
y.append(np.random.binomial(500,.5))
plt.hist(y)
我尝试了这个,但是没有用
z[np.random.binomial(500,.5) for i in xrange(499)]
此功能称为列表理解。 请查看此文档以了解它们如何工作。
但是,在这种特殊情况下,您不需要列表理解,因为numpy.random.binomial
采用size
参数。 要生成包含499个样本的向量,可以执行以下操作:
z = np.random.binomial(500, 0.5, size=499)
这应该工作:
z = [np.random.binomial(500,.5) for i in xrange(499)]
在这种情况下,numpy函数可以接受大小的第三个参数,因此可能更快。
z = np.random.binomial(500, .5, 499)
numpy.random.binomial采用大小/形状-使用以下方法:
z = np.random.binomial(500, 0.5, 499)
=
在哪里?
y = [i for i in range(10)]
# ^
z = [np.random.binomial(500,.5) for i in xrange(499)]
# ^
numpy.fromiter
可能是您最好的朋友。 它使您可以从可迭代对象创建新的一维数组。
基本上:
>>> iterable = (x*x for x in range(5))
>>> np.fromiter(iterable, np.float)
array([ 0., 1., 4., 9., 16.])
所以对你来说:
>>> iterable = np.asarray([np.random.binomial(500,.5) for x in xrange(499)])
>>> np.fromiter(iterable, np.float)
array([ 256., 251., 254., 229., 260., 253., 246., 231., 247.,
255., 245., 229., 269., 257., 248., 244., 224., 238.,
247., 250., 255., 239., 254., 276., 226., 241., 253.,
247., 264., 264., 244., 254., 259., 246., 253., 254.,
241., 269., 239., 246., 252., 251., 255., 247., 240.,
248., 264., 237., 239., 249., 252., 234., 239., 260.,
258., 252., 238., 240., 253., 262., 242., 282., 256.,
247., 241., 243., 252., 241., 265., 232., 261., 253.,
237., 256., 263., 232., 248., 258., 236., 261., 243.,
250., 245., 262., 250., 255., 255., 230., 238., 245.,
267., 246., 245., 249., 260., 233., 240., 282., 246.,
245., 245., 249., 256., 256., 245., 260., 241., 245.,
266., 237., 260., 242., 237., 265., 249., 256., 249.,
276., 264., 246., 256., 244., 253., 229., 249., 242.,
252., 234., 246., 232., 233., 250., 261., 239., 263.,
269., 254., 254., 246., 255., 241., 269., 259., 230.,
239., 249., 249., 267., 247., 219., 250., 257., 238.,
260., 254., 253., 244., 240., 245., 244., 257., 225.,
258., 269., 238., 248., 238., 254., 256., 284., 263.,
238., 254., 249., 261., 243., 244., 250., 236., 240.,
257., 262., 246., 227., 261., 266., 232., 250., 255.,
261., 253., 241., 252., 242., 244., 246., 244., 237.,
236., 286., 249., 245., 251., 245., 250., 272., 241.,
238., 247., 263., 258., 266., 240., 253., 266., 255.,
234., 245., 243., 266., 271., 251., 263., 237., 237.,
253., 250., 230., 245., 254., 242., 245., 265., 247.,
245., 242., 254., 255., 236., 234., 253., 257., 228.,
246., 236., 261., 244., 240., 259., 239., 254., 239.,
263., 260., 229., 266., 248., 242., 247., 251., 255.,
255., 251., 248., 254., 258., 236., 248., 248., 247.,
269., 236., 255., 231., 246., 258., 246., 254., 248.,
250., 265., 232., 259., 248., 241., 246., 239., 241.,
242., 268., 257., 239., 244., 240., 252., 258., 241.,
256., 256., 240., 246., 253., 257., 258., 232., 243.,
269., 248., 240., 249., 264., 246., 233., 245., 243.,
241., 240., 241., 228., 281., 252., 254., 266., 236.,
246., 249., 258., 242., 249., 254., 251., 249., 256.,
236., 247., 234., 252., 250., 246., 249., 246., 220.,
261., 230., 250., 225., 248., 233., 244., 258., 250.,
251., 259., 243., 249., 264., 250., 244., 255., 254.,
244., 230., 244., 237., 250., 267., 255., 236., 250.,
226., 257., 239., 253., 259., 231., 256., 251., 266.,
239., 260., 278., 248., 248., 238., 258., 234., 260.,
264., 261., 255., 266., 259., 236., 270., 244., 233.,
231., 252., 251., 262., 260., 243., 263., 250., 250.,
247., 261., 251., 229., 250., 237., 250., 252., 262.,
244., 246., 251., 249., 253., 246., 257., 245., 248.,
255., 268., 255., 253., 212., 241., 260., 259., 243.,
242., 246., 263., 251., 244., 252., 245., 265., 238.,
270., 253., 254., 242., 246., 265., 240., 255., 255.,
276., 255., 252., 260., 235., 256., 230., 269., 240.,
249., 254., 234., 250., 251., 243., 248., 250., 240.,
253., 259., 242., 255., 269., 252., 253., 230., 237.,
255., 247., 263., 231., 241., 258., 241., 252., 251.,
259., 241., 257., 262., 268., 256., 249., 239., 275.,
242., 262., 258., 244.])
虽然基本的列表理解是小型数组的执行者,但是如果您要使用更大大小的数组,则性能会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.