简体   繁体   English

itertools中的排列无法打印所有排列

[英]permutations in itertools cant print all permutations

My question is pretty straight forward:- 我的问题很简单:

in python itertools why can I get a print of all permutations for say [,r =3]: 在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)]

However only an empty list for [,r >3 eg 10]: 但是,对于[,r> 3例如10],只有一个空列表:

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

is there away of getting round this?? 有没有解决这个的办法?

As stated in the itertools docs permutations() does not repeat elements. itertools docs中所述, permutations()不会重复元素。

I think you want combinations_with_replacement() 我认为您想要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,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3, 3), (1, 3, 3, 3, 3, 3, 3, 3, 3, 3), (2, 2, 2, 2, 2, 2, 2, 2, 2, 2), (2, 2, 2, 2, 2, 2, 2, 2, 2, 3), (2, 2, 2, 2, 2, 2, 2, 2, 3, 3), (2, 2, 2, 2, 2, 2, 2, 3, 3, 3), (2, 2, 2, 2, 2, 2, 3, 3, 3, 3), (2, 2, 2, 2, 2, 3, 3, 3, 3, 3), (2, 2, 2, 2, 3, 3, 3, 3, 3, 3), (2, 2, 2, 3, 3, 3, 3, 3, 3, 3), (2, 2, 3, 3, 3, 3, 3, 3, 3, 3), (2, 3, 3, 3, 3, 3, 3, 3, 3, 3), (3, 3, 3, 3, 3, 3, 3, 3, 3, 3)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM