简体   繁体   English

(Python)如何制作 function 来计算随机列表中特定数字的数量?

[英](Python)How do I make a function to count the amount of specific numbers in a randomized list?

My program is to create a randomized list of 1-10 for a range of 1000, each number has to be randomized through 1-10 then once that is done.我的程序是为 1000 的范围创建一个 1-10 的随机列表,每个数字必须通过 1-10 随机化,然后一旦完成。 I need to identify how many 1s, 2s,3s, and 4,s, etc there are in the 1000 randomized numbers.我需要确定 1000 个随机数字中有多少个 1、2、3 和 4,等等。

import random
#Printing a random list of 1 through 10
#loop to loop 1000 times
for i in range(1000):
  #lisitng random numbers 1 through 10 inclusive 
  l1 = random.randint(1, 10)
  l2 = random.randint(1, 10)
  l3 = random.randint(1, 10)
  l4 = random.randint(1, 10)
  l5 = random.randint(1, 10)
  l6 = random.randint(1, 10)
  l7 = random.randint(1, 10)
  l8 = random.randint(1, 10)
  l9 = random.randint(1, 10)
  l10 = random.randint(1, 10)
  #printing the numbers in a list form 
  print(l1,l2,l3,l4,l5,l6,l7,l8,l9,l10)
  #A  function to identify how many X are there in each number 
   

If you need more information on this please tell me in the comments.如果您需要这方面的更多信息,请在评论中告诉我。

First you need to generate the list, simplest way to do this is like so:首先你需要生成列表,最简单的方法是这样的:

randomList = [random.randint(1, 10) for x in range(1, 1000)]

Next you need to count how many of each number there are in that array, you can do that like this:接下来,您需要计算该数组中每个数字的数量,您可以这样做:

randomCounts = [ (x, randomList.count(x)) for x in range(1, 10)]

This will output an array of tupples, where the first value of the tuple represents a number, and the second represents how often that number occurs in the randomList.这将 output 元组数组,其中元组的第一个值表示一个数字,第二个表示该数字在 randomList 中出现的频率。

You could then do something like this to find how often the number N occured in that list:然后,您可以执行以下操作来查找数字 N 在该列表中出现的频率:

amount = randomCounts[n - 1][1]

This should do the trick:这应该可以解决问题:

import random

random_list = []

for i in range(1000):
    random_list.append(random.randint(1, 10))

for i in range(10):
    print("{} was in the list exactly {} times.".format(i+1, random_list.count(i+1)))

Use collections.Counter to tally the occurrences of each number, and use random.choices to create your random numbers within a range:使用collections.Counter计算每个数字的出现次数,并使用random.choices在一个范围内创建随机数:

from collections import Counter
from random import choices

counter = Counter(choices(range(1, 10 + 1), k=1000))

for key, value in sorted(counter.items()):
    print(f"Number of {key}s: {value}")

Output: Output:

Number of 1s: 87
Number of 2s: 102
Number of 3s: 93
Number of 4s: 116
Number of 5s: 109
Number of 6s: 79
Number of 7s: 105
Number of 8s: 105
Number of 9s: 104
Number of 10s: 100
>>> 

It seems you want to:您似乎想要:

  1. create a list of 1000 items where each item can be a random integer in the range from 1 to 10.创建一个包含 1000 个项目的列表,其中每个项目可以是 1 到 10 范围内的随机 integer。
  2. count how many of each integer there are in that list.计算该列表中有多少个 integer。

So, let's create a list of 1000 items where each item can be a random integer in the range from 1 to 10:因此,让我们创建一个包含 1000 个项目的列表,其中每个项目可以是 1 到 10 范围内的随机 integer:

import random
my_list = [random.randint(1, 10) for x in range(0, 1000)]

Then, let's count how many 1s, 2s, 3s, ..., 10s there are in my_list :然后,让我们数一下my_list中有多少个 1、2、3、...、10:

count = [my_list.count(i+1) for i in range(0, 10)]

Now count is a list like:现在count是一个类似的列表:

Number of 1s 1s 的数量 Number of 2s 2s数 ... ... Number of 10s 10s 数量
32 32 21 21 ... ... 43 43

If you want to print (python 3.x):如果要打印(python 3.x):

for idx, number in enumerate(count):
    print(f'There are {number} item of {idx+1}s')

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

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