i have written simple python program:
#!/usr/bin/env python3.5 import sys, itertools scharacters = '123' icombinationlength in range(0, len(scharacters)+1): acombination in itertools.combinations_with_replacement(scharacters, icombinationlength): print(''.join(acombination))
this outputs following:
1 2 3 11 12 13 22 23 33 111 112 113 122 123 133 222 223 233 333
however combinations of numbers 1 2 , 3, need include:
311 312 313 321 322 323 331 332 333
and can see above, not. have seen other posts giving combinations_with_replacement function given solution possible combinations of characters passed in. yet not seem happening. doing wrong here, , how can possible combinations of characters in characters variable?
thanks time ;-)
"combinations" order-insensitive term; if have 113
, don't need 131
or 311
, because of them same "combination" (if input sequence combinations_with_replacement
unique, view outputs being unique values after converting collections.counter
; regardless of order, 2 1
s , 3
collections.counter({1: 2, 3:1})
).
if want order sensitive version of combinations_with_replacement
(so 113
, 131
, 311
separate outputs), use itertools.product
repeat
argument (repeat
must passed keyword due design of product
, takes variable length positional arguments):
scharacters = '123' icombinationlength in range(0, len(scharacters)+1): acombination in itertools.product(scharacters, repeat=icombinationlength): print(''.join(acombination))