Jam*_*een 3 python numpy vector
我有两个向量x,y长度相同,用 NumPy 定义。
如何遍历x和修改 中的值y?
我的意思是像
ingredients = empty(cakes.shape)
for ingredient, cake in np.nditer([ingredients,cakes]):
ingredient = cake * 2 + 2
Run Code Online (Sandbox Code Playgroud)
正如其他人所说,使用矢量化通常更好/更快/更好/...
但是如果你有充分的理由使用迭代,你当然可以这样做。
我刚刚从官方文档中复制了这个:
>>> a = np.arange(6).reshape(2,3)
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> for x in np.nditer(a, op_flags=['readwrite']):
... x[...] = 2 * x
...
>>> a
array([[ 0, 2, 4],
[ 6, 8, 10]])
Run Code Online (Sandbox Code Playgroud)