B
B
berng2021-08-22 17:59:31
Python
berng, 2021-08-22 17:59:31

Python/numpy: how to increase an array by one row without using extra memory?

There is a large numpy array, very large - barely fits in RAM.
Is there a way to quickly append a newline to it without using extra memory or copying the variable? It is to add to an existing array without allocating additional memory for its copy. There is no way to convert to something, you need exactly numpy.array.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-08-22
@berng

It is forbidden. This is the difference between an array and a list. A list is easier to expand than an array, because an array stores all of its data in one block, while a list only stores references to its elements in one block.
You need to either allocate an array with a margin, or flush the data to disk for later reading into a numpy array.

W
Wataru, 2021-08-22
@wataru

No.
To do what you want - you need the program to grab the memory immediately after the array, so that there is where to stretch it.
This can't be done even in C++, let alone Python.
Edit: Generally speaking, C++ has realloc, but it does not guarantee that an existing memory area will be extended. Because she might be busy with something else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question