我可以使用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]
添加到Nakor的注释中,看起来您想要的是cartesian product。您可以用list(itertools.product([1,2,3],repeat=2))
来获得。
Nakor得到了正确的答案:
您正在寻找permutations_with_replacement
工具。