itertools中的排列不能打印所有排列

时间:2015-03-11 13:33:45

标签: python itertools

我的问题很简单: -

在python itertools中为什么我可以得到所有排列的印数[:r = 3]:

>>>import itertools
>>>print list(itertools.permutations([1,2,3], 3))
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

但是只有[,r> 3例如10]的空列表:

>>>print list(itertools.permutations([1,2,3], 10))
[] 

离开这个?

1 个答案:

答案 0 :(得分:0)

正如itertools docs permutations()中所述,不重复元素。

我想你想要combinations_with_replacement()

>>> from itertools import combinations_with_replacement

>>> print list(combinations_with_replacement([1, 2, 3], 10))

[(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1, 2), (1, 1, 1, 1, 1, 1, 1, 1, 1, 3), (1, 1, 1, 1, 1, 1, 1, 1, 2, 2), (1, 1, 1, 1, 1, 1, 1, 1, 2, 3), (1, 1, 1, 1, 1, 1, 1, 1, 3, 3), (1, 1, 1, 1, 1, 1, 1, 2, 2, 2), (1, 1, 1, 1, 1, 1, 1, 2, 2, 3), (1, 1, 1, 1, 1, 1, 1, 2, 3, 3), (1, 1, 1, 1, 1, 1, 1, 3, 3, 3), (1, 1, 1, 1, 1, 1, 2, 2, 2, 2), (1, 1, 1, 1, 1, 1, 2, 2, 2, 3), (1, 1, 1, 1, 1, 1, 2, 2, 3, 3), (1, 1, 1, 1, 1, 1, 2, 3, 3, 3), (1, 1, 1, 1, 1, 1, 3, 3, 3, 3), (1, 1, 1, 1, 1, 2, 2, 2, 2, 2), (1, 1, 1, 1, 1, 2, 2, 2, 2, 3), (1, 1, 1, 1, 1, 2, 2, 2, 3, 3), (1, 1, 1, 1, 1, 2, 2, 3, 3, 3), (1, 1, 1, 1, 1, 2, 3, 3, 3, 3), (1, 1, 1, 1, 1, 3, 3, 3, 3, 3), (1, 1, 1, 1, 2, 2, 2, 2, 2, 2), (1, 1, 1, 1, 2, 2, 2, 2, 2, 3), (1, 1,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