Define matrix in python

Asked

Viewed 1,852 times

2

I need to create a matrix and fill it with random values (randint(0,9)), except in equal rows and columns. For example, in this way:

   A| B| C| D| E
A  x| 2| 1| 4| 2 
B  1| x| 1| 1| 2
C  5| 1| x| 4| 3
D  8| 2| 1| x| 2
E  5| 4| 1| 4| x

I’ve done this before:

from random import randint


def matrix():

    n=5 
    m=10

    matrixC = []
    value = (randint(0,9))

    #adicionar esse valores à matriz gerada
    for i in range(0, n):
        for j in range(0, n):
            if i != j:
                matrizC.append[[random.random() for i in range(n)] for j in range(n)]
            else:
                matrizC.append("x")

[UPDATE]

Following the directions I have this:

from random import randint

def matriz():
    #pedir o n
    n=5
    matriz = []  #declaração da matriz

    n_col=1
    n_lin=1

    for n_lin in range(n):
        linha = [] # cria uma linha para a matriz
        for n_col in range(n):
            if n_lin==n_col:
                linha.append('x')               
            else:
                linha.append(randint(1,9))

    matriz.append(linha)

    n_col=1
    n_lin=1 

    for n_lin in range(n):
        for n_col in range(n):
            print(matriz[n_lin][n_col], end='\t')

In the 3rd go I print the matrix, but when compiling I get no results.

  • There doesn’t seem to be anything wrong with the question - there is the initial code of the A.P., which has some parts missing, and an answer may guide it in the right direction.

  • Elaine, would [Dit] be your post adding a brief explanation of what went wrong or what did not correspond to the expectation in the code you tried? The post started well, with relevant details, but lacked a description of where exactly is the problem, which is precisely the doubt of programming in site scope. It ended up being a "question without question", but I believe that a simple edition complementing solve.

  • That’s all your code? you know that in Python there is no special named function that is called automatically - rather that the program runs from start to finish, and functions need to be called. To see any result - even if it is not what you expect there, you have to call the function `matrix() in the module body.

1 answer

1


The problem is that you are mixing two techniques. One way to do it is by using the forstructured one on each line:

matriz = []
for n_lin in range(5):
    linha = [] # cria uma linha para a matriz
    for n_col in range(5):
        if n_lin == n_col:
            linha.append('x')
        else:
            linha.append(randint(0, 9))
    matriz.append(linha)

The other way is to put everything in one line, the famous "list understanding":

matriz = [['x' if x == y else randint(0, 9) for x in range(5)] for y in range(5)]
  • Thanks nosko, I think so already gives. Could you help me to print this matrix? I did as indicated in the edition, but gives me output nenhm.

  • in your code missed calling function: a line matriz() in column zero, at the end of the file would do this.

Browser other questions tagged

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