【发布时间】:2021-04-21 01:04:50
【问题描述】:
假设我有如下的 np.array
dat = array([[ 0, 1, 0],
[ 1, 0, 0],
[0, 0, 1]]
)
我想要做的是将(行的索引 + 1)作为新列添加到该数组中,就像
newdat = array([[ 0, 1, 0, 1],
[ 1, 0, 0, 2],
[0, 0, 1, 3]]
)
我应该如何做到这一点。
【问题讨论】:
-
concatenate是你的朋友。阅读它的文档 -
这行得通吗?
np.hstack([dat, np.linspace(1, dat.shape[0], dat.shape[0]).reshape((-1, 1))]) -
np.c_[dat,1:len(dat)+1]
标签: python python-3.x list numpy numpy-ndarray