优雅的方式来创建等间距数组的列表

时间:2021-11-15 20:23:41

I have a vector r0 defined via r0 = np.r_[0, 0, 1].

我有一个通过r0 = np.r_ [0,0,1]定义的向量r0。

I can define its mirror image r0 = -r0, which is np.r_[0, 0, -1].

我可以定义它的镜像r0 = -r0,即np.r_ [0,0,-1]。

I would like to create a list of r0, like this:

我想创建一个r0列表,如下所示:

0, 0, -1
0, 0, -1 + step
0, 0, -1 + 2*step
...
0, 0, 1

as an array of 3 columns and (1-(-1))/step lines.

作为3列和(1 - ( - 1))/步骤行的数组。

I can only think of doing this with a list and then turning it into an array, but I'd really like to keep working with arrays since I converted all my code to vectors.

我只能想到用列表做这个然后把它变成一个数组,但我真的想继续使用数组,因为我将所有代码转换为向量。

2 个解决方案

#1


3  

You appear to be looking for np.linspace:

您似乎在寻找np.linspace:

>>> z_start = -1
>>> z_stop = 1
>>> step = 0.25
>>> np.linspace(z_start, z_stop, num=1+(z_stop-z_start)/step)
array([-1.  , -0.75, -0.5 , -0.25,  0.  ,  0.25,  0.5 ,  0.75,  1.  ])

Packing them back into an array:

将它们装回阵列:

>>> z = np.linspace(z_start, z_stop, num=1+(z_stop-z_start)/step)
>>> n = len(z)
>>> np.vstack([np.zeros(n), np.zeros(n), z]).T
array([[ 0.  ,  0.  , -1.  ],
       [ 0.  ,  0.  , -0.75],
       [ 0.  ,  0.  , -0.5 ],
       [ 0.  ,  0.  , -0.25],
       [ 0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.25],
       [ 0.  ,  0.  ,  0.5 ],
       [ 0.  ,  0.  ,  0.75],
       [ 0.  ,  0.  ,  1.  ]])

#2


0  

You can create one numpy array and have a list of views for parts of the data.

您可以创建一个numpy数组,并为部分数据提供视图列表。

import numpy as np

step = 10
r = np.zeros((step, 3))
r[:, -1] = np.linspace(-1, 1, step)

r0 = [r[i] for i in range(r.shape[0])]

# r0 == [array([ 0.,  0., -1.]),
#  array([ 0.,  0., -0.77777778]),
#  array([ 0.,  0., -0.55555556]),
#  array([ 0.,  0., -0.33333333]),
#  array([ 0.,  0., -0.11111111]),
#  array([ 0.,  0.,  0.11111111]),
#  array([ 0.,  0.,  0.33333333]),
#  array([ 0.,  0.,  0.55555556]),
#  array([ 0.,  0.,  0.77777778]),
#  array([ 0.,  0.,  1.])]

#1


3  

You appear to be looking for np.linspace:

您似乎在寻找np.linspace:

>>> z_start = -1
>>> z_stop = 1
>>> step = 0.25
>>> np.linspace(z_start, z_stop, num=1+(z_stop-z_start)/step)
array([-1.  , -0.75, -0.5 , -0.25,  0.  ,  0.25,  0.5 ,  0.75,  1.  ])

Packing them back into an array:

将它们装回阵列:

>>> z = np.linspace(z_start, z_stop, num=1+(z_stop-z_start)/step)
>>> n = len(z)
>>> np.vstack([np.zeros(n), np.zeros(n), z]).T
array([[ 0.  ,  0.  , -1.  ],
       [ 0.  ,  0.  , -0.75],
       [ 0.  ,  0.  , -0.5 ],
       [ 0.  ,  0.  , -0.25],
       [ 0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.25],
       [ 0.  ,  0.  ,  0.5 ],
       [ 0.  ,  0.  ,  0.75],
       [ 0.  ,  0.  ,  1.  ]])

#2


0  

You can create one numpy array and have a list of views for parts of the data.

您可以创建一个numpy数组,并为部分数据提供视图列表。

import numpy as np

step = 10
r = np.zeros((step, 3))
r[:, -1] = np.linspace(-1, 1, step)

r0 = [r[i] for i in range(r.shape[0])]

# r0 == [array([ 0.,  0., -1.]),
#  array([ 0.,  0., -0.77777778]),
#  array([ 0.,  0., -0.55555556]),
#  array([ 0.,  0., -0.33333333]),
#  array([ 0.,  0., -0.11111111]),
#  array([ 0.,  0.,  0.11111111]),
#  array([ 0.,  0.,  0.33333333]),
#  array([ 0.,  0.,  0.55555556]),
#  array([ 0.,  0.,  0.77777778]),
#  array([ 0.,  0.,  1.])]