如何查找二维数组中有多少行和列?
例如,
Input = ([[1, 2], [3, 4], [5, 6]])`
Run Code Online (Sandbox Code Playgroud)
应显示为3行和2列.
Ósc*_*pez 137
像这样:
numrows = len(input) # 3 rows in your example
numcols = len(input[0]) # 2 columns in your example
Run Code Online (Sandbox Code Playgroud)
假设所有子列表具有相同的长度(即,它不是锯齿状数组).
Aka*_*all 33
你可以用numpy.shape.
import numpy as np
x = np.array([[1, 2],[3, 4],[5, 6]])
Run Code Online (Sandbox Code Playgroud)
结果:
>>> x
array([[1, 2],
[3, 4],
[5, 6]])
>>> np.shape(x)
(3, 2)
Run Code Online (Sandbox Code Playgroud)
元组中的第一个值是数行= 3; 元组中的第二个值是列数= 2.
mac*_*how 10
假设输入[row] [col],
rows = len(input)
cols = map(len, input) #list of column lengths
Run Code Online (Sandbox Code Playgroud)