I can use array.resize(shape) to resize my array and have zeros added to those indices without any value. If my array is [1,2,3,4] and I use array.resize[5,0] I get [1,2,3,4,0]. How can I append / pad the zeros to the front, yielding [0,1,2,3,4]?
I am doing this dynamically – trying to use:
array.resize(arrayb.shape)
I would like to avoid (at all costs) making an in-memory copy of the array. That is to reverse the array, resize, and reverse again. Working with a view would be ideal.
I believe you can use slice assignment to do this. I see no reason why
numpywould need to make a copy for an operation like this, as long as it does the necessary checks for overlaps (though of course as others have noted,resizemay itself have to allocate a new block of memory). I tested this method with a very large array, and I saw no jump in memory usage.The following showed no jump in memory usage for the assignment operation:
I don’t know of a better way, and this is just a conjecture. Let me know if it doesn’t work.