Notepad/enter/Coding Tips (Classical)/Terminal Tips/2. CLI Tools/Languages/Python/code/arrays/About arrays.md

1.6 KiB

Arrays in Python

Arrays in python are a largely different data structure entirely than the more commonly used list, and different still than the pythonic dictionary. Arrays are different than lists than in the sense that they are used for entirely different purposes as lists are more iterable and arrays have the advantage of being of fixed size.

In python3, they are most commonly called by using numpy array.

It is used by creating an np.array([]) call.


#finding max of an array 

0-D array

This is simply the same as just having one object

import numpy as np 
arr = np.array(42)
print(arr)

1-D array

This is a more classic array that we can set of fixed size.

import numpy as np 
arr = np.arr([1, 2, 3, 4, 5])
print(arr)

2-D array

Arrays that have 1-D arrays as its elements and are often used to represent matrix or 2nd order tensors. Numpy has a sub module called numpy.mat to deal with such operations.

import numpy as np  
  
arr = np.array([[1, 2, 3], [4, 5, 6]])  
  
print(arr)

Higher DImensional arrays

Create a defined number of dimensions by using ndmin argument like so:

A 5 dimensional array:


import numpy as np  
  
arr = np.array([1, 2, 3, 4], ndmin=5)  
  
print(arr)  
print('number of dimensions :', arr.ndim)

To check the number of dimensions

Simply use the ndim attribute on an object.