I want to create a dictionary mapping all combinations of three groups to an integer. Is it possible to do this in a single line and without using any imports?
With itertools it could be done with:
colours = ['red','green','blue']
shapes = ['circle','square','triangle']
sizes = ['small','medium','large']
import itertools as it
lookup = {key:val for val,key in enumerate(it.product(colours,shapes,sizes))}
However, I can't figure out how to do enumeration with a nested for-loop and list comprehension. Eg, the below syntax is an attempt, but doesn't increment for each level of the for loop:
lookup = {(c,s,z):i for i,c in enumerate(colours) for s in shapes for z in sizes}
The output should look like:
{('red', 'circle', 'small'): 0,
('red', 'circle', 'medium'): 1,
('red', 'circle', 'large'): 2,
('red', 'square', 'small'): 3,
('red', 'square', 'medium'): 4,
...