Python 用列表替换for循环
大家好,这里是python的新手…如果这是一个基本问题,请原谅我 我想让我的代码非常简短,并注意到有一些简单的方法可以通过将Python 用列表替换for循环,python,Python,大家好,这里是python的新手…如果这是一个基本问题,请原谅我 我想让我的代码非常简短,并注意到有一些简单的方法可以通过将for放在方括号内来浏览排序列表 e、 g 相当于: 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,.
for
放在方括号内来浏览排序列表
e、 g
相当于:
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)]
哪里是
=
y = [i for i in range(10)]
# ^
z = [np.random.binomial(500,.5) for i in xrange(499)]
# ^
哪里是
=
y = [i for i in range(10)]
# ^
z = [np.random.binomial(500,.5) for i in xrange(499)]
# ^
可能是你最好的朋友。它允许您从可编辑对象创建新的一维数组
基本上:
>>> 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.])
虽然基本的列表理解对于您的小阵列来说非常有用,但如果您希望使用更大的阵列,这将更加有效。可能是您最好的朋友。它允许您从可编辑对象创建新的一维数组
基本上:
>>> 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.])
虽然基本列表理解适用于您的小阵列,但如果您希望使用更大尺寸的阵列,这将更加有效。采用大小/形状-使用:
z = np.random.binomial(500, 0.5, 499)
采用尺寸/形状-使用以下选项:
z = np.random.binomial(500, 0.5, 499)
此功能称为列表理解。看看它们是如何工作的 然而,在这种特殊情况下,您不需要理解列表,因为它需要一个
size
参数。要生成包含499个样本的向量,可以执行以下操作:
z = np.random.binomial(500, 0.5, size=499)
此功能称为列表理解。看看它们是如何工作的 然而,在这种特殊情况下,您不需要理解列表,因为它需要一个
size
参数。要生成包含499个样本的向量,可以执行以下操作:
z = np.random.binomial(500, 0.5, size=499)
这应该起作用:
z=[np.随机.二项式(500.5)表示X范围内的i(499)]
在这种情况下,numpy函数可以接受第三个大小参数,这样可能更快
z = np.random.binomial(500, .5, 499)
这应该起作用:
z=[np.随机.二项式(500.5)表示X范围内的i(499)]
在这种情况下,numpy函数可以接受第三个大小参数,这样可能更快
z = np.random.binomial(500, .5, 499)
同一位置
=
也用于y
:)同一位置=
也用于y
)