Let's say you want to make an asymmetric transformation on an array of data, but, you want your data to be symmetric in regard of this transformation. You would
- make a random permutation of your data,
- do your asymmetric transformation your permuted data,
- reverse the permutation to put your data back in place.
Problem : it is much easier to make the permutation on your original dataspace in order to put your data back in place, than to build a reversed permutation.
Example : to generate a random foreground color, which is far from a background color, you would need to be far from one of the three fundamental colors and both the other colors may be random. Let's say my background color is:
import random
background = tuple(random.randint(0, 0xff) for _ in range(3))
let's choose one component at random:
selector = random.choice(((0, 1, 2), (1, 0, 2), (2, 0 ,1)))
Then, I will choose my first component, far from that one:
far_component = 0xff if background[selector[0]] < 0x80 else 0
and then I will create my foreground color as a function of my initial selection:
foreground = list(range(3))
foreground[selector[0]] = far_component
foreground[selector[1]] = random.randint(0, 0xff)
foreground[selector[2]] = random.randint(0, 0xff)
In this simple case, things are quite manageable, but let's imagine a much more complex case with many more dimensions. It will require a loop to manage all the output assignations like the following:
variable = len(selector)*None # If ever variable doesn't exist yet.
for i in range(len(selector)):
variable[selector[i]] = ordered_source_tuple[i]
Would there be a nice pythonic way to write such an assignation which would look like a comprehension list on variables. Something like this (which obviously won't work because the left part won't be a tuple of LValues):
(variable[i] for i in selector) = ordered_source_tuple
and finding the reverse permutation to write this is no simple operation
variable = ordered_source_tuple(reversed_selector)