Reputation: 37
There are many examples how to get all combinations of n items of length l, if l <= n. But how about get all combinations of n items (f.i. digits) of lenght l, where n < l, for instance:
n = 3 ([0,1,2])
l = 5
sequence to get:
00000
00001
00002
00010
...
22222
It is not a problem to implement it, but I suppose it is possible to do using itertools. I can not find how.
Upvotes: 0
Views: 59
Reputation: 233
Since this set is essentially just a ternary number system, we can use a ternary function from here
def ternary (n):
if n == 0:
return '0'
nums = []
while n:
n, r = divmod(n, 3)
nums.append(str(r))
return ''.join(reversed(nums))
And you can treat this as an array, and iterate to it through a for loop that goes something like this:
for i in range(3**5):
print(ternary(i))
Upvotes: 0
Reputation: 167
From itertools documentation :
product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
So for your case the code will be
itertools.product(range(3), repeat=5)
Upvotes: 1