我有两个数组:
a = numpy.array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
label = numpy.array(['a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b'])
我正在寻找的是根据以下条件填充零:
If the label[i-1] != label[i]:
pad several zeros (say, 3) to the 'a' array at the same 'i' location
所以,我想要的结果是:
a = numpy.array([ 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 8, 9, 10])
label = numpy.array(['a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b'])
如您所见,数组a
现在在 value 之后有 3 个零7
,它们由标签值更改的条件填充。
我尝试了以下代码:
for i in range(len(a)):
if label[i-1] != label[i]:
a = numpy.pad(a, (0,3), 'constant')
else:
pass
但是,零被填充在a
数组的末尾。正如我怀疑的那样,我应该将填充操作等同于同一个数组,因为它在 for 循环中发生了变化。