我是 Python 的新手。在练习期间,我应该使用掩码将以下列表中所有低于 100 的值乘以 2:
a = np.array([230, 10, 284, 39, 76])
Run Code Online (Sandbox Code Playgroud)
所以我写了下面的代码:
import numpy as np
a = np.array([230, 10, 284, 39, 76])
cut = 100
a[a < cut] = a*2
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误:
IndexError: index 230 is out of bounds for axis 0 with size 5
这令人困惑,因为根据我的理解,ain[a < cut]实际上指的是 array 中的每个值a,但aina*2指的是整个数组。
如何使用屏蔽方法而不是使用循环更正此代码?