比如我有个3x3的numpy ndarray,我有一个list [1, 2, 3],怎么把这个list插入到array中作为最后一行?
2个回答
用np.vstack就可以了
np.vstack([a, new_row])
例如
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> new_row = [1, 5, 9]
>>> b = np.vstack([a, new_row])
>>> b
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 5, 9]])
SofaSofa数据科学社区DS面试题库 DS面经>>> import numpy as np
>>> a=np.array([[1,2],[3,4]])
>>> b=np.array([[5,6],[7,8]])
>>> c=np.concatenate((a,b),axis=0)
>>> c
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
>>> c=np.concatenate((a,b),axis=1)
>>> c
array([[1, 2, 5, 6],
[3, 4, 7, 8]])
>>>
谢大哥!
-
ggg818
2018-04-16 11:20