How to find and display the position of an item in the list, not setting the string value for the search? [duplicate]

0

How to find and display the position of an item in the list, not setting the string value for the search?

Ex:

nome_pessoa = str(input('Informe o seu nome completo: '))

lista_nomes = nome_pessoa.split()

After the names are divided and transformed into a list, you would like to know how to tell which position the third name (or item) is in.

Suppose that in this first execution, the user has entered the name: 'Pedro Lucas Gomes'.

In this way, after transforming this string in the list with the names, it will have the items: ['Pedro', 'Lucas', 'Gomes']

What I want in output data, ie print

The name of the second position, which in this case would be 'Gomes', and the position number, which will be 2.

BUT IN THE SECOND EXECUTION, the user type the name 'João Neves Frufru Feitosa', what I want in the output is:

The name of the second position is 'Frufru', and the number of its position is 2.

Have you understood? I will not use the command:

print (name_name.infr ('Frufru'))

Because the name 'Frufru', will not be fixed in the code! The name requested in the program will be typed from the keyboard by the user at the time of each code execution.

(Thanks to all who are struggling to help and I apologize for my lack of clarity before editing)

    
asked by anonymous 01.06.2018 / 00:59

2 answers

0

Use "index" to get the position of the item in the list.

lista_nomes.index('Gomes')

I do not know if this is what you want, but this is the default way to get an address from a list.

    
01.06.2018 / 01:12
0

If you want to get the third item from the list, you must use brackets to access the list item you want. It is important to remember that the indexes start at 0 , so to access the 3rd item, you should search for index 2:

nome_completo = input('Por favor, informe seu nome completo: ')
lista_nome_completo = nome_completo.split()
print('O elemento 3 da lista é {}'.format(minha_lista[2]))

If you want to get the last name every time, you should get the size of the list. A shortcut to index is that -1 is the index of the last element.

nome_completo = input('Por favor, informe seu nome completo: ')
lista_nome_completo = nome_completo.split()
ultimo_elemento = len(lista_nome_completo) - 1
print('O último elemento da lista é o de número {}, e é {}'.format(ultimo_elemento, lista_nome_completo[-1]))
    
01.06.2018 / 01:35