打印Python中特定键的词典术语的值 [英] Print just the value of a dictionary term for a specific key in Python
本文介绍了打印Python中特定键的词典术语的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如果我有一个字典,我在Python中做什么,我想打印出一个特定的键值。
I am wondering what I do in Python if I have a dictionary and I want to print out just the value for a specific key.
它将在变量以及
dict = {'Lemonade':["1", "45", "87"], 'Coke:["23", "9", "23"] 'Water':["98", "2", "127"}
inp = input("Select key to print value for!" + "/r>>> ")
if inp in dict:
#Here is where I would like it to print the Value list for the key that is entered.
我正在运行Python 3.3
I am running Python 3.3
推荐答案
我自由地重命名你的 dict
变量,以避免影子内置的名称。我也假设你在python3,如果你在2.x,你应该将输入
函数更改为 raw_input
I have taken the liberty of renaming your dict
variable, to avoid shadowing the built-in name. I'm also assuming you're on python3, if you're on 2.x you should change the input
function to raw_input
instead.
dict_ = {'Lemonade': ["1", "45", "87"], 'Coke': ["23", "9", "23"], 'Water': ["98", "2", "127"]}
inp = input("Select key to print value for!" + "/r>>> ")
if inp in dict_:
print(dict_[inp])
这篇关于打印Python中特定键的词典术语的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文