如何找到二维数组中的行数和列数?
例如,
Input = ([[1, 2], [3, 4], [5, 6]])`
应显示为3行2列。
发布于 2012-05-23 11:21:35
如下所示:
numrows = len(input) # 3 rows in your example
numcols = len(input[0]) # 2 columns in your example
假设所有的子列表都有相同的长度(也就是说,它不是锯齿数组)。
发布于 2012-05-23 11:24:29
您可以使用numpy.shape
。
import numpy as np
x = np.array([[1, 2],[3, 4],[5, 6]])
结果:
>>> x
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.shape(x)
(3, 2)
元组中的第一个值是number rows = 3;元组中的第二个值是number of column= 2。
发布于 2016-10-25 21:03:43
此外,计算项目总数的正确方法是:
sum(len(x) for x in input)
https://stackoverflow.com/questions/10713004
复制相似问题