How do I get the largest value from an array / vector in python?

1

How do I get the highest value from an array or vector?

For example, in this exercise I have to do a 4x4 array and return the 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)
    
asked by anonymous 24.05.2017 / 17:15

3 answers

2

First thing you need to note is that the matrix creation is wrong, you need to add a new row after you have already entered values in the four positions of it, so the line m.append(linha) must be outside the second for .

Finding the greatest value is simple, just use the max function combined with list comprehension , basically a new collection is created (linear this time) with all elements worry about its rows and columns) and in this linear collection the highest value is sought.

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

But as you need to find the indexes of it (row and column), I made a code that goes through all the elements, checks which is the largest and saves the row and column of it.

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.

    
24.05.2017 / 18:42
3

Basically, this is for a line:

max(linha)

And for an array:

max([valor for linha in matriz for valor in linha])
    
24.05.2017 / 18:17
1

You can still do the real-time scan, with each value read. This will prevent you from creating repeat loops for the array definition and other repeat loops to fetch the highest value.

See 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 at Repl.it .

    
24.05.2017 / 20:18