numpy.s_#
- numpy.s_=<numpy.lib._index_tricks_impl.IndexExpressionobject>#
A nicer way to build up index tuples for arrays.
Note
Use one of the two predefined instances
index_expors_rather than directly usingIndexExpression.For any index combination, including slicing and axis insertion,
a[indices]is the same asa[np.index_exp[indices]]for anyarraya. However,np.index_exp[indices]can be used anywherein Python code and returns a tuple of slice objects that can beused in the construction of complex index expressions.- Parameters:
- maketuplebool
If True, always returns a tuple.
See also
s_Predefined instance without tuple conversion:s_ = IndexExpression(maketuple=False). The
index_expis another predefined instance that always returns a tuple:index_exp = IndexExpression(maketuple=True).
Notes
You can do all this with
sliceplus a few special objects,but there’s a lot to remember and this version is simpler becauseit uses the standard array indexing syntax.Examples
>>>importnumpyasnp>>>np.s_[2::2]slice(2, None, 2)>>>np.index_exp[2::2](slice(2, None, 2),)
>>>np.array([0,1,2,3,4])[np.s_[2::2]]array([2, 4])