Another interesting way is using the method Counter
library Collections. In this case, the code would be:
from collections import Counter
lista = list(map(int, input('Digite os valores: ').split()))
c = Counter(lista)
for numero, repeticoes in c.items():
if repeticoes > 1:
result = [indice for indice, item in enumerate(lista) if item == numero]
print(f'O número "{numero}" se repete nos índices {result}.')
Note that when we execute this code we receive the following message: Digite os valores:
. Right now we must type all the values, in the same line, separated for a single space and press Enter
.
OBS: With this code you can enter as many numbers as you want in your list.
At this time the block for will travel the object c.items()
and whether the number of repetitions of a particular number is greater than 1, the list shall be assembled result. This list will be formed by List Comprehensions and, later, will be displayed the result composed by the number that has more than 1 occurrence and the respective indexes of its occurrences.
if ((i == j) == 0):
, what this should do?– Woss
compare the element of i with the element of j, and then check if it is equal to zero
– MasterZub