排列重复?

问题描述 投票:1回答:3

我可以使用itertools做到这一点:

list(permutations([1,2,3],2))
: [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

但是我怎么也生成:

 (1,1),(2,2),(3,3)

当然不用另外做:[[i,i)for range(4)中的i]

python repeat itertools permute
3个回答
3
投票

添加到Nakor的注释中,看起来您想要的是cartesian product。您可以用list(itertools.product([1,2,3],repeat=2))来获得。


0
投票

Nakor得到了正确的答案:


0
投票

您正在寻找permutations_with_replacement工具。

© www.soinside.com 2019 - 2024. All rights reserved.