gpt4 book ai didi

python - 计算二维列表中 False 的数量,直到遇到 True (Python)

转载 作者:太空宇宙 更新时间:2023-11-03 18:51:35 30 4
gpt4 key购买 nike

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

希望你能帮助我。

最佳答案

只需将 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)

关于python - 计算二维列表中 False 的数量,直到遇到 True (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18239995/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com