permutation - Is there an efficient way of generating numbers with repeated digits in Python? -
i trying generate 5 digit numbers out of {1,2,3,4,5,6,7,8,9}
such digit can repeated @ twice. is, numbers 12345, 11267, 11226
allowed 11134 or 11115
not allowed.
i can write code multiple loops perhaps, wondering if there shorter, more elegant methods available. example, itertools.product('123456789', repeat=5)
generate such 5 tuples (9^5
in total) or itertools.permutations(''123456789',5)
generate 5
tuples no repetitions (9x8x7x6x5
in total). wondering there way use these tools generate numbers of form 11235 , 11224
nothing else without going through multiple loops , like.
unless i've misunderstood question, using
itertools.permutations('112233445566778899',5)
should trick.
edit: included repeats, since had 2 of each digit. wrapping in set seems solve that:
set(itertools.permutations('112233445566778899',5))
Comments
Post a Comment