首先,请参阅以下示例代码:
Name i j k Final
A 1 0 3 1
B 0 5 4 5
C 0 0 4 4
D 0 5 <-- this one is tricky. We do count the null for j column here.
然而,显然这不能改变数组import numpy as np
a = np.arange(100)
ma = (a > 10)
# for some reason, I need to assign values to first 10 elements of the masked array a.
a[ma][0:10] = -999
内的值。最后,我通过引入另一个中介数组a
来完成它。例如:
b = a[ma]
实际上在我的复杂情况下,数组b = a[ma]
b[0:10] = -999
a[ma] = b
In [110]: a
Out[110]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
-999, -999, -999, -999, -999, -999, -999, -999, -999, -999, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
99])
可能非常大。所以它花了很多内存空间。有没有干净的方法呢?