python:从字典中打印值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3375804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python: print values from a dictionary
提问by l--''''''---------''''''''''''
generic_drugs_mapping={'MORPHINE':[86],
'OXYCODONE':[87],
'OXYMORPHONE':[99],
'METHADONE':[82],
'BUPRENORPHINE':[28],
'HYDROMORPHONE':[54],
'CODEINE':[37],
'HYDROCODONE':[55]}
How do I return 86?
我如何返回86?
This does not seem to work:
这似乎不起作用:
print generic_drugs_mapping['MORPHINE'[0]]
采纳答案by Andrew
The list is the value stored under the key. The part that gets the value out is generic_drugs_mapping['MORPHINE']so this has the value [86]. Try moving the index outside like this :
列表是存储在键下的值。获取值的部分是generic_drugs_mapping['MORPHINE']so this 具有 value [86]。尝试像这样将索引移到外面:
generic_drugs_mapping['MORPHINE'][0]
回答by Greg Hewgill
You have a bracket in the wrong place:
你在错误的地方有一个括号:
print generic_drugs_mapping['MORPHINE'][0]
Your code is indexing the string 'MORPHINE', so it's equivalent to
您的代码正在索引 string 'MORPHINE',因此它相当于
print generic_drugs_mapping['M']
Since 'M'is not a key in your dictionary, you won't get the results you expect.
由于'M'不是字典中的键,因此您不会得到预期的结果。

