Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(Python)如何制作一个函数来计算随机列表中特定数字的数量?_Python_Random - Fatal编程技术网

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

(Python)如何制作一个函数来计算随机列表中特定数字的数量?,python,random,Python,Random,我的程序是创建一个1-10的随机列表,范围为1000,每个数字必须随机通过1-10,然后一旦完成。我需要确定1000个随机数字中有多少个1、2、3和4、s等 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.randin

我的程序是创建一个1-10的随机列表,范围为1000,每个数字必须随机通过1-10,然后一旦完成。我需要确定1000个随机数字中有多少个1、2、3和4、s等

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 
   

如果您需要更多信息,请在评论中告诉我。

这应该可以做到:

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

首先需要生成列表,最简单的方法如下:

randomList = [random.randint(1, 10) for x in range(1, 1000)]
接下来,您需要计算该数组中每个数字的数量,您可以这样做:

randomCounts = [ (x, randomList.count(x)) for x in range(1, 10)]
这将输出一个元组数组,其中元组的第一个值表示一个数字,第二个值表示该数字在随机列表中出现的频率

然后,您可以执行类似的操作来查找该列表中数字N出现的频率:

amount = randomCounts[n - 1][1]

使用
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}")
输出:

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
>>> 
看来你想:

  • 创建一个包含1000个项目的列表,其中每个项目可以是1到10之间的随机整数
  • 计算该列表中每个整数的数量
  • 因此,让我们创建一个包含1000个项目的列表,其中每个项目可以是1到10之间的随机整数:

    import random
    my_list = [random.randint(1, 10) for x in range(0, 1000)]
    
    然后,让我们计算一下我的列表中有多少个1、2、3、…、10:

    count = [my_list.count(i+1) for i in range(0, 10)]
    
    现在,
    count
    是一个如下列表:

    1的数量 2的数量 ... 10秒数 32 21 ... 43
    这应该会有所帮助:您似乎知道要使用的API,但是您忘记了实际尝试制作一个
    列表,或者计算任何东西。我建议在要求其他人为您编写代码/做家庭作业之前,先阅读Python教程,真正学习Python。学习足够多的知识,自己尝试一下,如果你不能让它发挥作用,请为我们提供帮助;我们不能从头开始一个问题一个问题地教你所有的Python。你应该使用列表,而不是
    l1
    -
    l10
    。谢谢你提供的信息,我很抱歉问了一些非常基本的问题,我将代表我尝试研究更多内容,并感谢你提供的链接。如果这个答案有帮助,请竖起大拇指!)非常正确。你可以考虑使用一个理解(即RealthyList= = [Range.RangTin(1, 10)用于i(范围)(1000)]),但是这个答案与你的Syi知道没有区别,但是对于新的Python程序员来说,这是更可读的:+其他答案已经发布了。也许你误解了我解析结果的形式?我得到了一个类似的输出:[(1116),(288),(3110),(488),(5110),(5100),(5100),(794),(8111),(990)]我在解释中加了一点,解释了如何找到n的计数,希望克莱尔化了伊坦克斯~@Luke,它确实有帮助-不确定是什么搞混了。。。现在工作。