【问题标题】:How to get the height for each column in Python?如何获取Python中每一列的高度?
【发布时间】: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(我猜)
  • 是的,刚刚编辑过

标签: python list for-loop


【解决方案1】:

您可以在反向压缩矩阵上使用itertools.takewhile

from itertools import takewhile
out = [len(list(takewhile(lambda x: x=='1', reversed(l)))) for l in zip(*matrix)]

输出:

[1, 2, 2, 2, 3, 1, 5, 3, 4, 2]

如果您不想导入takewhile,请使用配方:

def takewhile(predicate, iterable):
    # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
    for x in iterable:
        if predicate(x):
            yield x
        else:
            break

工作原理:

zip 旋转矩阵:

>>> list(zip(*matrix))

[('0', '0', '0', '0', '0', '0', '0', '1'),
 ('0', '1', '0', '1', '0', '0', '1', '1'),
 ('0', '1', '1', '0', '0', '0', '1', '1'),
 ('0', '1', '1', '0', '1', '0', '1', '1'),
 ('0', '1', '0', '0', '0', '1', '1', '1'),
 ('1', '1', '1', '0', '0', '0', '0', '1'),
 ('1', '0', '0', '1', '1', '1', '1', '1'),
 ('0', '0', '1', '0', '0', '1', '1', '1'),
 ('0', '0', '1', '0', '1', '1', '1', '1'),
 ('0', '0', '0', '1', '0', '0', '1', '1')]

reversed 的列表推导反向旋转(实际上每行反转):

>>> [list(reversed(l)) for l in zip(*matrix)]

[['1', '0', '0', '0', '0', '0', '0', '0'],
 ['1', '1', '0', '0', '1', '0', '1', '0'],
 ['1', '1', '0', '0', '0', '1', '1', '0'],
 ['1', '1', '0', '1', '0', '1', '1', '0'],
 ['1', '1', '1', '0', '0', '0', '1', '0'],
 ['1', '0', '0', '0', '0', '1', '1', '1'],
 ['1', '1', '1', '1', '1', '0', '0', '1'],
 ['1', '1', '1', '0', '0', '1', '0', '0'],
 ['1', '1', '1', '1', '0', '1', '0', '0'],
 ['1', '1', '0', '0', '1', '0', '0', '0']]

takewhile 在条件为 True 时保留元素,这里当项目为 '1' (lambda x: x=='1') 时,len 获取输出的长度:

>>> l = ['1', '1', '1', '0', '0', '0', '1', '0']
>>> list(takewhile(lambda x: x=='1', l))

['1', '1', '1']

>>> len(list(takewhile(lambda x: x=='1', l)))
3

注意。 zipreversedtakewhile 等函数是生成器,除非有东西消耗它,否则它们不会产生输出,这就是我在示例中使用 list(generator(…)) 的原因

经典python循环的解决方案:

out = []
for l in zip(*matrix):
    counter = 0
    for elem in reversed(l):
        if elem == '1':
            counter +=1
        else:
            break
    out.append(counter)

【讨论】:

  • 这是一个很好的解决方案,我只是不知道 takewhile 和 zip 是做什么的 + 我完全不了解 lambda,所以我必须查一下。我也想了解是做什么的:)
  • @CaptainGuan 我添加了详细信息
  • 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多