I saw the following code here, which tries to iterate a numpy array arr and modify its elements. However, I do not quite understand what the purpose of using ellipsis (...) is h
Ellipsis ...
is a built-in Python symbol that is used to specify slices in N-dimensional Numpy arrays, such as a[0,...,0]
is equivalent to a[0,:,:,:,0]
for a 5-D array a
.
The nditer
objects use this syntax to make iterators writable.
Using an iterator object as an lvalue modifies the object itself instead of the location in an array it is referring to. So x[...]
is the syntax that is used for dereferencing an iterator.
You could also use this syntax to access the value for reading, but this is redundant.
Also note that your syntax for op_flags is incorrect. It should be a list or a tuple.
for x in np.nditer(arr,op_flags=['readwrite']):
x[...] = x*2;