我想在再次出现 True 之前计算嵌套列表中 False 的数量。我怎么做?True 之后的 False 数显示在列表中numoffalsescount
,然后将其收集到列表中numoffalsescountlist
。必须调整第二个 if 语句中的代码。这是我的代码:
def neighborhood(iterable):
iterator = iter(iterable)
prev = None
item = iterator.next() # throws StopIteration if empty.
for next in iterator:
yield (prev,item,next)
prev = item
item = next
yield (prev,item,None)
matrix2bool = [[True, False, True, False, False, True, False, True], [True, False, False, True, True, True, True, True], [False]]
i11 = 0
numoffalsescountlist = []
for index16 in matrix2bool:
falsecount = 0
falsecounttemp = 0
falsecountmax = 0
init = 0
numoffalsescount = []
for prev,item,next in neighborhood(matrix2bool[i11]):
if next == False:
#print item, next
if falsecount != 0:
falsecount += 1
falsecounttemp = falsecount
#init = 0
init += 1
if falsecounttemp > falsecountmax:
falsecountmax = falsecounttemp
print 'falsecount', falsecount
print 'init', init
print 'fcm', falsecountmax
numoffalsescount.append(0)
numoffalsescount[falsecount-init] = falsecountmax
if falsecount != 0:
numoffalsescount[falsecount-1] = 0
else:
init += 1
falsecount += 1
falsecounttemp = falsecount
falsecounttemp += falsecount - 1
numoffalsescount.append(falsecounttemp)
else:
if falsecount != 0:
falsecount = 0
numoffalsescount.append(falsecount)
else:
x = 0
numoffalsescount.append(0)
print 'numoffalsescount', numoffalsescount
i11 += 1
numoffalsescountlist.append(numoffalsescount)
print 'numoffalsescountlist', numoffalsescountlist
输入列表是matrix2bool
,并且应该给出输出:
numoffalsescount [1, 0, 2, 0, 0, 1, 0, 0]
numoffalsescount [2, 0, 0, 0, 0, 0, 0, 0]
numoffalsescount [0]
numoffalsescountlist [[1, 0, 2, 0, 0, 1, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0]]
但它给了我输出:
numoffalsescount [1, 0, 1, 2, 0, 1, 0, 0]
numoffalsescount [2, 0, 0, 0, 0, 0, 0, 0]
numoffalsescount [0]
numoffalsescountlist [[1, 0, 1, 2, 0, 1, 0, 0], [2, 0, 0, 0, 0, 0, 0, 0], [0]]
我希望你能帮助我。