There’s actually another way:
Matriz1 = [[1,2,3,4],[2,4,6,16],[4,8,12,45],[10,20,30,67],[1,2,3,67]]
Matriz2 = [[1,2],[1,2],[1,2],[1,2],[1,2],[1,2],[1,2],[1,2],[1,2],[1,2]]
Matriz3 = [[1,2,3,4,5,6],[1,2,3,4,5,6],[1,2,3,4,5,6]]
Let’s assume the 3 matrices, above, non-square of order, respectively equal to: (5 X 4), (10 X 2) and (3 X 6).
A simple way to determine the amount of columns consists of:
1) Find the number of lines: nlinhas = len(Matriz)
2) Find quantity of elements: nElementos = len(Matriz[0])
3) Finally, the ncolunas = nElementos/nlinhas
The algorithm goes like this:
nlinhas = 0
nElementos = 0
#ncolunas = 0
for i in range(len(Matriz)):
nlinhas += 1
for j in range(len(Matriz[0])):
nElementos += 1
ncolunas = (nElementos/nlinhas)
print('---------------------------------------------------------------------------------------------')
print('\nNúmero de linhas: ' +str(nlinhas)+'\n' + '\nNúmero de colunas: '+str(ncolunas)+'\n')
print('---------------------------------------------------------------------------------------------')
If we use Matriz1, Matriz2 and Matriz3, in that order, we will find:
---------------------------------------------------------------------------------------------
Número de linhas: 5
Número de colunas: 4.0
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
Número de linhas: 10
Número de colunas: 2.0
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
Número de linhas: 3
Número de colunas: 6.0
---------------------------------------------------------------------------------------------
You tried to:
len(n)
for lines andlen(n[0])
for the columns?– anonimo
Remembering that this is not a "matrix" - it is a "list list" - it is a two-dimensional data structure, and therefore can be used as a matrix. But nothing would prevent, for example, someone making an "append" in the list of the 5th line, and only that line would have more elements. Python does not have a two-dimensional (or mulitdimensional) data native structure, but it is very easy to create a class for this. Usually, however, one uses the Numpy library that already has these very well defined objects.
– jsbueno