0

Is there a keyword for ":" in python indices similar to "Ellipsis" for "..."? In multidimensional arrays, I need to sometimes access an entire dimension and sometimes a single slice. I do this by defining my indices in variables. However, I can save "..." or "Ellipsis" in a variable, but not ":". Slicing with multiple Ellipsis is not possible.

>>> n = numpy.arange(16).reshape(2, 2, 2, 2)
>>> n
array([[[[ 0,  1],
         [ 2,  3]],

        [[ 4,  5],
         [ 6,  7]]],

       [[[ 8,  9],
         [10, 11]],

        [[12, 13],
         [14, 15]]]])
>>> i0,i1,i2,i3 = 1,...,1,1  # same as i0,i1,i2,i3 = 1,Ellipsis,1,1
>>> n[i0,i1,i2,i3]           # this works
array([11, 15])
>>> i0,i1,i2,i3 = 1,...,1,...
>>> n[i0,i1,i2,i3]           # this does not work
IndexError: an index can only have a single ellipsis ('...')
>>> i0,i1,i2,i3 = 1,:,1,:    # this does not work either
SyntaxError: invalid syntax
>>> n[1,:,1,:]               # I want this output with variable indices.
array([[10, 11],
       [14, 15]])

Thanks for the help!

jestubbe
  • 1
  • 1

0 Answers0