NumPy Arrays
The central feature of NumPy is the array object class. Arrays are similar to lists in Python, except that every element of an array must be of the same type, typically a numeric type like float or int. Arrays make operations with large amounts of numeric data very fast and are generally much more efficient than lists.
Array slicing works with multiple dimensions in the same way, as usual, applying each slice specification as a filter to a specified dimension. Use of a single ":" in a dimension indicates the use of everything along that dimension.
>>> a = np.array([[1, 2, 3], [4, 5, 6]], float) >>> a[1,:] array([ 4., 5., 6.]) >>> a[:,2] array([ 3., 6.])
The shape property of an array returns a tuple with the size of each array dimension.
>>> a.shape (2, 3)
The dtype property tells you what type of values are stored by the array.
>>> a.dtype dtype('float64')
When used with an array, the len function returns the length of the first axis.
>>> a = np.array([[1, 2, 3], [4, 5, 6]], float) >>> len(a) 2
The in statement can be used to test if values are present in an array:
>>> a = np.array([[1, 2, 3], [4, 5, 6]], float) >>> 2 in a True >>> 0 in a False