数组的数组(蟒蛇/ numpy的) [英] Array of arrays (python/numpy)
问题描述
我使用python / numpy的,我有两个数组类似如下:
ARRAY1 = [1 2 3]
数组2 = [4 5 6]
和我想创建一个新的数组:
ARRAY3 = [[1 2 3],[4 5 6]
和追加的项目吧。因此,例如,如果新的项追加有:
array4 = [7 8 9]
array5 = [10 11 12]
然后现在ARRAY3将与2行2列象一个如下所示的阵列
ARRAY3 = [[1 2 3] [4 5 6]
[7 8 9],[10 11 12]]
我似乎有问题,因为我的数组的元素是不能用逗号分隔。
似乎很奇怪,你会写阵列没有逗号(是,的 MATLAB语法?)
您是否尝试过要通过 numpy的的文件建立多维数组一>
这似乎numpy的有<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html?highlight=append#numpy.append\"相对=nofollow>蟒蛇般的Append方法将项目添加到numpy的n维数组:
p值= np.array([1,2],[3,4])
p值= np.append(ρ,[5,6],0)
p值= np.append(ρ,[7],[8],[9],1)
P
阵列([[1,2,7],[3,4,8],[5,6,10]])
块引用>
块引用>这也被已经回答 ...
编辑:从网站MATLAB用户:
你可以使用一个矩阵的构造,它接受一个字符串矩阵MATLAB文字的形式:
垫(1 2 3 4 5 6)
或
矩阵([1 2 3 4 5 6])
块引用>请给它一个尝试,并告诉我如何去
I am using python/numpy, and I have two arrays like the following:
array1=[1 2 3] array2=[4 5 6]
And I would like to create a new array:
array3=[[1 2 3],[4 5 6]]
and append items to it. So for example if the new items to append are:
array4=[7 8 9] array5=[10 11 12]
Then now array3 would be an array with 2 rows and 2 columns like the one shown below:
array3=[[1 2 3], [4 5 6] [7 8 9], [10 11 12]]
I seem to have problems because the elements of my arrays are not separated by commas.
解决方案Seems strange that you would write arrays without commas (is that a matlab syntax?)
Have you tried going through numpy's documentaion on multi-dimensional arrays?
It seems numpy has a "python-like" append method to add items to a numpy n-dimensional array:
p = np.array([[1,2],[3,4]])
p = np.append(p, [[5,6]], 0)
p = np.append(p, [[7],[8],[9]],1)
p
array([[1, 2, 7], [3, 4, 8], [5, 6, 9]])
It has also been answered already...
Edit: from the site for matlab users: You could use a matrix constructor which takes a string in the form of a matrix matlab literal:
mat("1 2 3; 4 5 6")
or
matrix("[1 2 3; 4 5 6]")
Please give it a try and tell me how it goes
这篇关于数组的数组(蟒蛇/ numpy的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!