如何计算随机生成的列表中的值的数量?

时间:2017-09-24 04:12:01

标签: python random count

我已通过以下命令创建了一个随机列表:

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。 感谢您的评论。

4 个答案:

答案 0 :(得分: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))

答案 1 :(得分: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.

答案 2 :(得分:0)

您可以使用bisect.bisect(...)来实现此目标:

nestedDict = defaultdict(lambda:defaultdict(list))

def getInfo(line):
    global nestedDict
    tokens = re.split(r'\t+',line)
    docInfo = int(tokens[0]) #Set document Id
    termId = int(tokens[1]) #Set Term Id
    currentPosition = int(tokens[2])
    nestedDict[str(termId)][str(docInfo)] = str(currentPosition)        
    if len(tokens) > 3 :
        for i in range(3,len(tokens)):
            position = int(tokens[i])-currentPosition
            currentPosition = currentPosition + position
            nestedDict[str(termId)][str(docInfo)].append(currentPosition)

示例运行

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

答案 3 :(得分:0)

在数据分析和统计中,这称为“分箱”。如果你在网上找到像'bin'和'bins'这样的术语,你会发现很多关于软件的网页以及如何做到这一点。

但是一个非常好的人使用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)