0

This question is about the itertools.combinations_with_replacement. I am specifically looking for how to switch from list indexing to vector indexing, efficiently. Example:

import itertools as it
comb = it.combinations_with_replacement

V ='abcdefgh'
L = len(V)
R = 4

list(comb(V,R))[242] == (V[2],V[4],V[4],V[6])

The relationship of 242 and vector (2,4,4,6), is not obvious to me. In particular how do I write functions to convert between the two? I could clearly do the following inefficient methods:

def f(i,L,R):
    return list(comb(range(L),R))[i]

def g(v,L,R):
    return list(comb(range(L),R)).index(v)

But these are highly inefficient. I am looking for the direct functions that do not need to iterate through all the possibilities.

Bobby Ocean
  • 2,987
  • 1
  • 7
  • 15

0 Answers0