use of ellipsis in modifying numpy arrays

后端 未结 1 795
清酒与你
清酒与你 2021-01-29 07:40

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

相关标签:
1条回答
  • 2021-01-29 07:55

    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;
    
    0 讨论(0)
提交回复
热议问题