Slicing

Arrays can be sliced just like lists and tuples. Array slicing is identical to list slicing, except that the syntax is simpler. Arrays are sliced using the [ : , :, ... :] syntax, where the number of dimensions of the arrays determine the size of the slice, except that these dimensions for which slices are omitted, all elements are selected. For example, if b is a three-dimensional array, b[0:2] is the same as b[0:2,:,:]. There are shorthand notations for slicing. Some common ones are:

  • : and: are the same as 0:n:1, where n is the length of the array
  • m: and m:n: are the same as m:n:1, where n is the length of the array
  • :n: is the same as 0:n:1
  • ::d: is the same as 0:n:d, where n is the length of the array

All these slicing methods have been referenced ...

Get Python: Data Analytics and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.