如何初始化以特定数字开头的numpy数组?

时间:2019-08-14 23:20:17

标签: python arrays numpy

我可以初始化一个numpy数组,并在创建时对其进行整形。

test = np.arange(32).reshape(4, 8)

产生以下结果:

array([[ 0,  1,  2,  3,  4,  5,  6,  7],
   [ 8,  9, 10, 11, 12, 13, 14, 15],
   [16, 17, 18, 19, 20, 21, 22, 23],
   [24, 25, 26, 27, 28, 29, 30, 31]])

...但是我想知道如何在给定点(例如13而不是0)处开始顺序编号。如何在numpy中完成?

我一直在寻找答案并找到了somewhat similar,但似乎会有一个numpy命令来执行此操作。

1 个答案:

答案 0 :(得分:2)

arange采用可选的start参数。

start = 13 # Any number works here
np.arange(start, start + 32).reshape(4, 8)

# array([[13, 14, 15, 16, 17, 18, 19, 20],
#        [21, 22, 23, 24, 25, 26, 27, 28],
#        [29, 30, 31, 32, 33, 34, 35, 36],
#        [37, 38, 39, 40, 41, 42, 43, 44]])