How to determine how many times a certain item is repeated within a list? [closed]

0

In a certain list I want to find those items that are the same to count them.

Currently until this code arrives, try to create a counter for the items but I have not succeeded:.

def lista_nombres():
    nombres = []
    while True:
        x = input("Ingresa un nombre o 0 para terminar: ")
        if x == "0":
            break
        if x in nombres:
            continue           
        nombres.append(x)
    return nombres

nombres = lista_nombres()
print("La lista es:", nombres)
    
asked by Nico Ladeveze 12.07.2018 в 17:43
source

1 answer

0

You could use the Counter module, it gives you the information you need, here is a way to do it:

from collections import Counter

def nombres():
  ns = []
  while True:
    inp = input('Ingresa un nombre o 0 para terminar: ').strip().capitalize()
    if inp == '0':
      return nombres
    else:
      ns.append(inp)

print(Counter(nombres()))
    
answered by 12.07.2018 / 20:43
source