Skip to content

Instantly share code, notes, and snippets.

@thejohnhoffer
Last active March 15, 2017 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejohnhoffer/9ef40c5ba8420a86fd3c15f172f6c3ee to your computer and use it in GitHub Desktop.
Save thejohnhoffer/9ef40c5ba8420a86fd3c15f172f6c3ee to your computer and use it in GitHub Desktop.
Find combos of attributes each with any possible number of values
# coding: utf-8
import numpy as np
bases = [2,3,5,3]
nb = len(bases)
rnb = range(nb)
# Get scale factors
def get_scale(i):
# Get smaller bases
return np.prod(bases[i+1:])
# Get scales for all value numbers
scales = np.uint32(map(get_scale,rnb))
total = scales[0]*bases[0]
## scales = [45, 15, 3 1]
## total = 90
# Get values from index
def get_values(index):
values = np.zeros(nb, dtype=int)
# Get value vi
def get_val(idx, vi):
values[vi] = idx//scales[vi]
# Lower index by new value
return idx - values[vi]*scales[vi]
reduce(get_val,rnb,index)
return values
# Get index from values
def get_index(vals):
#sum vals times scales
return np.sum(vals*scales)
# print values by index
for i in range(total):
val = get_values(i)
print get_index(val),': ', val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment