I want to generate a mapping between a set of integer arrays and integer values in python, e.g for n=2
(size of the array) and m=2
(array item range), I want to have:
{
0: [0, 0, 0],
1: [0, 0, 1],
2: [0, 1, 1],
3: [1, 0, 0],
...
7: [1, 1, 1]
}
One way to do this is to use the itertools.product
and do the following:
from itertools import product
import numpy as np
n = 2
m = 3
a = list(product(np.arange(n), repeat=3))
a = list(map(lambda l: list(l), a))
b = dict(zip(np.arange(len(a)), a))
and b
will be:
{0: [0, 0, 0], 1: [0, 0, 1], 2: [0, 1, 0], 3: [0, 1, 1], 4: [1, 0, 0], 5: [1, 0, 1], 6: [1, 1, 0], 7: [1, 1, 1]}
and I can have the mentioned mapping e.g.
>>> b[1]
[0, 0, 1]
However, in my case the n=8
and m=16
and storing such a dictionary (8**16
entries!) will take more than 100 Gb. I want to know if there is a lazy way of generating each index of the b everytime I want to access that without generating the entire dictionary?
CodePudding user response:
You can make a function instead of full map. Your output is actually array of size m representing value of input a in n-th scale of notation. If you want to make a map just save the result of the function in map if key doesn't exist.
def f(a, n, m):
res = []
while a != 0:
res = [a % n] res
a //= n
m -= 1
while m != 0:
res = [0] res
m -= 1
return res
CodePudding user response:
You can generate the xth element by taking the successive x//n**(i-1)%n
for i that varies between m-1 and 0.
n = 3
m = 4
x = 6
[x//n**(i-1)%n for i in range(m, 0, -1)]
output: [0, 0, 2, 0]
as a function
def f(x, n, m):
return [x//n**(i-1)%n for i in range(m, 0, -1)]
f(1, n=2, m=3)
# [0, 0, 1]
f(1234567890, n=8, m=16)
# [0, 0, 0, 0, 0, 1, 1, 1, 4, 5, 4, 0, 1, 3, 2, 2]
CodePudding user response:
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