2017-09-24 71 views
0

我已经建立由下面的指令的一随机列表:如何统计随机生成列表中的值的数量?

import random 
a=[random.randrange(0,100) for i in xrange(50)] 
print a 

现在,可能是什么命令用于计数是0和9,图10和19,20和29之间的值的数目,等等。

我可以如下打印出来:

import random 
a = [random.randrange(0,100) for i in xrange(50)] 
for b in a: 
    if b<10: 
    print b 

但是,我不知道怎么写命令打印B之后计数的值的数量。 感谢您的意见。

回答

1

只是要一本字典,枚举和计数。

>>> 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. 
0

您可以使用bisect.bisect(...)实现这一为:

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 

样品试验:

random_nums值是:

>>> 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] 

上述程序返回count为:

>>> 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 
1

,如果我理解正确的话,那么这样:

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)) 

0

在数据分析和统计中,这被称为“分箱”。如果你在'网箱'和'箱子'这样的网络上徘徊,你会发现大量有关软件的页面以及如何做到这一点。

但是,一个非常好的使用Python,numpy的卓越产品。

>>> import random 
>>> a=[random.randrange(0,100) for i in range(50)] 
>>> from numpy import histogram 

在你的情况,你需要建立哪些是-0.5,9.5,19.5,29.5,39.5,49.5,59.5,69.5,79.5,89.5,和99.5仓的终点。 (我选择-0.5作为低端,只是因为它让我的计算更容易。)histogram计算在这些数字给出的每个范围内有多少物品,成对(-0.5至9.5,9.5至19.5等) )。

>>> bins = [-0.5+10*i for i in range(11)] 
>>> hist,_ = histogram(a, bins) 

这就是结果。

>>> hist 
array([6, 6, 2, 6, 2, 3, 6, 9, 5, 5], dtype=int64) 
+0

感谢您提供的信息。 – Nourolah

+0

非常欢迎。 –