仅打印Python中特定键的字典术语值
我想知道如果我有一个字典并且只想打印出一个特定键的值,我在Python中会做什么 它将包含在变量中,也包含在:仅打印Python中特定键的字典术语值,python,dictionary,python-3.x,Python,Dictionary,Python 3.x,我想知道如果我有一个字典并且只想打印出一个特定键的值,我在Python中会做什么 它将包含在变量中,也包含在: 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 wo
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.
我正在运行Python3.3我冒昧地重命名了
dict
变量,以避免隐藏内置名称
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])
正如阿什维尼指出的,你的字典应该是
{'Lemonade':[“1”、“45”、“87”],'Coke':[“23”、“9”、“23”],'Water':[“98”、“2”、“127”]}
要打印值,请执行以下操作:
if inp in dict:
print(dict[inp])
请注意,不要将dict
用作变量,因为它将覆盖内置类型,并可能在以后导致问题。在Python 3中:
# A simple dictionary
x = {'X':"yes", 'Y':"no", 'Z':"ok"}
# To print a specific value
print([value for value in x.values()][1])
输出:
no
第一步是拥有一个有效的dictionary对象。当然,这只是一个例子。dict[inp]在Python3.3中工作吗?