Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using itertools.permutation where r > n

I am trying to generate all permuations of a set of items and the my R needs to be larger than the size of the set of items

Here is an example :

itertools.permutations ("ABC", 4)

this always returns 0 items as R > N.

I want this

[A, A, A, A]

[A, A, A, B]

[A, A, B, A]

[A, B, A, A]

...

How can I achieve this?

like image 212
David Pilkington Avatar asked May 21 '26 00:05

David Pilkington


1 Answers

You don't seem to want the permutation, but the Cartesian product:

itertools.product("ABC", repeat=4)

https://docs.python.org/3/library/itertools.html#itertools.product

like image 156
Veedrac Avatar answered May 22 '26 12:05

Veedrac