Recover matrix dimensions in Python

Asked

Viewed 6,292 times

1

Can someone help me write the function "dimensions(matrix)" that receives a matrix as a parameter and prints the dimensions of the received matrix, in iXj format?

def crie_matriz(n_linhas, n_colunas, valor):

    matriz = []

    for i in range(n_linhas):
        linha = []

        for j in range(n_colunas):
            linha.append(valor)
            matriz.append(linha)
    for n in matriz:
        print(''.join(map(str,matriz)))
    return matriz

a = crie_matriz(2,3)

print(a)

The above code prints the output, but the question asks to print the size as shown below:

minha_matriz = [[1], [2], [3]]
dimensoes(minha_matriz)
3X1

minha_matriz = [[1, 2, 3], [4, 5, 6]]
dimensoes(minha_matriz)
2X3
  • You can put the code you have sff

  • @Miguel added the code I made, but it’s incomplete.

4 answers

2

can use the library numpy, which already comes with the method of returning the dimensions of the array/array

ex:

import numpy as np

np.array([[0, 1, 2],
          [3, 4, 5]]).shape

prints (2,3)

that when stored in a variable you can always call .shape ex:

import numpy as np

x = np.array([[0, 1, 2],
              [3, 4, 5]])

print(x.shape)

still if you want to get transposed, ex:

print(x.T)

prints:

array([[0, 3],
       [1, 4],
       [2, 5]])

good fun!

1

Here’s what you can do:

tam_matriz = (len(minha_matriz), len(minha_matriz[0]))

This is gonna be a tuple, in this case: (2, 3)

You can print like this:

print('{} x {}'.format(tam_matriz[0], tam_matriz[1]))

Or with a function:

def dimensoes(m):
    return (len(m), len(m[0]))

print(dimensoes(minha_matriz)) # (2, 3)

0

Look I did it this way, it’s not the best, though, functional:`

def dimensoes(matriz):
colunas = 0
linhas = 0
qtd_colunas = 0
a = ''
for i in range(len(matriz)):
    linhas = linhas + 1 # soma a qtd de linhas
    colunas = matriz[i] # Armazena os itens de cada linha
for w in colunas: # Ira interar a qtd de itens da ->ÚLTIMA<- lista dentro da matriz.
    qtd_colunas = qtd_colunas + 1
a = print('{}X{}'.format(linhas, qtd_colunas))

return a

0


The code would look like this:

def dimensoes(matriz):
tam_matriz = (len(matriz), len(matriz[0]))
print('{}X{}'.format(tam_matriz[0], tam_matriz[1]))

If the question asks to print, you have to use the 'print' command, ask to return or return, use the 'Return' command'

  • Hello Eduardo Oliveira, I tried how you said I inserted the variable and the print, replacing "print('.Join(map(str,matrix)))" with "tam_matrix = (Len(matrix), Len(matrix[0])) print('{}X{}'.format(tam_matrix[0], tam_matrix[1])". But it’s not working. For example when I call the function "cria_matrizes(2,2,0), the code is printing "4x2" four times. What’s wrong with my code?

  • The code is exactly what you asked for, nothing more. If you are asking to create a 'dimensos' function, you will not need the 'cria_matrizes' function you quoted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.