Arrays

class pydatastructs.OneDimensionalArray(dtype=<class 'NoneType'>, *args, **kwargs)

Represents one dimensional static arrays of fixed size.

Parameters
  • dtype (type) – A valid object type.

  • size (int) – The number of elements in the array.

  • elements (list) – The elements in the array, all should be of same type.

  • init (a python type) – The inital value with which the element has to be initialized. By default none, used only when the data is not given.

Raises

ValueError – When the number of elements in the list do not match with the size. More than three parameters are passed as arguments. Types of arguments is not as mentioned in the docstring.

Note

At least one parameter should be passed as an argument along with the dtype.

Examples

>>> from pydatastructs import OneDimensionalArray
>>> arr = OneDimensionalArray(int, 5)
>>> arr.fill(6)
>>> arr[0]
6
>>> arr[0] = 7.2
>>> arr[0]
7

References

1

https://en.wikipedia.org/wiki/Array_data_structure#One-dimensional_arrays

__getitem__(i)
__setitem__(idx, elem)
fill(elem)
__len__()
__module__ = 'pydatastructs.linear_data_structures.arrays'
class pydatastructs.MultiDimensionalArray(dtype: type = <class 'NoneType'>, *args, **kwargs)

Represents a multi-dimensional array.

Parameters
  • dtype (type) – A valid object type.

  • *args (int) – The dimensions of the array.

Raises
  • IndexError – Index goes out of boundaries, or the number of index given is not the same as the number of dimensions.

  • ValueError – When there’s no dimensions or the dimension size is 0.

Examples

>>> from pydatastructs import MultiDimensionalArray as MDA
>>> arr = MDA(int, 5, 6, 9)
>>> arr.fill(32)
>>> arr[3, 0, 0]
32
>>> arr[3, 0, 0] = 7
>>> arr[3, 0, 0]
7

References

1

https://en.wikipedia.org/wiki/Array_data_structure#Multidimensional_arrays

__getitem__(indices)
__setitem__(indices, element) None
fill(element) None
property shape: tuple
__module__ = 'pydatastructs.linear_data_structures.arrays'
class pydatastructs.DynamicOneDimensionalArray(dtype=<class 'NoneType'>, *args, **kwargs)

Represents resizable and dynamic one dimensional arrays.

Parameters
  • dtype (type) – A valid object type.

  • size (int) – The number of elements in the array.

  • elements (list/tuple) – The elements in the array, all should be of same type.

  • init (a python type) – The inital value with which the element has to be initialized. By default none, used only when the data is not given.

  • load_factor (float, by default 0.25) – The number below which if the ratio, Num(T)/Size(T) falls then the array is contracted such that at most only half the positions are filled.

Raises

ValueError – When the number of elements in the list do not match with the size. More than three parameters are passed as arguments. Types of arguments is not as mentioned in the docstring. The load factor is not of floating point type.

Note

At least one parameter should be passed as an argument along with the dtype. Num(T) means the number of positions which are not None in the array. Size(T) means the maximum number of elements that the array can hold.

Examples

>>> from pydatastructs import DynamicOneDimensionalArray as DODA
>>> arr = DODA(int, 0)
>>> arr.append(1)
>>> arr.append(2)
>>> arr[0]
1
>>> arr.delete(0)
>>> arr[0]
>>> arr[1]
2
>>> arr.append(3)
>>> arr.append(4)
>>> [arr[i] for i in range(arr.size)]
[None, 2, 3, 4, None, None, None]

References

1

http://www.cs.nthu.edu.tw/~wkhon/algo09/lectures/lecture16.pdf

append(el)
delete(idx)
property size
__str__()

Return str(self).

__reversed__()
__module__ = 'pydatastructs.linear_data_structures.arrays'