-2

I'm looking for code which gives me permutations of the numbers in the range 0-9.

The permutations should be of length x and the numbers can repeat.

I want to get these permutations one by one. When I find the permutations I need, I don't need the remaining ones.

All possibilities I have found so far give me all permutations simultaneously and it takes too much time to generate all of them.

LondonRob
  • 73,083
  • 37
  • 144
  • 201
Tomasz
  • 191
  • 2
  • 12

1 Answers1

2

Use itertools.permutations. It gives permutations one at a time and you can stop whenever you want.

>>> import itertools
>>> x = itertools.permutations(range(3))
>>> next(x)
(0, 1, 2)
>>> next(x)
(0, 2, 1)
>>> next(x)
(1, 0, 2)
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • I try that but i need to get all of posibilities of combination including repeating numbers example: (1,1,1) – Tomasz Dec 28 '15 at 23:11
  • Perhaps `itertools.product` with the `repeat` keyword set to whatever your `x` is? – LondonRob Dec 28 '15 at 23:13
  • I try that but ittertools.prduct give me all of permutation in one time and I can`t take it one by one. – Tomasz Dec 28 '15 at 23:18
  • @Tomasz no it is a generator that by definition will give them one at a time ... this is exactly the right answer ... – Joran Beasley Dec 28 '15 at 23:23