Home > Enterprise >  how does array.shape[:] works?
how does array.shape[:] works?

Time:02-25

I think I am just confused with how array.shape[] works.
The output of array.shape[:] is the shape of the array in tuple which is fine.output:(4,5)
The output of array.shape[1:] is (5,) and the output of array.shape[:1] is (4,).
I understand that it gives me the size of the columns and rows but why they both show up in the first element of the tuple? shouldn't be like (5,) and (,4)?
Also what does array.shape[:2] means that it gives me the size of the array but array.shape[2:] throw the "not enough values to unpack"?

enter image description here

CodePudding user response:

You got a problem with tuples here, not with array.shape in itself. As pointed out in the comments, x[:1] gives you the first element of a tuple as a singleton tuple, because you are indexing "all" elements of the tuple up to the first (included).

Instead, x[1:] gives you all but the first element of the tuple (a singleton tuple again, if your array is 2D.

This also explain your error: array.shape[2:] has no such elements: dimentions are (4,5): there is no element with index 2 (and surely nothing after that), so you can't unpack 0 values.

  • Related