I've already checked the following answers: Replacements for switch statement in Python? and How to refactor Python "switch statement"
but I think both refer to simpler switch statements with single cases.
I've got a problem where a switch statement would look something like this:
switch(view) {
case "negatives":
label = 0;
break;
case "cars\\00inclination_000azimuth":
case "buses\\00inclination_000azimuth":
case "trucks\\00inclination_000azimuth":
label = 1;
break;
case "cars\\00inclination_045azimuth":
case "buses\\00inclination_045azimuth":
case "trucks\\00inclination_045azimuth":
case "cars\\00inclination_090azimuth":
case "buses\\00inclination_090azimuth":
case "trucks\\00inclination_090zimuth":
case "cars\\00inclination_135azimuth":
case "buses\\00inclination_135azimuth":
case "trucks\\00inclination_135azimuth":
label = 2;
break;
# and so on
So there are many cases that result in the same label. Is there a quick way to do this using lists? Where I could use something like this
a = ["cars\\00inclination_045azimuth","buses\\00inclination_045azimuth","trucks\\00inclination_045azimuth","cars\\00inclination_090azimuth","buses\\00inclination_090azimuth", "trucks\\00inclination_090zimuth","cars\\00inclination_135azimuth","buses\\00inclination_135azimuth","trucks\\00inclination_135azimuth"]
if view in a:
label = 2
But then I'd have to make a list for every set of cases that map to the same label and then go through each of them.
Is there a way to do the following, and if not, then what is the easiest way to do this?
if view in _any_of_the_lists_i've_made:
label = the_index_of_that_list
Update
The values I showed here in the question were just a few, in order to get a general idea of the problem. But I realized from some of the comments that it would be better to give the full range of values I have as cases.
- There are 3 prefixes: "cars", "trucks" and "buses".
- There are 4 angles of inclination (the first two digits after the slashes). So I can have cars\00inclination_000azimuth, or cars\30inclination_000azimuth or cars\60inclination_000azimuth or cars\90inclination_000azimuth
- There are a total of 25 different azimuths. With differences of 45 degrees, so I can have cars\00inclination_000azimuth and cars\00inclination_045azimuth all the way to cars\00inclination_315azimuth
So in total I have 25 views for each vehicle, and with 3 vehicles, that's 75 different possible views, i.e. 75 cases.