[英]How do I assign to a zero-dimensional numpy array?
Suppose I have an array like x = np.array(3)
which has x.ndim == 0
. 假设我有一个像
x = np.array(3)
的数组,它的x.ndim == 0
。 How do I assign a new value to this array? 如何为该数组分配新值?
x[0] = 2
gives IndexError
, as does x[:] = 2
. x[0] = 2
给出IndexError
, x[:] = 2
。
This can be done using x.flat
which returns an np.flatiter
instance: 可以使用
x.flat
返回一个np.flatiter
实例:
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
我将使用
put
x.put(0,2)
x
Out[91]: array(2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.