Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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/list/4.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_List_Random_Generator - Fatal编程技术网

Python 从可能结果列表中随机选择时,某个结果出现的次数

Python 从可能结果列表中随机选择时,某个结果出现的次数,python,list,random,generator,Python,List,Random,Generator,我的代码生成列表“结果”的随机结果。我正在运行10000+个批次,我需要一种方法来计算批次并在底部合计它们。我该怎么做 这是我的密码: import random import time from time import sleep outcomes = ['TT','Tt','Tt','tt'] for x in range (0, 5): b = "LOADING OUTCOMES" + "." * x print (b, end="\r") time.slee

我的代码生成列表“结果”的随机结果。我正在运行10000+个批次,我需要一种方法来计算批次并在底部合计它们。我该怎么做

这是我的密码:

import random
import time
from time import sleep
outcomes = ['TT','Tt','Tt','tt']
for x in range (0, 5):  
    b = "LOADING OUTCOMES" + "." * x
    print (b, end="\r") 
    time.sleep(0.5)
print("4 Possible Genetic Outcomes Will Be Shown. . . ") ; sleep(1)
for x in range (0, 10000):
    print(random.choice(outcomes))
    time.sleep(0.001)        
x=input("Done determining outcomes!! Press enter to close")
这就产生了

Counter({'TT': 2528, 'Tt': 4914, 'tt': 2558})

这是使用您在屏幕截图中提供的代码。下面是我将如何着手的。检查此解决方案是否适用于您。下次请将代码放在问题本身中,而不是作为图像。这将吸引更多的人来帮助你,因为他们可以复制粘贴你的代码,帮助你更快,而不是自己打字

我是如何解决的: 已使用列表中的可能选项预定义词典。每次出现选项时,只需将计数器增加1。最后,打印所有可能的内容。您可以使用循环来执行此操作,但由于只有3个元素,所以我决定将它们打印出来

import random
import time
from time import sleep

outcomes = ["TT", "Tt", "Tt", "tt"]
outcomesCount = {"TT":0, "Tt":0, "tt":0}

for x in range(0,5):
    b = "LOADING OUTCOMES" + "." * x
    print(b, end="\r")
    time.sleep(0.5)
print("4 Possible Genetic Outcomes Will Be Shown. . . ")
sleep(1)

for x in range(0,10000):
    choice = (random.choice(outcomes))
    time.sleep(0.001)
    outcomesCount[choice] += 1
    print(choice) #This is something you were doing in original code. I would not do this because there are too many print statements, and will cause the program to run for a while.

print("The number of times each outcome appeared was: ")
print("TT : %s" %(outcomesCount["TT"]))
print("Tt : %s" %(outcomesCount["Tt"]))
print("tt : %s" %(outcomesCount["tt"]))
x = input("Done determining outcomes!! Press enter to close")
运行上述程序的输出为注意,这只是最后一次打印语句

The number of times each outcome appeared was: 

TT : 2484
Tt : 4946
tt : 2570
Done determining outcomes!! Press enter to close
改进: 1.摆脱睡眠,因为你只是在延迟程序的执行。在那里你不需要它。如果您想让用户在两秒钟内看到加载消息,您可以在末尾添加一个暂停

  • 第二个for循环中的睡眠根本不需要。这是一台计算机,能够做令人惊奇的事情。与它所能处理的相比,这算不了什么

  • 不要打印所有结果,因为它将打印10000个不同的行


  • 祝你好运,希望这有帮助。

    复制问题中的代码。不必打开外部链接来了解发生了什么。查看您已有的内容会很有帮助,这样我们可以帮助您改进代码。另外,您希望您的代码做什么?打印每个结果的总数?TT-1 TT-2 TT-16?我试图发布代码,但它没有正确显示,所以我发布了一个pcitureNice解决方案。比我提供的要干净得多。我该如何添加这个?当我用它而不是deltas solutionscript运行时,会出现错误。我不记得错误了。Ill x2检查文件“C:/Windows/System32/test1.py”,第41行,总计=计数器(范围内(10000)的选项(结果))文件“C:\Users\USER\AppData\Local\Programs\Python\Python35-32\lib\collections\u init\uuds.py”,第523行,在init self.update(*args,**kwds)文件中“C:\Users\User\AppData\Local\Programs\Python35-32\lib\collections\u init\uuuu.py”,第610行,在更新计数元素(自身,可编辑)文件“C:/Windows/System32/test1.py”第41行中,总计=计数器(范围内(10000)的选项(结果))TypeError:“str”对象在我的代码中是不可调用的,
    choice
    是一个函数;在你的代码中,它显然是一个字符串。@KYHS,很高兴我能帮助你。我还为人们编辑了这个问题,以便将来他们知道你的意思。这个问题一开始并不清楚。谢谢你,伙计。现在真的很干净,很快。另外,对于那些好奇的人来说,这是成功的这是一个糟糕的剧本,因为我最初制作它是为了给我的朋友列一个侮辱的列表,我意识到我可以为bio做这个。-这也是为什么它有睡眠的原因。我想让他每隔几秒钟看到一次侮辱,但我没想过要拉它
    The number of times each outcome appeared was: 
    
    TT : 2484
    Tt : 4946
    tt : 2570
    Done determining outcomes!! Press enter to close