You will need to read the user input in order to identify the size of the array. Do this using the input
;
a. The return of the input
function will always be a string , so you need to convert your string to a sequence of two integers. Do this with the help of string.split
and int
;
Possessing the dimensions of the array, NxM, you will have to read N times the user input, which will be the rows of the array. Again use the input
function to read the entry and repeat the process with a loop repeat. I recommend doing the for
structure supported by the range
;
a. Again, remember that the return of input
will always be a string , so use the same logic as 1.a to convert it to a sequence of numbers;
Tips:
An array can be represented as a list of lists;
You can initialize an empty list as lista = []
;
You can add new elements to a list with lista.append(...)
;
You can access a certain index in the list with lista[i]
;
Since you tried to do this, I'll put an example code:
import numpy
dimensions = input('Dimensões da matriz NxM: ').split()
N, M = [int(value) for value in dimensions]
matrix = []
for i in range(N):
row = input(f'Linha {i+1}: ').split()
if len(row) != M:
raise Exception(f'Você precisa informar {M} valores por linha')
numbers = [int(number) for number in row]
matrix.append(numbers)
matrix = numpy.matrix(matrix)
print(matrix)
See working at Repl.it | GitHub GIST
An example of the generated output is:
>>> Dimensões da matriz NxM: 2 2
>>> Linha 1: 1 2
>>> Linha 2: 3 4
[[1 2]
[3 4]]