Each Answer to this Q is separated by one/two green lines.
How do I find how many rows and columns are in a 2d array?
Input = ([[1, 2], [3, 4], [5, 6]])`
should be displayed as 3 rows and 2 columns.
numrows = len(input) # 3 rows in your example numcols = len(input) # 2 columns in your example
Assuming that all the sublists have the same length (that is, it’s not a jagged array).
You can use
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)
First value in the tuple is number rows = 3; second value in the tuple is number of columns = 2.
In addition, correct way to count total item number would be:
sum(len(x) for x in input)
rows = len(input) cols = map(len, input) #list of column lengths
You can also use np.size(a,1), 1 here is the axis and this will give you the number of columns
rows = len(input) cols = len(list(zip(*input)))