简体   繁体   English

计算三个骰子的频率

[英]Calculating the frequency of three dice rolls

I need to create a 6 x 6 x 6 table that has all the 216 possible three dice results.我需要创建一个 6 x 6 x 6 的表,其中包含所有 216 个可能的三个骰子结果。 Considering that the same dice values in whatever order they come is the same result (eg (1,4,6) is the same as all the 1, 4, 6 transformations, ie (6,4,1) or (4,6,1), and so on).考虑到相同的骰子值无论它们出现的顺序如何都是相同的结果(例如 (1,4,6) 与所有 1, 4, 6 转换相同,即 (6,4,1) 或 (4,6 ,1) 等)。

Then I want to calculate the frequency of different results and print them sorted by increasing incidence.(maybe with dictionary?)然后我想计算不同结果的频率并按增加的发生率排序打印它们。(也许用字典?)

I made this list:我做了这个清单:

mylist = [[[(x,y,z) for z in range (1,7)]for y in range(1,7)]for x in range (1,7)]

and I have all the 216 possible results.我有所有 216 种可能的结果。

But I can't make it work for counting them and find the same results...但我不能让它计算它们并找到相同的结果......

Can you help me?你能帮助我吗?

I think you do not have to use 3-dimensional table in this case, I would do it following way我认为在这种情况下您不必使用 3 维表,我会按照以下方式进行

mylist = []
for x in range(1,7):
    for y in range(1,7):
        for z in range(1,7):
            mylist.append((x,y,z))
mylist = [tuple(sorted(i)) for i in mylist]
for i in list(set(mylist)):
    print(i,'occurs',mylist.count(i),'times')

You can use itertools.product to produce all the possible results from rolling 3 dices, pass the results to collections.Counter and then to frozenset to count the composition of the results while ignoring orders, and then use the Counter.elements() method to reconstruct the counts back to readable tuples as keys in a dict comprehension:您可以使用itertools.product产生所有可能的 3 个骰子的结果,将结果传递给collections.Counter ,然后传递给frozenset来计算结果的组成,同时忽略订单,然后使用Counter.elements()方法来计算将计数重建回可读元组作为字典理解中的键:

from itertools import product
from collections import Counter
{tuple(sorted(Counter(dict(t)).elements())): c for t, c in Counter(frozenset(Counter(p).items()) for p in product(range(1, 7), repeat=3)).items()}

This returns:这将返回:

{(1, 1, 1): 1,
 (1, 1, 2): 3,
 (1, 1, 3): 3,
 (1, 1, 4): 3,
 (1, 1, 5): 3,
 (1, 1, 6): 3,
 (1, 2, 2): 3,
 (1, 2, 3): 6,
 (1, 2, 4): 6,
 (1, 2, 5): 6,
 (1, 2, 6): 6,
 (1, 3, 3): 3,
 (1, 3, 4): 6,
 (1, 3, 5): 6,
 (1, 3, 6): 6,
 (1, 4, 4): 3,
 (1, 4, 5): 6,
 (1, 4, 6): 6,
 (1, 5, 5): 3,
 (1, 5, 6): 6,
 (1, 6, 6): 3,
 (2, 2, 2): 1,
 (2, 2, 3): 3,
 (2, 2, 4): 3,
 (2, 2, 5): 3,
 (2, 2, 6): 3,
 (2, 3, 3): 3,
 (2, 3, 4): 6,
 (2, 3, 5): 6,
 (2, 3, 6): 6,
 (2, 4, 4): 3,
 (2, 4, 5): 6,
 (2, 4, 6): 6,
 (2, 5, 5): 3,
 (2, 5, 6): 6,
 (2, 6, 6): 3,
 (3, 3, 3): 1,
 (3, 3, 4): 3,
 (3, 3, 5): 3,
 (3, 3, 6): 3,
 (3, 4, 4): 3,
 (3, 4, 5): 6,
 (3, 4, 6): 6,
 (3, 5, 5): 3,
 (3, 5, 6): 6,
 (3, 6, 6): 3,
 (4, 4, 4): 1,
 (4, 4, 5): 3,
 (4, 4, 6): 3,
 (4, 5, 5): 3,
 (4, 5, 6): 6,
 (4, 6, 6): 3,
 (5, 5, 5): 1,
 (5, 5, 6): 3,
 (5, 6, 6): 3,
 (6, 6, 6): 1}

One way to do it using the stuff I've mentioned:使用我提到的东西的一种方法:

from collections import Counter

a = [(i,j,k) for i in range(1, 7) for j in range(1, 7) for k in range(1, 7)]
c = Counter(frozenset(x) for x in a if len(set(x)) == 3)

counter_most_common = c.most_common(1)[0][1]
for combination, counter in c.items():
    if counter == counter_most_common:
        print(f"{combination} is one of the most common combinations! It exists {counter} times")

As you can see, they're all equally likely, which makes perfect sense if you think about it如您所见,它们的可能性都相同,如果您考虑一下,这很有意义

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

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