-1

What is the meaning of ... in a python array? The code below is what it was written like.

 obj = target[..., 0 ]

Please help me!

Regina Kang
  • 135
  • 2
  • 11
  • 1
    Does this answer your question? [What do ellipsis \[...\] mean in a list?](https://stackoverflow.com/questions/17160162/what-do-ellipsis-mean-in-a-list) – Ulrich Eckhardt Dec 26 '21 at 17:05
  • To clarify: is this part of your code, or something your program printed out? Are you using numpy, or something like it? – Nick ODell Dec 26 '21 at 17:07
  • It was part of someone's code and I wanted to figure it out. Numpy was used. – Regina Kang Dec 26 '21 at 17:10
  • I guess this doesn't mean a loop as in the link above. – Regina Kang Dec 26 '21 at 17:11
  • 1
    Does this answer your question? [How do you use the ellipsis slicing syntax in Python?](https://stackoverflow.com/questions/118370/how-do-you-use-the-ellipsis-slicing-syntax-in-python) – Bharel Dec 26 '21 at 17:20

2 Answers2

0

Ellipsis expands to all other dimensions.

From numpy's documentation:

Ellipsis expands to the number of : objects needed for the selection tuple to index all dimensions.

Example for a 3 dimensional shape:

>>> x = np.array([[[1],[2],[3]], [[4],[5],[6]]])
>>> x.shape
(2, 3, 1)
>>> x[...,0]
array([[1, 2, 3],
      [4, 5, 6]])

You can see that the first item of the third dimension was picked across all others. Since it contains only 1 item and is not a slice, it was expanded.

Bharel
  • 23,672
  • 5
  • 40
  • 80
0

In numpy arrays, an ellipsis (...) is the equivalent of a column (:) in that they allow array slicing like in the example below where I create a 2D numpy array and print its columns as 1D array:

import numpy as np

x = np.array(range(12)).reshape(3,4)
print(f'x = {x}')
print(f'\nSlicing with ellipsis \t Slicing with column')
for i in range(x.shape[1]):
    print(f'{x[...,i]} \t\t {x[:,i]}')

The output of this code is:

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

Slicing with ellipsis    Slicing with column
[0 4 8]                  [0 4 8]
[1 5 9]                  [1 5 9]
[ 2  6 10]               [ 2  6 10]
[ 3  7 11]               [ 3  7 11]

The same could be done on rows using x[i,:] or its equivalent x[i,...].

tem
  • 101
  • 4