Reputation: 13
I have created a random list by below command:
import random
a=[random.randrange(0,100) for i in xrange(50)]
print a
Now, what could be the command for counting the number of values that are between 0 and 9, 10 and 19, 20 and 29, and so on.
I can print them as below:
import random
a = [random.randrange(0,100) for i in xrange(50)]
for b in a:
if b<10:
print b
But, I don't know how to write a command to count the number of the values after printing b. Thanks for your comments.
Upvotes: 0
Views: 175
Reputation: 21643
In data analysis and statistics this is called 'binning'. If you poke around on the 'net for terms like 'bin' and 'bins' you'll find a plethora of pages about software and how to do this.
But a really good one uses the preeminent product for Python, numpy.
>>> import random
>>> a=[random.randrange(0,100) for i in range(50)]
>>> from numpy import histogram
In your case you need to set up the end points of the bins which are -0.5, 9.5, 19.5, 29.5, 39.5, 49.5, 59.5, 69.5, 79.5, 89.5, and 99.5. (I've chosen -0.5 for the low end only because it made my calculation easier.) histogram counts how many items fall within each of the ranges given by these numbers, taken in pairs (-0.5 to 9.5, 9.5 to 19.5, etc).
>>> bins = [-0.5+10*i for i in range(11)]
>>> hist,_ = histogram(a, bins)
And here's the result.
>>> hist
array([6, 6, 2, 6, 2, 3, 6, 9, 5, 5], dtype=int64)
Upvotes: 0
Reputation: 48092
You may use bisect.bisect(...)
to achieve this as:
from bisect import bisect
import random
randon_nums = [random.randint(0,100) for _ in xrange(100)]
bucket = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] # can also be created using:
# range(10, 101, 10)
randon_nums.sort() # sort the initial list in order to use it with `bisect`
counts = []
last_bucket_count = 0 # to track the count of numbers in last calculated bucket
for range_max in bucket:
i = bisect(randon_nums, range_max, end_index)
counts.append(i - last_bucket_count)
last_bucket_count = i
Sample Run:
When the value of random_nums
is:
>>> randon_nums
[0, 1, 4, 5, 5, 5, 5, 6, 7, 7, 8, 8, 10, 10, 11, 11, 12, 13, 13, 13, 16, 17, 18, 18, 18, 18, 19, 20, 21, 22, 24, 24, 25, 25, 26, 26, 26, 26, 26, 29, 30, 30, 31, 33, 37, 37, 38, 42, 42, 43, 44, 44, 47, 47, 49, 51, 52, 55, 55, 57, 57, 58, 59, 63, 63, 63, 63, 64, 64, 65, 66, 67, 68, 71, 73, 73, 73, 74, 77, 79, 82, 83, 83, 83, 84, 85, 87, 87, 88, 89, 89, 90, 92, 93, 95, 96, 98, 98, 99, 99]
the above program returns count
as:
>>> counts
[ 14, 14, 14, 5, 8, 8, 10, 7, 12, 8]
# ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
# 0-10 10-20 20-30 30-40 40-50 50-60 60-70 70-80 80-90 90-100
Upvotes: 0
Reputation: 18008
Just make a dictionary, enumerate and count.
>>> import random
>>> a = [random.randrange(0,100) for i in xrange(50)]
>>> a
[88, 48, 7, 92, 22, 13, 66, 38, 72, 34, 8, 18, 13, 29, 48, 63, 23, 30, 91, 40, 96, 89, 27, 8, 92, 26, 98, 83, 31, 45, 81, 4, 55, 4, 42, 94, 64, 35, 19, 64, 18, 96, 26, 12, 1, 54, 89, 67, 82, 62]
>>> counts = {}
>>> for i in a:
t = counts.setdefault(i/10,0)
counts[i/10] = t + 1
>>> counts
{0: 6, 1: 6, 2: 6, 3: 5, 4: 5, 5: 2, 6: 6, 7: 1, 8: 6, 9: 7}
# Means: 0-9=> 6 numbers, 10-19=> 6 numbers etc.
Upvotes: 1
Reputation: 19
if I understood you correctly, then so:
import random
a = [random.randrange(0,100) for i in xrange(50)]
print len(filter(lambda x: 0 <= x < 10,a))
print len(filter(lambda x: 10 <= x < 20,a))
etc
Upvotes: 1