Reputation:
let say I have string number with "323123"
if I want to get the fixed number n digit permutation of "323123".
for example if n=3 '323','321'.....'123' <br/>
n=2 then '32',23'.....
Upvotes: 1
Views: 73
Reputation: 1291
The desired output (combinations as single strings) can be obtained like this (for the example case n = 2
):
import itertools as it
num = "323123"
n = 2
perms = ["".join(el) for el in it.permutations(list(num), n)]
print(perms)
Which prints:
['32', '33', '31', '32', '33', '23', '23', '21', '22', '23', '33', '32', '31', '32', '33', '13', '12', '13', '12', '13', '23', '22', '23', '21', '23', '33', '32', '33', '31', '32']
Upvotes: 0
Reputation: 126
Try this:
from itertools import permutations
string = "323123"
n = 2
perms = list(permutations(string, n))
Output:
[('3', '2'), ('3', '3'), ('3', '1'), ('3', '2'), ('3', '3'), ('2', '3'), ('2', '3'), ('2', '1'), ('2', '2'), ('2', '3'), ('3', '3'), ('3', '2'), ('3', '1'), ('3', '2'), ('3', '3'), ('1', '3'), ('1', '2'), ('1', '3'), ('1', '2'), ('1', '3'), ('2', '3'), ('2', '2'), ('2', '3'), ('2', '1'), ('2', '3'), ('3', '3'), ('3', '2'), ('3', '3'), ('3', '1'), ('3', '2')]```
Upvotes: 2