小编Ven*_*med的帖子

字典整数作为键和函数作为值?

这是我的代码:

def test1():
  print("nr1")

def test2():
  print("nr2")

def test3():
  print("nr3")

def main():
  dictionary = { 1: test1(), 2: test2(), 3: test3() }
  dictionary[2]

if __name__ == "__main__":
  main()
Run Code Online (Sandbox Code Playgroud)

此代码返回:

nr1
nr2
nr3
Run Code Online (Sandbox Code Playgroud)

我需要在代码中更改以获取此信息:

nr2
Run Code Online (Sandbox Code Playgroud)

我正在使用Python 2.7.13.

python dictionary function python-2.7

4
推荐指数
1
解决办法
3020
查看次数

标签 统计

dictionary ×1

function ×1

python ×1

python-2.7 ×1