Ask Your Question

Revision history [back]

I would use a different data structure, namely a dictionary:

p = {(k,l) : var(f'p_{k}{l}') for k in range(2) for l in range(4)}

Then you can do:

sage: p[0,0]
p_00
sage: p[1,2]
p_12

And if you want, you can define your flattened list z based on this dictionary:

z = [p[k,l] for k in range(2) for l in range(4)]

I would use a different data structure, namely a dictionary:

p = {(k,l) : var(f'p_{k}{l}') for k in range(2) for l in range(4)}

Then you can do:

sage: p[0,0]
p_00
sage: p[1,2]
p_12

And if you want, you can define your flattened list z based on this dictionary:

z = [p[k,l] for k in range(2) for l in range(4)]

And to go from a variable to a tuple of indices you can define an "inverse" dictionary:

p_inverse = {v:k for k,v in p.items()}

Indeed:

sage: p_inverse[p[1,2]]
(1,2)