【发布时间】:2022-01-09 20:54:23
【问题描述】:
这是一个嵌套列表:
matrix = [['0', '0', '0', '0', '0', '1', '1', '0', '0', '0'],
['0', '1', '1', '1', '1', '1', '0', '0', '0', '0'],
['0', '0', '1', '1', '0', '1', '0', '1', '1', '0'],
['0', '1', '0', '0', '0', '0', '1', '0', '0', '1'],
['0', '0', '0', '1', '0', '0', '1', '0', '1', '0'],
['0', '0', '0', '0', '1', '0', '1', '1', '1', '0'],
['0', '1', '1', '1', '1', '0', '1', '1', '1', '1'],
['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']]
#output should be [1,2,2,2,3,1,5,3,4,2]
Height = 8
Width = 10
我想知道如何获得每列的高度。然后每个高度,我想把它放在一个列表中。
我们计算 1,如果它们与 1 相邻,则它们只计算高度。
我们从下面开始计数,然后向上。
输出应该是[1,2,2,2,3,1,5,3,4,1]
我只想在 Python 函数中使用 build。
我尝试使用 for 循环和 if、else 语句。
For loop iterate through the list.
like if i == '1' add 1 to counter.
if i == '0' reset counter and add the last value from counter to counter1, but only if counter is greater then counter1.
【问题讨论】:
-
你能证明自己任何努力解决这个问题吗?
-
请编辑您的问题以包含您尝试过的内容
-
为什么最后一列的高度应该是1?
-
最后一列应该是 2(我猜)
-
是的,刚刚编辑过