python 开关
Dear learners, hope that you are learning well. In our previous tutorial you learned about Python Pickle. In this tutorial we will look into python switch case statement.
亲爱的学习者,希望您学习愉快。 在之前的教程中,您了解了Python Pickle 。 在本教程中,我们将研究python switch case语句。
Python开关盒 (Python switch case)
Though many other languages offer switch statements, python does not have any switch statement. According to PEP-3103, it assure about the fact. However if you like switch case statement in other programming languages, you can create code snippet that works like a switch case statement in python.
尽管许多其他语言提供了switch语句,但是python没有任何switch语句。 根据PEP-3103 ,它保证了这一事实。 但是,如果您喜欢其他编程语言中的switch case语句,则可以创建像python中的switch case语句一样工作的代码段。
使用字典返回值 (Using Dictionary to Return value)
You can use dictionary to work like a switch statement. Here the keys of the dictionary will work as case.
您可以使用字典来像switch语句一样工作。 在这里,字典的键将视case 。
Then according to the keys, the value of the dictionary will work as the result. We will give some example through out this tutorial. See the following code to understand this simple example of switch case statement alternative. See our Python Dictionary tutorial, if you don’t know about Python dictionary.
然后根据键,字典的值将作为结果。 在本教程中,我们将给出一些示例。 请参阅以下代码,以了解此简单的switch case语句替代示例。 如果您不了解Python字典,请参阅我们的Python字典教程。
b ={
'a' : 122,
'b' : 123,
'c' : 124,
'd' : 125
}
# take user input
inp = input('input a character : ')
# -1 is the default value if there is no keys that matches the input
print('The result for inp is : ', b.get(inp, -1))
So for my case I entered ‘b’ as input. The output was following
因此,对于我的情况,我输入了“ b”作为输入。 输出如下
带有动态功能的Python switch case语句替代 (Python switch case statement alternative with dynamic functions)
In this section we will see how to perform dynamic action based on dictionary keys. So, imagine a scenario where we need to calculate different thing based on the input. How to do that using dictionary? Well, in this case we will use Python Lambda function to do that. See the following example as an alternative of python switch case statement.
在本节中,我们将看到如何基于字典键执行动态操作。 因此,设想一个场景,在该场景中,我们需要根据输入来计算不同的事物。 如何使用字典做到这一点? 好吧,在这种情况下,我们将使用Python Lambda函数来执行此操作。 请参阅以下示例,以替代python switch case语句。
def switch_func(value, x):
return {
'a': lambda x: x+122,
'b': lambda x: x*2,
'c': lambda x: x-123,
'd': lambda x: x/2
}.get(value)(x)
# take user input
inp = input('input a character : ')
print('The result for inp is : ', switch_func(inp, 2))
I used ‘d’ as input. So the output of the following code was like this
我用“ d”作为输入。 所以以下代码的输出是这样的
input a character : d
The result for inp is : 1.0
So, that’s all about Python switch case statement alternatives. It’s not recommended to use this kind of code snippet. Python if else technique is more elegant to use than this snippet. But it is never bad to learn about new techniques.
因此,所有这些都与Python切换案例语句的替代方案有关。 不建议使用这种代码段。 Python if else技术比该代码段更优雅。 但是学习新技术从来都不坏。
python 开关
本文探讨了Python中没有内置的Switch Case语句,但可以通过使用字典来模仿其功能。介绍了如何利用字典的键作为Case,并通过Lambda函数实现动态功能。虽然Python推荐使用If Else语句,但了解字典的这种方法仍具有一定的教育意义。

2396

被折叠的 条评论
为什么被折叠?



