<p>如果你想一想,你必须循环所有13500*12000项至少一次,以计算总和。你不能低于这个值,所以你应该尝试在13500*12000次迭代中求解它。这意味着,您应该只迭代原始数组中的项,并且只迭代一次。你知道吗</p>
<p>为了计算和,应该避免使用会再次迭代的东西,所以不要使用<code>sum</code>。相反,您可以直接添加到目标数组中,因为它是使用<code>np.zeros</code>初始化为零的。你知道吗</p>
<pre><code>experiment2 = np.zeros(shape=(13500,2400))
for row in range(13500):
for col in range(12000):
# calculate the target indexes for `experiment2`. To keep the complexity
# linear to the original array size, this has to be done in constant time
# using only the indexes we have
evenIndex = (col / 10) * 2
oddIndex = ((col + 5) / 10 ) * 2 - 1
# add the numbers
expirement2[row, evenIndex] += expirement[row, col]
# only add odd if we’re past 4 though, and before 11995
if 4 < col < 11995:
expirement2[row, oddIndex] += expirement[row, col]
</code></pre>
<p>这将在O(N)中工作,因为N是输入数组的总大小,只进行一次迭代。如前所述,如果要查看所有数字,就不能低于此值。你知道吗</p>
<p>请注意,此解决方案保留了一个奇数数组索引:<code>expirement[row, 2399]</code>,因为这是最后一个,并且只获取列索引<code>11995</code>、<code>11996</code>、<code>11997</code>、<code>11998</code>和<code>11999</code>。我不知道您在这里的用例,但是您可以用值<code>0</code>、<code>1</code>、<code>2</code>、<code>3</code>和<code>4</code>来填充它,这些值也被忽略了。不过,您必须在上面显式地处理这个问题(只需添加一个<code>else</code>案例)。你知道吗</p>