数组的数组(蟒蛇/ numpy的) [英] Array of arrays (python/numpy)

查看:184
本文介绍了数组的数组(蟒蛇/ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆