我怎样才能获得“重复/替换的排列”?来自列表(列表与其自身的笛卡尔积)?

发布于 2024-09-06 20:22:41 字数 652 浏览 3 评论 0原文

假设我有一个列表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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

顾挽 2024-09-13 20:22:41

您正在寻找笛卡尔积

在数学中,笛卡尔积(或积集)是两个集合的直接乘积。

在您的情况下,这将是 {1, 2, 3, 4, 5, 6} x {1, 2, 3, 4, 5, 6}
itertools 可以帮助你:

import itertools
x = [1, 2, 3, 4, 5, 6]
[p for p in itertools.product(x, repeat=2)]
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), 
 (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), 
 (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), 
 (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]

获得随机骰子(以完全低效的方式):

import random
random.choice([p for p in itertools.product(x, repeat=2)])
(6, 3)

You are looking for the Cartesian Product.

In mathematics, a Cartesian product (or product set) is the direct product of two sets.

In your case, this would be {1, 2, 3, 4, 5, 6} x {1, 2, 3, 4, 5, 6}.
itertools can help you there:

import itertools
x = [1, 2, 3, 4, 5, 6]
[p for p in itertools.product(x, repeat=2)]
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2), (2, 3), 
 (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), 
 (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (5, 1), (5, 2), (5, 3), 
 (5, 4), (5, 5), (5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]

To get a random dice roll (in a totally inefficient way):

import random
random.choice([p for p in itertools.product(x, repeat=2)])
(6, 3)
对你而言 2024-09-13 20:22:41

您不是在寻找排列 - 您需要笛卡尔积。为此,请使用 itertools 的 产品

from itertools import product
for roll in product([1, 2, 3, 4, 5, 6], repeat = 2):
    print(roll)

You're not looking for permutations - you want the Cartesian Product. For this use product from itertools:

from itertools import product
for roll in product([1, 2, 3, 4, 5, 6], repeat = 2):
    print(roll)
離殇 2024-09-13 20:22:41

在 python 2.7 和 3.1 中,有一个 itertools.combinations_with_replacement 功能:

>>> list(itertools.combinations_with_replacement([1, 2, 3, 4, 5, 6], 2))
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 2), (2, 3), (2, 4), 
 (2, 5), (2, 6), (3, 3), (3, 4), (3, 5), (3, 6), (4, 4), (4, 5), (4, 6),
 (5, 5), (5, 6), (6, 6)]

In python 2.7 and 3.1 there is a itertools.combinations_with_replacement function:

>>> list(itertools.combinations_with_replacement([1, 2, 3, 4, 5, 6], 2))
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 2), (2, 3), (2, 4), 
 (2, 5), (2, 6), (3, 3), (3, 4), (3, 5), (3, 6), (4, 4), (4, 5), (4, 6),
 (5, 5), (5, 6), (6, 6)]
美男兮 2024-09-13 20:22:41

在这种情况下,并不是特别需要列表理解。

给定

import itertools as it


seq = range(1, 7)
r = 2

代码

list(it.product(seq, repeat=r))

详细信息

显然,笛卡尔积可以生成排列的子集。然而,它遵循以下原则:

  • 带替换:通过 product 生成所有排列 nr
  • 而无需替换:从后者过滤

带替换的排列,nr< /sup>

[x for x in it.product(seq, repeat=r)]

无需替换的排列,n!

[x for x in it.product(seq, repeat=r) if len(set(x)) == r]
# Equivalent
list(it.permutations(seq, r))  

因此,所有组合函数都可以从 product 实现:

In this case, a list comprehension is not particularly needed.

Given

import itertools as it


seq = range(1, 7)
r = 2

Code

list(it.product(seq, repeat=r))

Details

Unobviously, Cartesian product can generate subsets of permutations. However, it follows that:

  • with replacement: produce all permutations nr via product
  • without replacement: filter from the latter

Permutations with replacement, nr

[x for x in it.product(seq, repeat=r)]

Permutations without replacement, n!

[x for x in it.product(seq, repeat=r) if len(set(x)) == r]
# Equivalent
list(it.permutations(seq, r))  

Consequently, all combinatoric functions could be implemented from product:

玻璃人 2024-09-13 20:22:41

我想我找到了一个仅使用 lambdas、map 和 reduce 的解决方案。

product_function = lambda n: reduce(lambda x, y: x+y, map(lambda i: list(map(lambda j: (i, j), np.arange(n))), np.arange(n)), [])

本质上,我正在映射第一个 lambda 函数,该函数给定一行,迭代列,

list(map(lambda j: (i, j), np.arange(n)))

然后将其用作新 lambda 函数的输出

lambda i:list(map(lambda j: (i, j), np.arange(n)))

,该函数映射到所有可能的行

map(lambda i: list(map(lambda j: (i, j), np.arange(n))), np.arange(m))

,然后我们将所有结果列表减少为一个。

更好的是

还可以使用两个不同的数字。

prod= lambda n, m: reduce(lambda x, y: x+y, map(lambda i: list(map(lambda j: (i, j), np.arange(m))), np.arange(n)), [])

I think I found a solution using only lambdas, map and reduce.

product_function = lambda n: reduce(lambda x, y: x+y, map(lambda i: list(map(lambda j: (i, j), np.arange(n))), np.arange(n)), [])

Essentially I'm mapping a first lambda function that given a row, iterates the columnns

list(map(lambda j: (i, j), np.arange(n)))

then this is used as the output of a new lambda function

lambda i:list(map(lambda j: (i, j), np.arange(n)))

which is mapped across all the possible rows

map(lambda i: list(map(lambda j: (i, j), np.arange(n))), np.arange(m))

and then we reduce all the resulting lists into one.

even better

Can also use two different numbers.

prod= lambda n, m: reduce(lambda x, y: x+y, map(lambda i: list(map(lambda j: (i, j), np.arange(m))), np.arange(n)), [])
递刀给你 2024-09-13 20:22:41

首先,您需要首先将 itertools.permutations(list) 返回的生成器转换为列表。其次,您可以使用 set() 删除重复项
像下面这样:

def permutate(a_list):
    import itertools
    return set(list(itertools.permutations(a_list)))

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:

def permutate(a_list):
    import itertools
    return set(list(itertools.permutations(a_list)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文