在Python中使用字典代替Case / Switch语句 [英] Using Dictionaries in Python in place of Case/Switch statement

查看:186
本文介绍了在Python中使用字典代替Case / Switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想随机化一个初始化为完整的rubik的多维数据集(所有颜色都在正确的边上)。我有移动功能来旋转立方体。我想随机选择50个函数来正确地随机化。



我正在做这个项目来学习一些关于Python的更多信息,因为我主要做C ++和我看到Python没有case / switch,所以我正在尝试一个字典。当我制作字典时,代码似乎由于某种原因执行:

  def random_cube(self):
scramble = {0:self.up_turn(),
1:self.down_turn(),
2:self.left_turn(),
3:self.right_turn(),
4:self.front_turn(),
5:self.back_turn(),
6:self.up_turn(inverted),
7:self.down_turn(inverted),
8:self.left_turn(inverted),
9:self.right_turn(inverted),
10:self.front_turn(inverted),
11对于范围(x)中的x($)
i = random.randint(0,11)
scramble [i]

所以当我做这个字典,似乎运行并执行所有11个条目由于某些原因(我想)。我似乎找不到一个更好的方法,至少比一个长的if / elif字符串的语句更优雅。



!编辑:实现两个建议,(倒置)标志的功能不被任何一个建议设置。例如,调用1和7都给我down_turn,但输出显示当它应该在第7位时标志没有设置。



任何想法?

解决方案

当您定义dict时,它实际上是调用函数,并将返回值存储在字典中。要使字典存储对函数的引用,您需要删除后面的括号。所以这样的东西:

  scramble = {0:self.up_turn,
1:self.down_turn,

然后,在底部调用 scramble [i] )



这将调用没有参数的函数。要处理通过反转作为参数的情况,您可以定义单独的函数,如up_turn_inverted()等,或者您可以让字典存储由该函数组成的2个字符,然后调用某些东西有点像 scramble [i] [0](scramble [i] [1])



从建议更新注释:
您还可以使用lambda表达式来定义函数,特别是需要参数的函数。这基本上等同于定义一个up_turn_inverted函数,但是作为一个匿名函数就地完成。它将如下所示:

  6:lambda:self.up_turn(inverted)
7:lambda: self.down_turn(inverted)

等。


I want to randomize a rubik's cube that is initialized as complete (all colors on the correct sides). I have move functions that rotate the cube. I want to randomly pick 50 functions in a row to properly randomize it.

I am doing this project to learn a little bit more about Python, since I mostly do C++ and I see there is no case/switch for Python, so I am trying a dictionary. When I make the dictionary, the code seems to execute for some reason:

def random_cube(self):
    scramble = {    0 : self.up_turn(),
                    1 : self.down_turn(),
                    2 : self.left_turn(),
                    3 : self.right_turn(),
                    4 : self.front_turn(),
                    5 : self.back_turn(),
                    6 : self.up_turn("inverted"),
                    7 : self.down_turn("inverted"),
                    8 : self.left_turn("inverted"),
                    9 : self.right_turn("inverted"),
                    10: self.front_turn("inverted"),
                    11: self.back_turn("inverted")
                }
    for x in range(50):
        i = random.randint(0,11)
        scramble[i]

So when I make this dictionary, it seems to run through and execute all 11 entries for some reason (I think). I can't seem to find a better way, at least more elegant than a long if/elif string of statements.

!EDIT: Implementing both suggestions, the ("inverted") flag for the functions are not being set by either suggestion. For example, calling 1 and 7 both give me down_turn, but the output shows that the flag was not set when it should have been on number 7.

Any ideas?

解决方案

When you define the dict, it's actually calling the functions, and storing the return value in the dictionary. To just have the dictionary store a reference to the functions, you need to drop the trailing parentheses. So something like:

scramble = {  0: self.up_turn,
              1: self.down_turn,
              etc.

Then, at the bottom, call scramble[i]().

This will call the function with no arguments. To handle the case where you pass "inverted" as an argument, you could either define separate functions like up_turn_inverted(), etc., or you could have the dictionary store a 2-ple consisting of the function, and the argument, then call something liks scramble[i][0](scramble[i][1])

Update from suggestion in comments: You could also use lambda expressions to define the functions, particularly the ones that require an argument. This is basically equivalent to defining an up_turn_inverted function, but done in-place as an anonymous function. It would look like this:

6: lambda: self.up_turn("inverted")
7: lambda: self.down_turn("inverted")

etc.

这篇关于在Python中使用字典代替Case / Switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆