I want to print the count of items based on the IF statement below. What I have below is printing the entire list, 7 times, and a count for each item (1). That is not what I want. Ideally it would return:
5
1
1
Any ideas?
from collections import Counter
li = (1,4,55,6,87,44,25)
for i in li:
if i < 50:
print(Counter(li))
elif i > 50 and i < 85:
print(Counter(li))
else:
print(Counter(li))
You need to 'normalise' your values and count those; so for the
i < 50
case, you could use a string'below 50'
and count those values:Note that I counted
50
in the50 - 84
group. This produces a single counter object, and you then just ask for the specific labels:You don't really need a
Counter()
here, however; for this case it would be easier to just use 3 variables: