我还没有安装 numpy,但如果我正确理解你想要的,我认为下面的代码会让你想到在 numpy 中做类似的事情:
b = [True,True,True,False,False,True,True,False,False,False,True,False,True,
False,False,False,False,True,False,False,True]
print b,'\n'
def grignote(X):
it = iter(xrange(len(X)))
for i in it:
print 'i == %d %s' % (i,X[i])
if X[i]==False:
j = (k for k in it if X[k]==True).next()
print ' j == %d %s X[%d:%d]==%r' % (j,X[j],i,j,X[i:j])
if j-i<3:
print ' executing X[%d:%d]==%r' % (i,j,[True for m in xrange(j-i)])
X[i:j] = [True for m in xrange(j-i)]
else:
print ' --- no execution --- too long'
grignote(b)
print '\n',b
结果
[True, True, True, False, False, True, True, False, False, False, True, False, True, False, False, False, False, True, False, False, True]
i == 0 True
i == 1 True
i == 2 True
i == 3 False
j == 5 True X[3:5]==[False, False]
executing X[3:5]==[True, True]
i == 6 True
i == 7 False
j == 10 True X[7:10]==[False, False, False]
--- no execution --- too long
i == 11 False
j == 12 True X[11:12]==[False]
executing X[11:12]==[True]
i == 13 False
j == 17 True X[13:17]==[False, False, False, False]
--- no execution --- too long
i == 18 False
j == 20 True X[18:20]==[False, False]
executing X[18:20]==[True, True]
[True, True, True, True, True, True, True, False, False, False, True, True, True, False, False, False, False, True, True, True, True]