Python NumPy Arrays
kodingwindow@kw:~$ python3 ... >>> from numpy import * >>> arr1 = array([40, 96, 24, 55, 78, -111]) >>> arr2 = array(['x','y','z']) >>> arr3 = array(["Orange", "Pineapple", "Banana", "Blueberry", "Grapes", "Fig"]) >>> arr1.size, arr2.size, arr3.size (6, 3, 6) >>> arr1.dtype, arr2.dtype, arr3.dtype (dtype('int64'), dtype('<U1'), dtype('<U9'))# memory size of an element stored in the array >>> arr1.itemsize, arr2.itemsize, arr3.itemsize (8, 4, 36)# total memory size of the array >>> arr1.nbytes, arr2.nbytes, arr3.nbytes (48, 12, 216)# number of dimensions of the array >>> arr1.ndim, arr2.ndim, arr3.ndim (1, 1, 1) >>> arr1[0], arr2[0], arr3[0] (40, 'x', 'Orange') >>> for i in arr1: ... print(i) ... 40 96 24 55 78 -111 >>> for s in arr3: ... print(s) ... Orange Pineapple Banana Blueberry Grapes Fig
Python NumPy Array Methods
>>> from numpy import * >>> arr1 = array([40, 96, 24, 55, 78, -111]) >>> arr2 = array(['x','y','z']) >>> arr3 = array(["Orange", "Pineapple", "Banana", "Blueberry", "Grapes", "Fig"]) >>> len(arr1), len(arr2), len(arr3) (6, 3, 6) >>> min(arr1), min(arr2), min(arr3) (-111, 'x', 'Banana') >>> max(arr1), max(arr2), max(arr3) (96, 'z', 'Pineapple') >>> sort(arr1) array([-111, 24, 40, 55, 78, 96]) >>> sort(arr2) array(['x', 'y', 'z'], dtype='<U1') >>> sort(arr3) array(['Banana', 'Blueberry', 'Fig', 'Grapes', 'Orange', 'Pineapple'], dtype='<U9') >>> arr2 = append(arr2, 'w') >>> arr2 array(['x', 'y', 'z', 'w'], dtype='<U1') >>> arr2 = delete(arr2, 3) >>> arr2 array(['x', 'y', 'z'], dtype='<U1') >>> arr3 = insert(arr3, 0, "Cherry") >>> arr3 array(['Cherry', 'Orange', 'Pineapple', 'Banana', 'Blueberry', 'Grapes', 'Fig'], dtype='<U9') >>> arr3 = delete(arr3, 0) >>> arr3 array(['Orange', 'Pineapple', 'Banana', 'Blueberry', 'Grapes', 'Fig'], dtype='<U9') >>> arr1.tolist() [40, 96, 24, 55, 78, -111]
What Next?
Python to create a view() and copy() of NumPy arrays
Python NumPy Arrays Comparison
Python NumPy Mathematical Functions for Arrays
Advertisement