Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of arrays (Python/NumPy)

Tags:

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.

like image 282
user1609104 Avatar asked Aug 18 '12 17:08

user1609104


People also ask

Can you have an array of arrays NumPy?

We pass a sequence of elements enclosed in a pair of square brackets to the numpy. array() function, and it returns an array containing the exact sequence of elements. The array of arrays, or known as the multidimensional array, can be created by passing arrays in the numpy. array() function.

How do you make an array of arrays in Python?

a(0) = someOtherArray is trying to call a function called a and pass in 0 as an argument. The correct syntax would be a[0] = someOtherArray . arr = [[]] I'm not sure what you're trying to do, python lists is dynamically assigned, but if you want a predefined length and dimension use list comprehensions.

How do you print an array of arrays in Python?

To print an array in Python, use the print() function. The print() is a built-in Python function that takes the name of the array containing the values and prints it. To create an array in Python, use the numpy library and create an array using the np. array() function, and then print that array in the console.

How do you add an array to an array in Python?

Adding elements to an Array using array moduleUsing + operator: a new array is returned with the elements from both the arrays. append(): adds the element to the end of the array. insert(): inserts the element before the given index of the array. extend(): used to append the given array elements to this array.


2 Answers

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.

like image 84
tutuDajuju Avatar answered Oct 22 '22 02:10

tutuDajuju


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:

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

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

Hope that helps.

like image 43
user2750362 Avatar answered Oct 22 '22 02:10

user2750362