You can use the slice function and call it with the appropriate list of variables at runtime as follows:
Example:
>>> from numpy import * >>> a = (1, 2, 3) >>> b = arange(27).reshape(3, 3, 3) >>> s = slice(*a) >>> b[s] array([[[ 9, 10, 11], [12, 13, 14], [15, 16, 17]]])
This is the same as:
>>> b[1:2:3] array([[[ 9, 10, 11], [12, 13, 14], [15, 16, 17]]])
Finally, the equivalent of not specifying anything between 2 :
in conventional notation, is to put None
in those places in the created tuple.
Dhara source share