Reputation: 11
So I wanted to write a program that would print out how many times a number is repeated in a list. I'm very new to Python and I used the .count() command to get the number of times an element is repeated. I want the output to follow the format "The number n is repeated n times".
This is what I have so far:
x = [1,1,2,4,5,7,2,3,3,8,2,3,6,7]
index = 0
while index < len(x):
print('The number {} is repeated {} times.'.format(x[index],x.count(x[index])))
index = index + 1
And this is the output:
The number 1 is repeated 2 times
The number 1 is repeated 2 times
The number 2 is repeated 3 times
The number 4 is repeated 1 times
The number 5 is repeated 1 times
The number 7 is repeated 2 times
The number 2 is repeated 3 times
The number 3 is repeated 3 times
The number 3 is repeated 3 times
The number 8 is repeated 1 times
The number 2 is repeated 3 times
The number 3 is repeated 3 times
The number 6 is repeated 1 times
The number 7 is repeated 2 times
I want the output to show how many times a number is repeated only once, and in ascending order. How can I make the output come out like this:
The number 1 is repeated 2 times.
The number 2 is repeated 3 times.
The number 3 is repeated 3 times.
...
Thank you! :)
Upvotes: 0
Views: 628
Reputation: 5935
If you want to emulate what Counter
does, you can just fill your own dict
. I intentionally do not use try
and except
, or setdefault
, or defaultdict
, to show that the basic logic is very simple, just a loop and an if-else block:
def count_elements(lst):
ret = {}
for elem in lst:
if elem in ret:
ret[elem] += 1
else:
ret[elem] = 1
return ret
def print_counts(counts):
for n, c in sorted(counts.items()):
print(f'The number {n} is repeated {c} times')
x = [1,1,2,4,5,7,2,3,3,8,2,3,6,7]
c = count_elements(x)
print_counts(c)
Upvotes: 0
Reputation: 2812
This counts all occurrences of numbers and prints them in ascending order.
from collections import Counter
x = [1, 1, 2, 4, 5, 7, 2, 3, 3, 8, 2, 3, 6, 7]
for key, val in sorted(Counter(x).items()):
print(f'The number {key} is repeated {val} times.')
Code Output:
The number 1 is repeated 2 times.
The number 2 is repeated 3 times.
The number 3 is repeated 3 times.
The number 4 is repeated 1 times.
The number 5 is repeated 1 times.
The number 6 is repeated 1 times.
The number 7 is repeated 2 times.
The number 8 is repeated 1 times.
Upvotes: 1
Reputation: 428
You could try this (though any of these answers seem just as valid):
x = [1,1,2,4,5,7,2,3,3,8,2,3,6,7]
x_sorted = sorted(set(x)) # remove duplicates with set, convert to list, and sort
for num in x_sorted:
print('The number {} is repeated {} times.'.format(num,x.count(num)))
This may not be the prettiest solution (I'm not a fan of the sorted(set(x))
line), but it prints the results in ascending order.
This outputs:
The number 1 is repeated 2 times.
The number 2 is repeated 3 times.
The number 3 is repeated 3 times.
The number 4 is repeated 1 times.
The number 5 is repeated 1 times.
The number 6 is repeated 1 times.
The number 7 is repeated 2 times.
The number 8 is repeated 1 times.
Upvotes: 0
Reputation: 522451
Use a Counter
to do the counting and "deduplicating", then iterate over its results:
from collections import Counter
x = [1,1,2,4,5,7,2,3,3,8,2,3,6,7]
for n, c in Counter(x).items():
print('The number {} is repeated {} times.'.format(n, c))
For printing ordered by least common to most common:
for n, c in reversed(Counter(x).most_common()):
...
For printing ordered by the values:
for n, c in sorted(Counter(x).items()):
...
Upvotes: 2
Reputation: 972
Try this:
x = [1,1,2,4,5,7,2,3,3,8,2,3,6,7]
index = 0
while index < len(x):
if index==x.index(x[index]):
print('The number {} is repeated {} times.'.format(x[index],x.count(x[index])))
index = index + 1
Upvotes: 0