How to find the position of an item on a list?

Asked

Viewed 41,177 times

4

I am writing a program that takes a specific amount of values, and returns the lowest value of the list and its position. It is as follows:

quantidade = int(raw_input())
numeros = raw_input()
NumerosNaLista = numeros.split()
if len(NumerosNaLista) == quantidade:
    MenorValor = min(NumerosNaLista)
    print "Menor valor:", MenorValor

I tried to use findto find the position of the lowest value in NumerosNaLista, but it doesn’t work. How to proceed?

6 answers

5


#!/usr/bin/env python
#*-* coding: utf-8 *-*

numbers = raw_input()

# gera um vetor de numeros atravez da string
n_list = [ int(x) for x in numbers.split() ]

n_min = min(n_list)
n_pos = n_list.index(n_min) # pega a posição do valor n_min

print "Menor valor: %s" % n_min
print "Posição: %" % n_pos
  • I understand. I need to submit my code to a website, which only accepts if it is correct, that is, if it works. I wrote two codes that work, but he does not accept, and apparently he wants me to write using loops that replace the smallest values found and save his position. I’ll put the second code, I’d like you to take a look.

  • @Guilhermesantanadesouza, I get it, is one of those programming marathon sites, has some very boring exercises.

  • In fact it’s a site used by teachers... They register problems, and students have to solve and send... Who does not send, is without presence... Despite some mistakes, my two codes meet what the problem asks, and the site did not accept... Thanks for the touches.

5

You can implement your algorithm using loop, in this case the while. Look at this example:

lista = []
i = 0
quantidade = int(raw_input('Qtd: '))

while (i < quantidade):
    numero = input("Valor: ")
    i += 1
    lista.append(numero)

print 'Indice do menor valor:', lista.index(min(lista))

Entry (number to be inserted in the list):

3

Input (values):

21
33
-21

Output (lowest value index):

2

The method index() find the index of an item of a given list, passed the desired item as argument that in this case was the lowest value of the list returned by the method min().

I suggest consulting the documentation to learn more about the methods and a reading on list manipulation in Python.

0

It is very old the post, but the answer can be used for the next ones. To return the index of an element of a list you must use index (element).

    lista = ["a", "b", "c"] 
    print(lista.index("b")) 
    retorna # 1

0

I got it this way:

quantidade = int(raw_input())
numeros = raw_input()
NumerosNaLista = numeros.split()
if len(NumerosNaLista) == quantidade:
    MenorValor = min(NumerosNaLista)
    print "Menor valor:", MenorValor
    lista = numeros.replace(" ", "")
    MenorValor = min(lista)
    posicao = lista.find(MenorValor)
    print "Posicao:", posicao

It worked, and I believe you’re right. If there are any mistakes, I ask that you be informed by whom you view.

  • has some redundant lines in its code, such as the quantidade, as it has no repeating structure is not necessary, the condition if, and the way you’re taking the position is not the most viable way, the correct is to use the existing vector, I’ll leave an example in the answer.

0

Another way:

menor_ate_agora = None
quantidade = int(raw_input())
numeros = raw_input()
NumerosNaLista = numeros.split()
if len(NumerosNaLista) == quantidade:
    for valor in NumerosNaLista:
        if menor_ate_agora is None:
            menor_ate_agora = valor
        elif valor < menor_ate_agora:
            menor_ate_agora = valor
    print "Menor valor:", menor_ate_agora
    lista = numeros.replace(" ", "")
    MenorValor = min(lista)
    posicao = lista.find(MenorValor)
    print "Posicao:", posicao

-1

Simple as that...

print('Posição: {}'.format(NumerosNaLista.index(MenorValor))

Browser other questions tagged

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