Generating combinations with repetetions in python -
i generated combination of numbers, example 123
, using code
from itertools import combinations in set(combinations('123',2)): print(''.join(i))
i desired output here
13 12 23
but when use 133
, get
13 33
but want ignore repetition, want output
13 13 33
is there alternate approach?
set()
s nature, don't allow duplicate elements. each element in a set()
must unique. python documentation makes note of this:
python includes data type sets. set unordered collection with no duplicate elements.
emphasis mine. why not getting expected output. when call set()
, removes duplicate 13
combinations. instead, iterate through combination object is:
from itertools import combinations in combinations('133', 2): # no call set() print(''.join(i))
which outputs:
13 13 33
Comments
Post a Comment