Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python slicing replace : with a variable [duplicate]

Tags:

python

numpy

I want to replace the column slicing of a Numpy Array by a variable. e.g.

a = np.zeros((3, 3))
slic = 3
a[:, slic]

So how do I represent : case where:

a[:, slic] == a[:, :]

so what should slic = ?

Many thanks

J

like image 470
J_yang Avatar asked May 24 '26 12:05

J_yang


1 Answers

As mentioned in the comment, the colon notation inside brackets generates an internal slice(start, end, step) object. Your specific slic should therefore be set to slice(None). See here for details.

Note that numpy slices can generally be an integer, a slice object, an Ellipsis, a newaxis, or a tuple of the above. You could therefore also assign slic to the tuple (slice(None), slice(None)) and pass that in instead. See here for numpy details.

like image 115
Uri Granta Avatar answered May 27 '26 01:05

Uri Granta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!