简体   繁体   English

Python将范围内的随机整数附加到列表中,计算数字的出现次数

[英]Python appending random int in range to list, count number of occurences of numbers

So I have the following code: 所以我有以下代码:

import random
from collections import Counter
L = []
for x in range(0, 100):
    L.append([])
    for y in range(0, 6):
        L[x].append(random.randint(0, 45) + 1)

Now I need to be able to count the number of times each number appears in the list. 现在,我需要能够计算每个数字出现在列表中的次数。 I tried counter and all kinds of other stuff, still get errors and I'm stuck. 我尝试了counter和其他各种方法,但仍然出现错误,我被卡住了。 Any help would be appreciated. 任何帮助,将不胜感激。

You can store a per sublist Counter in a list and then sum all these counters. 您可以将每个子列表Counter存储在列表中,然后将所有这些计数器求和。

I assume you want the count of each number across all sublists. 我假设您想要所有子列表中每个数字的计数。

from collections import Counter
from functools import reduce
import random


L = []
counters = []

for _ in range(0, 100):
    sublist = []

    for y in range(0, 6):
        sublist.append(random.randint(0, 45) + 1)

    counters.append(Counter(sublist))
    L.append(sublist)


def counter_reducer(accumulator, next_item):
    return accumulator + next_item


counts = reduce(counter_reducer, counters, Counter())
print(counts)  # Counter({23: 20, 46: 19, 12: 19, ...})

If i understand your question right - You can create another list with the length of all your possible numbers, now every time that number appended you are adding 1 to the his corresponding index , say: 如果我对您的问题的理解正确-您可以创建一个包含所有可能数字长度的列表,现在每次添加该数字时,您都要在其对应的索引中加1,例如:

count_list = [0] * 47
L = []
for x in range(0, 100):
L.append([])
for y in range(0, 6):
    L[x].append(random.randint(0, 45) + 1)
    count_list[L[x][y]] += 1

now, every index in the coubt_list represent a number and inside him the number of appears 现在,coubt_list中的每个索引都代表一个数字,而在他内部,出现的数字

You can add a List contain the same elements as L and count them using count try this : 您可以添加一个包含与L相同的元素的列表,并使用count对其进行计数,请尝试以下操作:

L = []
e = []
for x in range(0, 10):
    L.append([])
    for y in range(0, 3):
        a = random.randint(0, 45) + 1
        L[x].append(a)
        e.append(a)
for z in e :
print (z,e.count(z))

The easiest way to do this is to make use of the dictionary .get() method to increment values if they have been seen before, or start with a default count of 0. 最简单的方法是使用字典.get()方法增加值(如果以前已看到过这些值.get() ,或者以默认计数0开头。

my_count = {}
for lst in L:
    for item in lst:
        my_count[item] = my_count.get(item, 0) + 1

However, since you're already iterating in this way to create the list, you can do it all together: 但是,由于您已经通过这种方式来创建列表,因此可以一起完成所有操作:

import random

my_count = {}
L = []
for x in range(0, 100):
    L.append([])
    for y in range(0, 6):
        num = random.randint(0, 45) + 1
        L[x].append(num)
        my_count[num] = my_count.get(num, 0) + 1

You could use the histogram function from numpy: 您可以使用numpy的直方图功能:

import numpy as np 
import random
from collections import Counter
L = []
ll, lu = 0, 45 # set bounds for random samples
for x in range(0, 2):
    L.append([])
    for y in range(0, 3):
        L[x].append(random.randint(ll,lu) + 1)


bcen=np.linspace(ll, lu, lu+1) # centers
bed=np.linspace(ll-0.5, lu+0.5, lu+2) #edges
cnts, bed =np.histogram(L, bins=bed)

print('Random integers')
print(L)
print('Counts')
print(np.append(bcen.reshape(-1,1),cnts.reshape(-1,1), axis=1))

Note: I made your list smaller, just so that the print out is readable, but just adjust it as you wish. 注意:我使列表变小了,以使打印输出易于阅读,但可以根据需要进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM