Reputation: 90
In Python 3, if I have a 2-dimensional list in which the last row is not completely filled out (example below), how do I get the length of a specific column?
[[1, 2, 3,],
[4, 5, 6,],
[7, 8,]]
For example, columns 0 and 1 have length of 3, but column 2 has a length of 2. Is there a way to do this without using the pandas module?
Upvotes: 2
Views: 986
Reputation: 1243
Since you can't have empty values in the middle of a list, the incomplete columns are always the last columns. The incomplete columns are always length len(lst) - 1
, so you could use:
def lenCol(lst, col):
num_rows = len(lst) # number of rows
cutoff = len(lst[num_rows-1]) # length of last row, i.e. index where column is 1 shorter
if col < cutoff:
return num_rows # if before that index, then just number of rows
else:
return num_rows-1 # otherwise number of rows - 1
No need for summing or any mapping functions, since only the last row is incomplete, just making use of the properties of your list.
This has the added benefit of being constant time, if that is particularly important to your application.
Upvotes: 1
Reputation: 353419
A column is missing if in a row its index is greater than or equal to the length of the row. That is, if a row only has 2 elements, then columns 0 and 1 exist, but that's it. So we simply need to count the number of rows where the length is greater than the index:
In [58]: L = [[1, 2, 3,], [4,], [7, 8,]]
In [59]: for row in L: print(row)
[1, 2, 3]
[4]
[7, 8]
In [60]: lens = [sum(len(row) > i for row in L) for i in range(max(map(len, L)))]
In [61]: lens
Out[61]: [3, 2, 1]
and
In [62]: L = [[1, 2, 3,], [4, 5, 6,], [7, 8,]]
In [63]: lens = [sum(len(row) > i for row in L) for i in range(max(map(len, L)))]
In [64]: lens
Out[64]: [3, 3, 2]
The max(map(len, L))
simply finds the number of columns. If you only cared about finding one column in particular, you could just do sum(len(row) > column_number for row in L)
.
Upvotes: 2
Reputation: 2017
This will change your list of row values to a list of column values with missing values filled in with None:
list_of_columns = map(list,map(None,*list_of_rows))
Then a list comprehension and filtering out null values in the columns will get you a list of column lengths:
column_lengths = [len(filter(None, col))) for col in list_of_columns]
Then simply index (eg lenof column 2):
column_lengths[2]
out:
2
Upvotes: 0
Reputation: 164783
Here's one way using itertools.zip_longest
:
from itertools import zip_longest
lens = [sum(1 for _ in filter(None.__ne__, i)) for i in zip_longest(*L)]
print(lens)
[3, 3, 2]
Upvotes: 1