[英]Summing elements of lists in list using zip(), map() functions and itertools
I'm writing a program that looks at every possible sum of a dice roll given X die with X sides. 我正在编写一个程序,该程序查看给定的X面带有X面的骰子掷骰的所有可能和。 For example, with 2 dice with 6 sides, I need a list of elements giving all possible unique sums achievable with these dice (something like 2,3,3,4,4,4,4 etc..).
例如,对于2个骰子和6个面,我需要一个元素列表,给出这些骰子可以实现的所有可能的唯一总和(例如2,3,3,4,4,4,4,4等。)。 I can do this for smaller sets (2 dice 6 sides is fine), but larger sets like 10 dice and 10 sides I run into mem issues.
我可以为较小的集合(2个骰子6个面都可以)执行此操作,但是较大的集合(如10个骰子和10个面)会遇到内存问题。 I think I've found the solution, but am having issues with implementation.
我想我已经找到了解决方案,但是在实现方面遇到了问题。
I have a list of lists containing all rolls of each individual dice. 我有一个列表列表,其中包含每个骰子的所有掷骰。 For this example, we have 3 dice that can roll a 1, 2, or 3.
对于此示例,我们有3个骰子可以掷1、2或3。
dice = [[1,2,3], [1,2,3], [1,2,3]]
outcomes = list(map(sum, zip(itertools.product(*dice))))
I think the pieces are there, but I keep getting TypeError: unsupported operand type(s) for +: 'int' and 'tuple'. 我认为这些部分已经存在,但我不断收到TypeError:+不支持的操作数类型:“ int”和“ tuple”。 I've tried moving the pieces around in numerous ways but nothing has clicked.
我尝试过以多种方式移动这些片段,但是没有任何点击。 What am I doing wrong?
我究竟做错了什么? Ultimately I'm given a number, say 6, that I have to find out what the odds are I'll roll that number.
最终,我得到一个数字,比如6,我必须找出将这个数字翻几率的几率。 So my goal with the above code is to get a generator that I get the total times X number appeared and figure out the probability of rolling X number.
所以我上面的代码的目标是得到一个生成器,让我得到出现X数的总次数,并弄清楚滚动X数的可能性。 I've gotten a list for smaller inputs, but like I said, once I get larger inputs the list gets too big
我已经获得了较小输入的列表,但是就像我说的那样,一旦获得较大输入,列表就会变得太大
You don't need zip
here. 您不需要在这里
zip
。 Map the sequence of tuples generated by itertools.product
to sum
directly and it will be the sequence of sums you're looking for: 映射
itertools.product
生成的元组序列以直接sum
,这将是您要寻找的求和序列:
outcomes = list(map(sum, itertools.product(*dice)))
For any given set of dice, you know that the minimum possible sum of the dice roll will be the min_value * num_dice
. 对于任何给定的骰子集合,您都知道骰子掷骰的最小和为
min_value * num_dice
。 For example, if you have 2 dice with 6 sides, the minimum dice sum value will be 1 * 2 = 2
. 例如,如果您有2个骰子带有6个边,则最小骰子总和值为
1 * 2 = 2
。 Similarly, you know that the maximum value will be max_value * num_dice
. 同样,您知道最大值将是
max_value * num_dice
。 For example, if you have 2 dice with 6 sides, the max dice sum value will be 6 * 2 = 12
. 例如,如果您有2个骰子带有6个边,则最大骰子总和值为
6 * 2 = 12
。
Furthermore, if we can assume that the dice all have the same values and all each side has a unique number and the numbers are in the range [1, number_of_sides]
, then every whole number value between min_value
and max_value
will be accounted for. 此外,如果我们可以假设,骰子都具有相同的值和所有各边都有一个唯一的号码和号码的范围是
[1, number_of_sides]
然后之间的每一个整数值min_value
和max_value
将被考虑。
Hence, in python, you can simply list all the unique sums of n
dice with x
sides as follows: 因此,在python中,您可以简单地列出带有
x
边的n
骰子的所有唯一和,如下所示:
def all_possible_sums(number_of_dice: int, number_of_sides: int) -> List[int]:
return list(range(number_of_dice, number_of_sides * number_of_dice + 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.