How comma separated range mechanism works in Python? -


this question has answer here:

code:

import numpy np  z = np.array([[1,3,5],[2,4,6]])  print(z[0:, :2]) 

answer:

[[1, 3] [2, 4]] 

i python beginner, solving interactive exercise when above mentioned question appeared.

i not able understand, how z[0:, :2] works in case? if possible, please me understand scenario.

you can read numpy slicing , indexing here:

https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

in case, 0: means "all rows, starting (and including) row 0 , going until end" (you use equivalent :, means "all rows, starting beginning , going until end").

:2 means "all columns, starting beginning , going until (but not including) column 2".

together, z[0:, :2] means "the part of z includes rows , first 2 columns". first dimension listed rows, , second columns. if array 3d, include yet dimension comma, , on.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -