Home > Enterprise >  Can you make a switch-case-type list without conditions in Python where it just chooses a random cas
Can you make a switch-case-type list without conditions in Python where it just chooses a random cas

Time:02-01

This is a question that is just about convenience (/laziness) of writing. I know I will spend more time writing this question than how much I will save, but I'm curious. EDIT: I am aware that it might not be possible to do what I'm looking for, but I was curious to see if someone with more knowledge of Python knew of a way. I've also edited my codeblocks to clarify some situations that came up in the comments.

Say I have 3 things that can happen, and my program is supposed to choose one at random. Now I'm going to add a 4th thing that can happen, but I don't want to have to increase the b in random.randint(a,b), nor do I want to have to increase the i in elif Case == i:

Right now I have this:

x_1 = 0
x_2 = 0
Case = random.randint(1, 3)

if Case == 1: ## a, b, and d are known
    mylist = [1, 1, 0, 1, 0, 0]
    x_1  = 1
        
elif Case == 2: ## a, b, and f are known
    mylist = [1, 1, 0, 0, 0, 1]
    x_2  = 1
        
elif Case == 3: ## a, c, and d are known
    mylist = [1, 0, 1, 1, 0, 0]
    x_1  = 2
    x_2  = x_1

Then, if I want to add a 4th case, I copy-paste one, and just edit the body of the case. However, I also need to change the 3 in the first line, as well as the 3 in the line I just pasted. And, the important part, if I want to add an option between Case 1 and Case 2 for readability, it means I have to update all the integers after my new Case 2 - which is the thing that I don't want to have to do.

Is there a way where Python could pick one of my elifs at random? Obviously they don't have to be elifs, I tried looking at switch-cases also, but haven't found what I'm looking for. Something like this:

ChooseRandomOption
    option ## a, b, and d are known
        mylist = [1, 1, 0, 1, 0, 0]
        x_1  = 1

    option ## a, b, and f are known
        mylist = [1, 1, 0, 0, 0, 1]
        x_2  = 1

    option ## a, c, and d are known
        mylist = [1, 0, 1, 1, 0, 0]
        x_1  = 2
        x_2  = x_1

and then all I have to do is add this and not change anything else:

    option ## a, b, and e are known
        mylist = [1, 1, 0, 0, 1, 0]
        x_1 -= 1

Thanks!

PS.

  1. Keep in mind, the purpose is to write even less, so adding functions and picking a random function is not really what I'm looking for. EDIT: Especially since the inputs to each function could be different.
  2. (EDIT: This PS has now been integrated in the codeblocks) The list is just one example of what could be done, but maybe multiple actions have to be executed inside the option, so mylist = random.choice([[1, 1, 0, 1, 0, 0], [1, 1, 0, 0, 0, 1], [1, 0, 1, 1, 0, 0]]) may work here, but not in other cases. I would also still like to add a comment in each case to clarify what situation it is.

CodePudding user response:

import random
cases = {
    "a": 1,
    "b": 2,
    "c": 3
}

print(cases.get(random.choice(list(cases.keys()))))

Using a dict to store your cases allows you to pick a random entry from the dict by randomly choosing from the keys of the dictionary. You can easily add more entries or remove them at will.

CodePudding user response:

You could have a list of functions, then execute a random one. That way you can even have random behaviour, not just lists, and can name them for clarity as well.

def func1():
    return [1, 1, 0, 1, 0, 0]

def func2():
    return [1, 1, 0, 0, 0, 1]

def func3():
    return [1, 0, 1, 1, 0, 0]

myFuncs = [func1, func2, func3]
myFunc = random.choice(myFuncs)
myList = myFunc()

If you really, really don't ever want to update the myFuncs list, you could do something like this and prefix your function names with an identifier

import inspect
import sys

def listFunc1():
    return [1, 1, 0, 1, 0, 0]

def listFunc2():
    return [1, 1, 0, 0, 0, 1]

def func3():
    return [1, 0, 1, 1, 0, 0]

myFuncs = [obj for name,obj in inspect.getmembers(sys.modules[__name__]) 
                                  if (inspect.isfunction(obj) 
                                  and name.startwith('listFunc')]
myFunc = random.choice(myFuncs)
myList = myFunc()
  • Related