查找二维列表中特定列的长度
Finding the Length of a Specific Column in a 2-Dimensional List
在Python3中,如果我有一个二维列表,其中最后一行没有完全填写(下面的示例),我如何获得特定列的长度?
[[1, 2, 3,],
[4, 5, 6,],
[7, 8,]]
例如,第 0 列和第 1 列的长度为 3,但第 2 列的长度为 2。有没有不使用 pandas 模块的方法来做到这一点?
这是使用 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]
这会将您的行值列表更改为列值列表,其中缺失值填充 None:
list_of_columns = map(list,map(None,*list_of_rows))
然后列表理解和过滤掉列中的空值将为您提供列长度列表:
column_lengths = [len(filter(None, col))) for col in list_of_columns]
然后简单索引(例如lenof第2列):
column_lengths[2]
out:
2
如果某列的索引大于或等于该行的长度,则该列缺失。也就是说,如果一行只有 2 个元素,那么列 0 和列 1 存在,仅此而已。所以我们只需要统计长度大于索引的行数:
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]
和
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]
max(map(len, L))
只是求列数。如果你只关心找到一列,你可以做 sum(len(row) > column_number for row in L)
.
由于列表中间不能有空值,因此不完整的列始终是最后一列。不完整的列总是长度 len(lst) - 1
,所以你可以使用:
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
不需要求和或任何映射函数,因为只有最后一行不完整,只需利用列表的属性。
如果这对您的应用程序特别重要,这还有一个额外的好处,即恒定时间。
在Python3中,如果我有一个二维列表,其中最后一行没有完全填写(下面的示例),我如何获得特定列的长度?
[[1, 2, 3,],
[4, 5, 6,],
[7, 8,]]
例如,第 0 列和第 1 列的长度为 3,但第 2 列的长度为 2。有没有不使用 pandas 模块的方法来做到这一点?
这是使用 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]
这会将您的行值列表更改为列值列表,其中缺失值填充 None:
list_of_columns = map(list,map(None,*list_of_rows))
然后列表理解和过滤掉列中的空值将为您提供列长度列表:
column_lengths = [len(filter(None, col))) for col in list_of_columns]
然后简单索引(例如lenof第2列):
column_lengths[2]
out:
2
如果某列的索引大于或等于该行的长度,则该列缺失。也就是说,如果一行只有 2 个元素,那么列 0 和列 1 存在,仅此而已。所以我们只需要统计长度大于索引的行数:
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]
和
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]
max(map(len, L))
只是求列数。如果你只关心找到一列,你可以做 sum(len(row) > column_number for row in L)
.
由于列表中间不能有空值,因此不完整的列始终是最后一列。不完整的列总是长度 len(lst) - 1
,所以你可以使用:
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
不需要求和或任何映射函数,因为只有最后一行不完整,只需利用列表的属性。
如果这对您的应用程序特别重要,这还有一个额外的好处,即恒定时间。