用于模拟开关操作器的dict

Kif*_*sif 2 python python-3.x

在我的教科书中,我读到新手需要一些时间来识别这种结构:

choice = 'ham'
print ({
    'spam': 1.25, 
    'ham': 1.99,
    'eggs': 0.99,
    'bacon': 1.10
}[choice])
Run Code Online (Sandbox Code Playgroud)

结果如下:

The result is 1.99 
Run Code Online (Sandbox Code Playgroud)

说实话,我甚至无法抓住结的尾巴,无法解开它.你可以向我澄清一下吗?

Mar*_*ers 5

它是一个python字典文字,结合使用choice键作为键:

mapping = {'spam': 1.25, 'ham': 1.99, 'eggs': 0.99, 'bacon': 1.10}
choice = 'ham'
price = mapping[choice]
print(price)
Run Code Online (Sandbox Code Playgroud)