Python if, elif, else chain alternitive

This question already has an answer here: Replacements for switch statement in Python? 44 answers Try using a dictionary setting in which the key is the value that you are testing for and the entry for that key is a function to process. Some of the text books on Python point out that this is a more elegant solution than a series of if ... elif statements and picks up the entry immediately i

Python if,elif,else链交替

这个问题在这里已经有了答案: 替代Python中的switch语句? 44个答案 尝试使用字典设置,其中密钥是您正在测试的值,并且该密钥的条目是要处理的函数。 一些关于Python的教科书指出,这是比一系列if ... elif语句更优雅的解决方案,并立即获取条目,而不必测试每种可能性。 请注意,因为每个键都可以是任何类型,所以这比C中的switch语句更好,这需要switch参数和case为整数值。 例如。 def default(command) print

Python Use Switch Case?

This question already has an answer here: Replacements for switch statement in Python? 44 answers Python does not implement a switch . An alternative is using a dict like so: def func1(): pass def func2(): pass switch = { "do1": func1, "do2": func2, } do_str = "do1" switch[do_str]()

Python使用开关盒?

这个问题在这里已经有了答案: 替代Python中的switch语句? 44个答案 Python没有实现一个switch 。 另一种方法是使用像这样的字典: def func1(): pass def func2(): pass switch = { "do1": func1, "do2": func2, } do_str = "do1" switch[do_str]()

Rewriting Python switch into a more compact way

Possible Duplicate: Replacements for switch statement in python? Suppose I have a list in Python: list = ('ADD', 'SUB', 'PUSH', 'POP') I want to run a function depending on input, and that input can be any value in the list. Instead of writing a switch case statement for each element in list , is there a more compact way of writing it? My reasoning is f

将Python切换重写为更紧凑的方式

可能重复: python中switch语句的替代? 假设我在Python中有一个列表: list =('ADD','SUB','PUSH','POP') 我想根据输入运行一个函数,并且该输入可以是列表中的任何值。 而不是为list每个元素写一个switch case语句,是否有更紧凑的编写方式? 我的推理是针对未来名单的增长。 那么Python中没有switch / case语句。 对于一个小list ,你想使用if / elif : def do_stuff(

how to implement the switch case in python..?

This question already has an answer here: Replacements for switch statement in Python? 44 answers Fixing your Bugs You need to add a comma at the end of each item: 1: "Jan", 2: "Feb", Working program: def switch_demo(var): switcher = { 1: "Jan", 2: "Feb", 3: "March", 4: "April", 5: "May",

如何在python中实现switch case??

这个问题在这里已经有了答案: 替代Python中的switch语句? 44个答案 修复你的错误 您需要在每个项目的末尾添加逗号: 1: "Jan", 2: "Feb", 工作计划: def switch_demo(var): switcher = { 1: "Jan", 2: "Feb", 3: "March", 4: "April", 5: "May", 6: "June", 7: "July",

Looking for a good replacement for elif & if , like switch case

Possible Duplicate: Replacements for switch statement in python? Given this method : def getIndex(index): if((index) < 10): return 5 elif(index < 100): return 4 elif(index < 1000): return 3 elif(index < 10000): return 2 elif(index < 100000): return 1 elif(index < 1000000): return 0 I want to make it in

寻找elif&if的好替代品,就像开关柜一样

可能重复: python中switch语句的替代? 鉴于此方法: def getIndex(index): if((index) < 10): return 5 elif(index < 100): return 4 elif(index < 1000): return 3 elif(index < 10000): return 2 elif(index < 100000): return 1 elif(index < 1000000): return 0 我希望以开关盒的形式制作,但是,Python不支持开关盒。

python if statement too long and ugly, is there a way to shorten it

This question already has an answer here: Replacements for switch statement in Python? 44 answers 有一个包含给定p的y值的字典p={"A":10,.....} y=dict[p] There is no possibility to shorten this piece of code. As you may have noticed, python does not provide any switch-case statement. Depending on your usage, you could change it to a dictionary or recalculate it by value: values = { "A": 10,

python如果语句太长和难看,有没有办法缩短它

这个问题在这里已经有了答案: 替代Python中的switch语句? 44个答案 有一个包含给定p的y值的字典p={"A":10,.....} y=dict[p] 没有可能缩短这段代码。 正如您可能已经注意到的那样,python不提供任何switch-case语句。 根据您的使用情况,您可以将其更改为字典或按值重新计算: values = { "A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15 } y = values[p] 要么 y = ord(p) - ord("A") + 10 使用字典: choic

Since Python doesn't have a switch statement, what should I use?

Possible Duplicate: Replacements for switch statement in python? I'm making a little console based application in Python and I wanted to use a Switch statement to handle the users choice of a menu selection. What do you vets suggest I use. Thanks! Dispatch tables, or rather dictionaries. You map keys aka. values of the menu selection to functions performing said choices: def AddR

由于Python没有switch语句,我应该使用什么?

可能重复: python中switch语句的替代? 我在Python中制作了一个基于控制台的应用程序,我想使用一个Switch语句来处理用户选择菜单选项。 你的兽医建议我用什么。 谢谢! 调度表,或者说是字典。 你也可以映射钥匙。 菜单选择的值到执行所述选择的功能: def AddRecordHandler(): print("added") def DeleteRecordHandler(): print("deleted") def CreateDatabaseHandler(): print("done")

What is the Python equivalent for a case/switch statement?

This question already has an answer here: Replacements for switch statement in Python? 44 answers While the official docs are happy not to provide switch, I have seen a solution using dictionaries. For example: # define the function blocks def zero(): print "You typed zero.n" def sqr(): print "n is a perfect squaren" def even(): print "n is an even numbern" def prime():

什么是case / switch语句的Python等价物?

这个问题在这里已经有了答案: 替代Python中的switch语句? 44个答案 虽然官方文档很乐意不提供切换,但我看到了使用字典的解决方案。 例如: # define the function blocks def zero(): print "You typed zero.n" def sqr(): print "n is a perfect squaren" def even(): print "n is an even numbern" def prime(): print "n is a prime numbern" # map the inputs to the function blocks options

Python: get escaped SQL string

When I have a cursor, I know I can safely execute a query as follows: cur.execute("SELECT * FROM foo WHERE foo.bar = %s", (important_variable,)) Is there any way to just get the string safely without executing the query? For example, if important_variable is a string, like "foo 'bar' "baz" , I'd want the appropriately escaped one: "SELECT * FROM foo WHERE foo.bar =

Python:获取转义的SQL字符串

当我有一个游标时,我知道我可以安全地执行一个查询,如下所示: cur.execute("SELECT * FROM foo WHERE foo.bar = %s", (important_variable,)) 有什么方法可以安全地获取字符串而不执行查询? 例如,如果important_variable是一个字符串,例如"foo 'bar' "baz" ,我想要适当地转义一个: "SELECT * FROM foo WHERE foo.bar = "foo 'bar' "baz" (或者任何适当的转义,我甚至不确定)。 我正在使

How to delete a character from a string using python?

There is a string, for example EXAMPLE How can I remove the middle character ie M from it. I don't need the code, what I want to know is Do strings in python end in any special character? Which is a better way - shifting everything right to left starting from the middle character OR creation of a new string and not copying the middle character? In Python, strings are immutable, so yo

如何使用python从字符串中删除一个字符?

有一个字符串,例如EXAMPLE 我如何从中删除中间字符,即M 我不需要代码,我想知道的是 python中的字符串以任何特殊字符结束? 哪一种更好的方法 - 从中​​间字符开始,将所有内容从右向左移动,或者创建一个新字符串,而不是复制中间字符? 在Python中,字符串是不可变的,所以你必须创建一个新的字符串。 你有几个如何创建新字符串的选项。 如果你想删除所有出现的'M': newstr = oldstr.replace("M", "")