如何将所有数组存储在for循环输出中

时间:2019-02-03 23:19:20

标签: python for-loop storage

进行此for循环时,我仅从代码中打印出一个数组

 npoints=10

x0 = np.zeros(npoints)
y0 = np.zeros(npoints)
z0 = np.zeros(npoints)
vx0 = np.zeros(npoints)
vy0 = np.zeros(npoints)
vz0 = np.zeros(npoints)
result=[]
#Set the initial conditions
for step in range(0,len(n1)):
     x0[0] = x1[step]
     y0[0] = y1[step]
     z0[0] = z1[step]

     vx0[0] = vx1[step]
     vy0[0] = vy1[step]
     vy0[0] = vz1[step]



print x0

这会打印出结果

[-2.72482266  0.          0.          0.          0.          0.
  0.          0.          0.          0.        ]

但是,当我在循环中包含“ print x0”时,我得到的输出是 want 。喜欢:

npoints=10

x0 = np.zeros(npoints)
y0 = np.zeros(npoints)
z0 = np.zeros(npoints)
vx0 = np.zeros(npoints)
vy0 = np.zeros(npoints)
vz0 = np.zeros(npoints)
result=[]
#Set the initial conditions
for step in range(0,len(n1)):
     x0[0] = x1[step]
     y0[0] = y1[step]
     z0[0] = z1[step]

     vx0[0] = vx1[step]
     vy0[0] = vy1[step]
     vy0[0] = vz1[step]



    print x0

以及我希望得到的结果:

[-0.29914467  0.          0.          0.          0.          0.
  0.          0.          0.          0.        ]
[2.24151163 0.         0.         0.         0.         0.
 0.         0.         0.         0.        ]
[-0.01034917  0.          0.          0.          0.          0.
  0.          0.          0.          0.        ]......
[-2.72482266  0.          0.          0.          0.          0.
  0.          0.          0.          0.        ]

我如何在不打印for循环的情况下存储所有这些数组,而不仅仅是最后一个?

1 个答案:

答案 0 :(得分:0)

然后您需要为x0创建2D数组以存储所有像这样的递归值

npoints=10

x0 = np.zeros([len(n1), npoints])
y0 = np.zeros(npoints)
z0 = np.zeros(npoints)
vx0 = np.zeros(npoints)
vy0 = np.zeros(npoints)
vz0 = np.zeros(npoints)
result=[]
#Set the initial conditions
for step in range(0,len(n1)):
    x0[step, 0] = x1[step]
    y0[0] = y1[step]
    z0[0] = z1[step]

    vx0[0] = vx1[step]
    vy0[0] = vy1[step]
    vy0[0] = vz1[step]



print x0