4

I want to create a dynamic array without size specification. In that array, I need to insert elements at any point I require. The remaining values in them can be either null or undefined till it gets a value assigned to it.

Ex:

a = np.array([])

np.insert(a, any index value, value)

So, if I use np.insert(a, 5, 1) I should get the result as:

array([null, null, null, null, null, 1])
Mazdak
  • 105,000
  • 18
  • 159
  • 188
Lingaselvan
  • 61
  • 1
  • 1
  • 3
  • You can't do that with numpy – hpaulj Oct 16 '17 at 09:00
  • You're bounded by your machine's memory capacity, thus there's no dynamic data structure as you want. But if you want a dynamic data structure that can increases its size util a certain point (maximum capacity of the memory) that's not what you can do with Numpy arrays. – Mazdak Oct 16 '17 at 09:00
  • 2
    You have to create your own data structure for the above implementation. You can initialize your array of some size with `None` and implement insert in required fashion. You can extend the array by doubling it if the user is accessing the index outside the limit of your data structure. refer [this](https://www.happybearsoftware.com/implementing-a-dynamic-array). – Harman Oct 16 '17 at 09:06
  • How about using a dictionary? – hpaulj Oct 16 '17 at 11:17
  • You do have the standard [array](https://docs.python.org/3/library/array.html#module-array) lib in Python which, for all intents and purposes, is a dynamic array. As for the specific behavior you gave to `insert` I doubt it to be valid (in other words, I don't think insert will add nulls automatically). My advice is for you to make your own implementation storing a numpy array (and using its methods to obtain your required behavior). – armatita Oct 16 '17 at 11:38

2 Answers2

3

In MATLAB/Octave you can create and extend a matrix with indexing:

>> a = []
a = [](0x0)
>> a(5) = 1
a =
   0   0   0   0   1

That is if you index a slot beyond the current end, it expands the matrix, and fills it with 0s.

Javascript (in a Nodejs session) does something similar

> var a = [1,2];
undefined
> a
[ 1, 2 ]
> a[6] = 1
1
> a
[ 1, 2, , , , , 1 ]
> a[3]
undefined

Leaving the intermediate slots undefined.

A Python dictionary can grow simply by indexing

In [113]: a = {0:1, 1:2}
In [114]: a[5]=1
In [115]: a
Out[115]: {0: 1, 1: 2, 5: 1}
In [116]: a[3]
...
KeyError: 3
In [117]: a.get(3,None)

Dictionaries also implement a setdefault, and a defaultdict.

A Python list grows by append and extend

In [120]: a = [1,2]
In [121]: a.append(3)
In [122]: a
Out[122]: [1, 2, 3]
In [123]: a.extend([0,0,0,1])
In [124]: a
Out[124]: [1, 2, 3, 0, 0, 0, 1]

Lists also change size with del and sliced assignment, e.g. a[1:2] = [0,0,0,2].

A numpy array is fixed in size. To grow one, you have to make a new array by concatenation

In [125]: a = np.arange(3)
In [126]: a
Out[126]: array([0, 1, 2])
In [127]: a = np.concatenate((a, [0,0,1]))
In [128]: a
Out[128]: array([0, 1, 2, 0, 0, 1])

Array functions like append, stack, delete and insert use some form of concatenate or allocate-n-fill.

In restricted cases an array can be resized (but this method is not used very often):

In [161]: a.resize(10)
In [162]: a[-1]=10
In [163]: a
Out[163]: array([ 0,  1,  2,  0,  0,  1,  0,  0,  0, 10])
hpaulj
  • 221,503
  • 14
  • 230
  • 353
2

Check out this package.

from numpy_da import DynamicArray

data = DynamicArray(shape=2, index_expansion=True)
data[5] = 1
print(data)  # [0, 0, 0, 0, 0, 1]

index_expansion option allows the array to grow if you index beyond the current largest index.

Flair
  • 2,609
  • 1
  • 29
  • 41
basil_man
  • 322
  • 5
  • 14