How to get the highest value of a python array / vector?

Asked

Viewed 8,205 times

1

how do I get the highest value of a matrix or vector?

For example, in this exercise I have to make a 4x4 matrix and return to row and column of the largest element, but I stopped there.

m = []
for i in range(4):
    linha = []
    for j in range(4):
        linha.append(int(input()))
        m.append(linha)

4 answers

3

Basically, that’s it for a line:

max(linha)

And for a matrix:

max([valor for linha in matriz for valor in linha])

2


First thing you need to notice is that the creation of the matrix is wrong, you need to add a new line after you have already inserted values in her four positions, for this the line m.append(linha) needs to be out of second for.

Find the highest value is simple, just use the function max combined with comprehensilist on, basically, a new collection is created (linear, this time) with all the elements (without worrying about its lines and columns) and in this linear collection is sought the highest value.

maior = max([valor for linha in m for valor in linha])

But since you need to find his contents (row and column), I made a code that goes through all the elements, checks which is the largest and stores his row and column.

Is there a simpler way to do it, since Python has this philosophy of being simplistic? Most likely yes, but I have no idea what it would be like

m = []
for i in range(4):
    linha = []
    for j in range(4):
        linha.append(int(input()))

    m.append(linha)

maior_linha = 0
maior_coluna = 0
maior = m[0][0]
for l in range(4):
    for c in range(4):        
        if maior < m[l][c]:
            maior = m[l][c]
            maior_linha = l
            maior_coluna = c

print('linha do maior: {}\ncoluna do maior: {}'.format(maior_linha, maior_coluna))

See working on repl.it.

  • 1

    It is really a good thing to give the answer to the homework instead of giving the means to reach the solution?

  • 2

    @ppalacios I explained everything that is done, if he wants to study will read the code and my explanation and if you have doubts can comment or even go on chat and talk to me. If he doesn’t want to learn there are 1001 ways to get the result anyway, it won’t be a Sopt response that will prevent this. In addition, the AP showed that it is stopped at the given point and that it needs help, at no time the question seemed to me a "do it for me".

1

You can still check in real time, at each value read. This will prevent you from creating repeat loops for matrix definition and other repeat loops to fetch the highest value.

Take the example:

m = []               # Matriz
max = None           # Armazena o maior valor
position = (0, 0)    # Armazena a posição do maior valor

# Define 4 linhas na matriz:
for i in range(4):

    # Linha da matriz:
    linha = []

    # Define 4 colunas na matriz:
    for j in range(4):

        # Lê o valor a ser inserido:
        value = int(input("Valor da posição ({}, {}):".format(i, j)))

        # Verifica se o valor é maior que o máximo atual:
        if max is None or value > max:

            # Sim, então altera o valor do máximo e de sua posição
            max = value
            position = (i, j)

        # Adiciona o valor à linha:
        linha.append(value)

        # Adiciona a linha na matriz:
        m.append(linha)

# Exibe a posição do maior valor:
print("O maior valor está na posição {} e vale {}".format(position, max))

See working on Repl.it.

0

maior_linha = matriz.index(max(matriz))
maior_coluna = matriz[maior_linha].index(max(matriz[maior_linha]))
print(f'localização maior valor: ({maior_linha}, {maior_coluna})')

That’s the simplest way I could think of. I would like to put all the code here to help you create and print a matrix well but I do not know this platform and having to put line by line the code is tense hauhaua qlqr thing calls that I send you!

Browser other questions tagged

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