Suppose I have an array like x = np.array(3)
which has x.ndim == 0
. How do I assign a new value to this array? x[0] = 2
gives IndexError
, as does x[:] = 2
.
This can be done using x.flat
which returns an np.flatiter
instance:
import numpy as np
x = np.array(3)
x.flat[:] = 2
# or x.flat[0] = 2
or by indexing the original array with ellipsis or an empty tuple:
x[...] = 2
x[()] = 2
I will using put
x.put(0,2)
x
Out[91]: array(2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.