[英]Can you make a switch-case-type list without conditions in Python where it just chooses a random case?
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.
编辑:我知道可能无法做我正在寻找的事情,但我很想知道是否有更多了解 Python 的人知道一种方法。 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.假设我有 3 件事情可能发生,我的程序应该随机选择一件。 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:
现在我要添加可能发生的第四件事,但我不想增加
random.randint(a,b)
b
的 b,也不想增加elif Case == i:
中的i
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.然后,如果我想添加第 4 个案例,我复制粘贴一个,然后只编辑案例的主体。 However, I also need to change the
3
in the first line, as well as the 3
in the line I just pasted.但是,我还需要更改第一行中的
3
,以及我刚刚粘贴的行中的3
。 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.而且,重要的是,如果我想在案例 1 和案例 2 之间添加一个选项以提高可读性,这意味着我必须在我的新案例 2 之后更新所有整数——这是我不想做的事情做。
Is there a way where Python could pick one of my elifs at random?有没有办法让 Python 随机选择我的一个 elif? Obviously they don't have to be elifs, I tried looking at switch-cases also, but haven't found what I'm looking for.
显然他们不必是 elifs,我也尝试查看 switch-cases,但没有找到我要找的东西。 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.附言。
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. option
内执行多个操作,因此mylist = random.choice([[1, 1, 0, 1, 0, 0], [1, 1, 0, 0, 0, 1], [1, 0, 1, 1, 0, 0]])
可能在这里有效,但在其他情况下无效。 I would also still like to add a comment in each case to clarify what situation it is.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.
您可以随意添加或删除更多条目。
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如果你真的,真的不想更新 myFuncs 列表,你可以做这样的事情,并在你的 function 名称前加上一个标识符
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.