In your example, you are looking more as mapping the product than permutations.
The specific example given can be converted to a function that will give you the bits of an integer (without having to store anything):
def bits(n,w=16): return [*map(int,f'{n:0{w}b}')]
bits(5,3) # [1, 0, 1]
bits(13,8) # [0, 0, 0, 0, 1, 1, 0, 1]
For a more generalized virtual lists of 'products', you can make a function to get the Nth product from a list of sequences which will cover your example and more:
def prodAtIndex(i,L):
result = [] # product fed in reversed order
for v in reversed(L): # from least significant to most
i,p = divmod(i,len(v)) # get digit in current 'base'
result.insert(0,v[p]) # build product
return result if i==0 else None # i will end at zero if in range
prodAtIndex(16,[(0,1,2)]*3) # [1,2,1]
prodAtIndex(13,[(0,1)]*8) # [0, 0, 0, 0, 1, 1, 0, 1]
prodAtIndex(10,["ABCD",(1,2,3)]) # ['D', 2]
A class can be made to manipulate virtual lists of products more seamlessly:
class Products:
def __init__(self,*values):
self.values = values
def __len__(self):
result = 1
for s in map(len,self.values): result *= s
return result
def __getitem__(self,index):
if isinstance(index,slice):
return (self[i] for i in range(len(self))[index])
if index<0: index += len(self)
result = [] # product fed in reversed order
for v in reversed(self.values): # from least significant to most
index,p = divmod(index,len(v)) # get digit in current 'base'
result.insert(0,v[p]) # build product
if index: raise IndexError # will be zero if in range
return tuple(result)
def index(self,P):
index = 0
for v,p in zip(self.values,P):
index *= len(v)
index += v.index(p)
return index
Usage:
p = Products("ABCD",(1,2,3,4,5))
len(p) # 20
p[4] # ('A', 5)
p[-4] # ('D', 2)
p.index(['A',5]) # 4
for prod in p[3:5]: print(prod)
('A', 4)
('A', 5)
# For your example...
m = 3
n = 2
b3 = Products(*[range(n)]*m)
b3[5] # [1, 0, 1]
Permutations?
If you actually want to manipulate permutations as if you had a list but without generating the actual list, you can make a function that will give you then Nth permutation in a "virtual" list:
from math import factorial
def permCount(A,k): return factorial(len(A))//factorial(len(A)-k)
def permAtIndex(A,k,index):
A = list(A)
base = permCount(A,k)
result = []
for _ in range(k): # for required size
base //= len(A) # chunk size for position
i,index = divmod(index,base or 1) # i is value index at position
result.append(A.pop(i)) # build permutation
return result
This will give you the size of the permutation "list" and allow you to get a permutation at a given index:
print(permCount([1,2,3,4],4)) # 24
print(permAtIndex([1,2,3,4],4,22)) # [4, 3, 1, 2]
print(permCount(range(15),10)) # 10897286400
print(permAtIndex(range(15),10,5897286400)) # [8,1,10,5,12,0,13,7,14,9]
You can also create a reverse function to give you the index of a given permutation:
def indexOfPerm(A,k,P):
A = list(A)
base = permCount(A,k)
result = 0
for n in P:
base //= len(A) # chunk size for position
i = A.index(n) # index in remaining items
result += base*i # position's component of index
A.pop(i) # next position on rest of items
return result
print(indexOfPerm([1,2,3,4],4,[4, 3, 1, 2])) # 22
print(indexOfPerm(range(15),10,[8,1,10,5,12,0,13,7,14,9])) # 5897286400
All this can be turned into a class that allows manipulation of virtual lists of permutations:
from math import factorial as fact
class Permutations:
def __init__(self,values,size):
self.values = values
self.size = size
def __len__(self):
return fact(len(self.values))//fact(len(self.values)-self.size)
def __getitem__(self,index):
if isinstance(index,slice):
return (self[i] for i in range(len(self))[index])
A = list(self.values)
base = len(self)
value = []
if index<0: index += len(self)
for _ in range(self.size): # for required size
base //= len(A) # chunk size for position
i,index = divmod(index,base or 1) # i is value index at position
value.append(A.pop(i)) # build permutation
return tuple(value)
def index(self,P):
A = list(self.values)
base = len(self)
index = 0
for n in P:
base //= len(A) # chunk size for position
i = A.index(n) # index in remaining items
index += base*i # position's component of index
A.pop(i) # next position on rest of items
return index
Usage:
p = Permutations(range(15),10)
print(len(p)) # 10897286400
print(p[5897286400]) # (8, 1, 10, 5, 12, 0, 13, 7, 14, 9)
print(p[-100]) # (14, 13, 12, 11, 10, 9, 8, 5, 4, 2)
print(p.index([8, 1, 10, 5, 12, 0, 13, 7, 14, 9])) # 5897286400
for perm in p[100:105]: print(perm)
(0, 1, 2, 3, 4, 5, 6, 9, 10, 13)
(0, 1, 2, 3, 4, 5, 6, 9, 10, 14)
(0, 1, 2, 3, 4, 5, 6, 9, 11, 7)
(0, 1, 2, 3, 4, 5, 6, 9, 11, 8)
(0, 1, 2, 3, 4, 5, 6, 9, 11, 10)
Note that this Permutations class used on a list of non-unique values will produce duplicate permutations