I am new to Python. I want to apply various permutations of three functions f, g, and h where juxtaposition means function composition (e.g., fg(x) = f(g(x))) to say, a list x of boolean integers, and put the results in an array. To make it easy to verify that each array element gets assigned the correct value, I want its index to reflect the permutation that generated it.
What I came up with below works fine but is there a better way? Specifically, I'd prefer that something shorter than "(list(c[p.index(" appear between the current function and the previously applied ones, if possible.
def f(x):
# manipulate x
return x
def g(x):
# manipulate x
return x
def h(x):
# manipulate x
return x
def apply_perms(x):
assert (len(x) == 10)
p = (
'f','g','h',
'fg','gh','hf',
'ffg','fgh','fhf'
)
c = [[0] * 10 for _ in range(9)]
c[p.index('f')] = f(list(x))
c[p.index('g')] = g(list(x))
c[p.index('h')] = h(list(x))
c[p.index('fg')] = f(list(c[p.index('g')]))
c[p.index('gh')] = g(list(c[p.index('h')]))
c[p.index('hf')] = h(list(c[p.index('f')]))
c[p.index('ffg')] = f(list(c[p.index('fg')]))
c[p.index('fgh')] = f(list(c[p.index('gh')]))
c[p.index('fhf')] = f(list(c[p.index('hf')]))
return c
x = [1,0,1,0,0,0,1,0,0,1]
apply_perms(x)