我怎样才能获得“重复/替换的排列”?来自列表(列表与其自身的笛卡尔积)?
假设我有一个列表die_faces = [1, 2, 3, 4, 5, 6] 。我想生成掷两个骰子的所有 36 种可能结果:(1, 1)
、(1, 2)
、(2, 1)
等等。如果我尝试使用 itertools
标准库中的 permutations
:
>>> import itertools
>>> die_faces = [1, 2, 3, 4, 5, 6]
>>> list(itertools.permutations(die_faces, 2))
[(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5)]
只有 30 个结果,缺少两个骰子上出现相同数字的结果。看起来它只生成排列而没有重复。我该如何解决这个问题?
Suppose I have a list die_faces = [1, 2, 3, 4, 5, 6]
. I want to generate all 36 possible results for rolling two dice: (1, 1)
, (1, 2)
, (2, 1)
etc. If I try using permutations
from the itertools
standard library:
>>> import itertools
>>> die_faces = [1, 2, 3, 4, 5, 6]
>>> list(itertools.permutations(die_faces, 2))
[(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5)]
there are only 30 results, missing the ones where the same number comes up on both dice. It seems that it only generates permutations without repetitions. How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您正在寻找笛卡尔积。
在您的情况下,这将是
{1, 2, 3, 4, 5, 6}
x{1, 2, 3, 4, 5, 6}
。itertools
可以帮助你:获得随机骰子(以完全低效的方式):
You are looking for the Cartesian Product.
In your case, this would be
{1, 2, 3, 4, 5, 6}
x{1, 2, 3, 4, 5, 6}
.itertools
can help you there:To get a random dice roll (in a totally inefficient way):
您不是在寻找排列 - 您需要笛卡尔积。为此,请使用 itertools 的 产品:
You're not looking for permutations - you want the Cartesian Product. For this use product from itertools:
在 python 2.7 和 3.1 中,有一个
itertools.combinations_with_replacement
功能:In python 2.7 and 3.1 there is a
itertools.combinations_with_replacement
function:在这种情况下,并不是特别需要列表理解。
给定
代码
详细信息
显然,笛卡尔积可以生成排列的子集。然而,它遵循以下原则:
product
生成所有排列 nr带替换的排列,nr< /sup>
无需替换的排列,n!
因此,所有组合函数都可以从
product
实现:combinations_with_replacement
实现于product
组合
由排列
实现,可以使用product
来实现(见上文)In this case, a list comprehension is not particularly needed.
Given
Code
Details
Unobviously, Cartesian product can generate subsets of permutations. However, it follows that:
product
Permutations with replacement, nr
Permutations without replacement, n!
Consequently, all combinatoric functions could be implemented from
product
:combinations_with_replacement
implemented fromproduct
combinations
implemented frompermutations
, which can be implemented withproduct
(see above)我想我找到了一个仅使用 lambdas、map 和 reduce 的解决方案。
本质上,我正在映射第一个 lambda 函数,该函数给定一行,迭代列,
然后将其用作新 lambda 函数的输出
,该函数映射到所有可能的行
,然后我们将所有结果列表减少为一个。
更好的是
还可以使用两个不同的数字。
I think I found a solution using only
lambdas
,map
andreduce
.Essentially I'm mapping a first lambda function that given a row, iterates the columnns
then this is used as the output of a new lambda function
which is mapped across all the possible rows
and then we reduce all the resulting lists into one.
even better
Can also use two different numbers.
首先,您需要首先将 itertools.permutations(list) 返回的生成器转换为列表。其次,您可以使用 set() 删除重复项
像下面这样:
First, you'll want to turn the generator returned by itertools.permutations(list) into a list first. Then secondly, you can use set() to remove duplicates
Something like below: