我不明白到底需要这段代码的最后一行,请有人解释一下吗?提前致谢!

ops = {'+': operator.add,'-': operator.sub,'*': operator.mul} #creating a dictioanry, using 'operators' built in functions

keys = list(ops.keys()) # ['+', '*', '-'] list The method keys() returns a list of all the available keys in the dictionary.
opt = random.choice(keys)  #e.g. '+'  randomly choose an operator/key/ from the dictionary
operation = ops[opt]

最佳答案

ops是一本字典
opt是随机密钥


如果opt =“ +”,则ops [opt] = operator.add

您应该看一下python的字典。

关于python - python中的keys()操作说明?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32342783/

10-13 07:22