我想帮助找到更好的方法来制作骰子计划的概率。
这是我的所有代码:
import random,collections
probabiltyOfDice = {}
rolls = 100
#Counters
counter1 = 0
counter2 = 0
counter3 = 0
counter4 = 0
counter5 = 0
counter6 = 0
for x in range(rolls):
output = random.randint(1,6)
if output == 1:
counter1 += 1
probabiltyOfDice[1] = counter1
elif output == 2:
counter2 += 1
probabiltyOfDice[2] = counter2
elif output == 3:
counter3 += 1
probabiltyOfDice[3] = counter3
elif output == 4:
counter4 += 1
probabiltyOfDice[4] = counter4
elif output == 5:
counter5 += 1
probabiltyOfDice[5] = counter5
elif output == 6:
counter6 += 1
probabiltyOfDice[6] = counter6
ordered = collections.OrderedDict(sorted(probabiltyOfDice.items()))
print(ordered)
我的方法有效,如果常量滚动数设置为100,则输出为
OrderedDict([(1, 15), (2, 16), (3, 14), (4, 22), (5, 21), (6, 12)])
OrderedDict显示骰子编号以及该编号出现的次数。我能够通过使用6个计数器变量并使用一组if-elif
语句来解决这个问题。
我正在使用集合库来排序字典以进行显示。但是,如果有人能告诉我更好的方法来解决这个问题,我希望如此。
显然,对于这种情况,使用6个变量和许多if
语句可能是一种冗长的方法。
答案 0 :(得分:2)
我们可以通过使用列表来简化代码。这里的列表包含六个元素,最初都设置为零:
import random, collections
rolls = 100
#Counters
counters = [0] * 6
for x in range(rolls):
output = random.randint(1,6)
counters[output-1] += 1
ordered = collections.OrderedDict(enumerate(counters, 1))
print(ordered)
对于跑步,它会在我的机器上打印:
>>> print(ordered)
OrderedDict([(0, 12), (1, 11), (2, 17), (3, 31), (4, 14), (5, 15)])
我们可以通过生成0
和5
之间的数字(包括两者)来轻松提高代码的效率:
rolls = 100
#Counters
counters = [0] * 6
for x in range(rolls):
output = random.randint(0, 5)
counters[output] += 1
ordered = collections.OrderedDict(enumerate(counters, 1))
print(ordered)
答案 1 :(得分:2)
因此,python有一个非常方便的字典子类,名为Counter,实质上,它在计算计数方面非常快。 我精心设计了以下内容:
final WebElement Elementform1 = driver.findElement(By.xpath(k.toString()));
Actions action = new Actions(driver);
action.contextClick(Elementform1).build().perform();
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_DOWN);
//robot.keyRelease(KeyEvent.VK_DOWN);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_DOWN);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_DOWN);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_DOWN);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_ENTER);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
robot.keyRelease(KeyEvent.VK_DOWN);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_ENTER);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
现在,问题在于它只能在合理的大范围内工作。因此,为了避免创建中间列表,我们可以具有以下内容: 随机导入 来自馆藏进口柜台
import random
from collections import Counter
prob = Counter([random.randint(1,6) for _ in range(10000)])
print(prob)
>> Counter({1: 1650, 2: 1750, 3: 1679, 4: 1546, 5: 1686, 6: 1689})
编辑:添加第三个版本,基本上是第一个版本,放弃了更方便的生成器的中间列表,如@Mark Dickinson所示:
prob = Counter()
for _ in range(10000000):
prob[random.randint(1,6)] += 1
print(prob)
>> Counter({1: 13084052,
2: 13087518,
3: 13090177,
4: 13086193,
5: 13081778,
6: 13085341})