简体   繁体   中英

How to create empty numpy array of arrays

I want to have numpy array, each array element will be another array (or python list) of ints.

import numpy as np
arr = np.empty(2, dtype=int) # what to put here? int? list?

arr = np.append(arr, [0,1]) # there will be always exactly two numbers added
arr = np.append(arr, [1,1])
arr = np.append(arr, [5,1])
# and so on - I want to append undefined amount of times, these numbers are computed at runtime

what I am getting:

0: 0
1: 1
2: 1
3: 1
4: 5
5: 1

What I want to get:

0: [0,1]
1: [1,1]
2: [5,1]

The main point here is that I want to iterate afterwards like:

for element in arr:
    x = element[0]
    y = element[1]
    # do something with x and y here

As this is already implemented, and I dont want to refactor very long code. Could you please show me any solution, how to correctly initialize this array, and which (tuples, lists, numpy arrays) should store there integers, to make it as fast as possible?

Thank you

You need to append the two numbers inside another list because of the second dim.

arr = np.empty((0,2),int)
arr = np.append(arr, [[0, 1]], axis=0)
arr = np.append(arr, [[1, 1]], axis=0)
arr = np.append(arr, [[5, 1]], axis=0)

arr should now be:

array([[0, 1],
       [1, 1],
       [5, 1]])

You can use deque and then convert it to numpy array

from collections import deque
import numpy as np
lists = deque()
lists.append([0,1])
lists.append([1,1])
lists.append([5,1])
results = np.array(lists)
results

Output

array([[0, 1],
       [1, 1],
       [5, 1]])

Repeated array append has a couple of problems:

arr = np.append(arr, [[0, 1]], axis=0)

Setting the initial arr value requires some understanding of array shapes. It's not as simple as np.array([]) . And each append (really a np.concatenate ) makes a whole new array, with full data copying. List append on the other hand just adds a reference to the existing list.

So if you need to make an array iteratively, list append is generally recommended:

In [10]: alist = []
    ...: for row in [[0,1],[1,1],[5,1]]:
    ...:     alist.append(row)
    ...: arr = np.array(alist)
    ...: 

In [11]: alist
Out[11]: [[0, 1], [1, 1], [5, 1]]
In [12]: arr
Out[12]: 
array([[0, 1],
       [1, 1],
       [5, 1]])

An alternative is to start with a arr=np.zeros((3,2),int) and assign successive arr[i] rows. Speeds about the same.

But if you are going to iterate row by row, you might as well stick with the list - it will be faster:

In [13]: for row in alist:
    ...:     x,y = row
    ...:     print(x,y,x+y)
    ...: 
0 1 1
1 1 2
5 1 6

If you go to the work of creating an array, use it as such:

In [14]: arr.sum(axi  s=1)
Out[14]: array([1, 2, 6])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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