数组数组 (Python/NumPy) [英] Array of arrays (Python/NumPy)

查看:30
本文介绍了数组数组 (Python/NumPy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python/NumPy,我有两个如下所示的数组:

array1 = [1 2 3]数组 2 = [4 5 6]

我想创建一个新数组:

array3 = [[1 2 3], [4 5 6]]

并将项目附加到它.例如,如果要追加的新项目是:

array4 = [7 8 9]数组 5 = [10 11 12]

那么现在 array3 将是一个两行两列的数组,如下所示:

array3= [[1 2 3], [4 5 6][7 8 9], [10 11 12]]

我好像有问题,因为我的数组元素没有用逗号分隔.

解决方案

编写不带逗号的数组似乎很奇怪(是 MATLAB 语法?)

您是否尝试过查看 NumPy 关于多维数组的文档?

似乎 NumPy 有一个 类似Python"的append方法将项目添加到NumPy n维数组:

<预><代码>>>>p = np.array([[1,2],[3,4]])>>>p = np.append(p, [[5,6]], 0)>>>p = np.append(p, [[7],[8],[9]],1)>>>p数组([[1, 2, 7], [3, 4, 8], [5, 6, 9]])

它也已经得到答复...

来自 MATLAB 用户文档:

您可以使用矩阵构造函数,该构造函数采用矩阵 MATLAB 文字形式的字符串:

mat("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 two rows and two 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.

解决方案

It seems strange that you would write arrays without commas (is that a MATLAB syntax?)

Have you tried going through NumPy's documentation 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...

From the documentation 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.

这篇关于数组数组 (Python/NumPy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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