1

我想在再次出现 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]]

我希望你能帮助我。

4

1 回答 1

1

只需附加True到每一行,然后True为每个True元素计算下一行的索引:

matrix = [[True, False, True, False, False, True, False, True],
          [True, False, False, True, True, True, True, True],
          [False]]

res = []
for row in matrix:
    rr = row + [True]
    row_res = [rr[n+1:].index(True) if rr[n] else 0
               for n in range(len(row) - 1)]
    res.append(row_res + [0])

为了更好的可读性,在不使用列表理解的情况下编写相同的内容并添加一些注释:

res = [] #our result list
for row in matrix:
    rr = row + [True]  #row with an extra True at the end so index always works
    row_res = []       #result for this row
    for n in range(len(row) - 1):
        #if x is True, calculate the relative index of the next true
        x = rr[n+1:].index(True) if rr[n] else 0
        row_res.append(x)
    row_res.append(0)  #add an extra 0 at the end for the last element
    res.append(row_res)
于 2013-08-14T19:10:43.860 回答