Home > Enterprise >  How to get the height for each column in Python?
How to get the height for each column in Python?

Time:12-04

This is a nested list:

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

I want to know how I can get the height of each column. And then each height, I want to put it in a list.

We counting 1's and they only count for the height if they are adjoint 1's.

We start with counting below and then go up.

Output should be [1,2,2,2,3,1,5,3,4,1]

I only want to use build in Python functions.

I tried with a for loop and if, else statements.

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.

CodePudding user response:

You can use itertools.takewhile on the reversed, zipped matrix:

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

output:

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

If you don't want to import takewhile, use the recipe:

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

How it works:

zip rotates the matrix:

>>> 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')]

The list comprehension with reversed rotates the other way around (actually inverses each row):

>>> [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 keepd the elements while the condition is True, here while the items are '1' (lambda x: x=='1'), and len gets the length of the output:

>>> 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

NB. functions like zip, reversed, takewhile are generators, they don't produce output unless something consumes it, that's why I used list(generator(…)) in the exammples

solution with classical python loops:

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