def counter(user_inp):
for i in user_inp:
print(f"{i} occurs {user_inp.count(i)} times")
counter([1, 2 ,3, 1])
whenever there is a duplicate number the iterator will print it out again instead of just once. Unsure how to specify that
CodePudding user response:
You need to make your for-loop ignore duplicates - often the easiest way to do this is to convert the list to a set first.
#input is a list of integers
def counter(user_inp):
for i in set(user_inp):
print (f"{i} occurs {user_inp.count(i)} times")
CodePudding user response:
If I understood correctly this should solve it for you:
lista = [3, 3, 3, 5, 5, 1, 2, 4]
def counter(x):
temp = []
for i in x:
if i not in temp:
temp.append(i)
print(str(i) " occurs " str(x.count(i)) " times")
counter(lista)