简体   繁体   English

数组数组(Python / NumPy)

[英]Array of arrays (Python/NumPy)

I am using Python/NumPy, and I have two arrays like the following: 我正在使用Python / NumPy,我有两个如下的数组:

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将是一个包含两行和两列的数组,如下所示:

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 ?) 你会编写没有逗号的数组(这是一个MATLAB语法吗?)似乎很奇怪

Have you tried going through NumPy's documentation on multi-dimensional arrays ? 您是否尝试过关于多维数组的NumPy文档

It seems NumPy has a "Python-like" append method to add items to a NumPy n-dimensional array : 似乎NumPy有一个“类似Python”的追加方法来向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
array([[1, 2, 7], [3, 4, 8], [5, 6, 9]])

It has also been answered already ... 已经回答了 ......

From the documentation for MATLAB users : MATLAB用户文档

You could use a matrix constructor which takes a string in the form of a matrix MATLAB literal: 您可以使用矩阵构造函数,它以矩阵MATLAB文字的形式获取字符串:

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. 请试一试,告诉我它是怎么回事。

If the file is only numerical values separated by tabs, try using the csv library: http://docs.python.org/library/csv.html (you can set the delimiter to '\\t') 如果文件只是由制表符分隔的数值,请尝试使用csv库: http//docs.python.org/library/csv.html (您可以将分隔符设置为'\\ t')

If you have a textual file in which every line represents a row in a matrix and has integers separated by spaces\\tabs, wrapped by a 'arrayname = [...]' syntax, you should do something like: 如果你有一个文本文件,其中每一行代表一个矩阵中的一行,并且整数由spaces \\ tabs分隔,用'arrayname = [...]'语法包装,你应该做类似的事情:

import re
f = open("your-filename", 'rb')
result_matrix = []
for line in f.readlines():
    match = re.match(r'\s*\w+\s+\=\s+\[(.*?)\]\s*', line)
    if match is None:
        pass # line syntax is wrong - ignore the line
    values_as_strings = match.group(1).split()
    result_matrix.append(map(int, values_as_strings))

You'll have problems creating lists without commas. 创建没有逗号的列表时会遇到问题。 It shouldn't be too hard to transform your data so that it uses commas as separating character. 转换数据应该不会太难,因此它使用逗号作为分隔字符。

Once you have commas in there, it's a relatively simple list creation operations: 一旦你有逗号,这是一个相对简单的列表创建操作:

array1 = [1,2,3]
array2 = [4,5,6]

array3 = [array1, array2]

array4 = [7,8,9]
array5 = [10,11,12]

array3 = [array3, [array4, array5]]

When testing we get: 测试时我们得到:

print(array3)

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

And if we test with indexing it works correctly reading the matrix as made up of 2 rows and 2 columns: 如果我们使用索引进行测试,它可以正确读取由2行和2列组成的矩阵:

array3[0][1]
[4, 5, 6]

array3[1][1]
[10, 11, 12]

Hope that helps. 希望有所帮助。

a=np.array([[1,2,3],[4,5,6]]) 一个= np.array([[1,2,3],[4,5,6]])

a.tolist() a.tolist()

tolist method mentioned above will return the nested Python list 上面提到的tolist方法将返回嵌套的Python列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM