通过从用户输入中获取键来从python中的字典中查找值 [英] Find a value from a dictionary in python by taking a key from user input
本文介绍了通过从用户输入中获取键来从python中的字典中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有字典.如何通过从用户那里获取密钥来获取价值?
I have a dictionary. How can I get the value by taking the key from user?
d = {'a': '1', 'b': '2', 'c':'3'}
如果用户输入 a
,我想打印值'1'
.
If the user enters a
I wanted to print value '1'
.
推荐答案
这将为您服务:
print(p["a"])
这将在字典中打印"a"的数字.
This prints the number for "a" in the dictionary.
对于您的程序:
d = str(input("Enter what you want: "))
p = {'a': '1', 'b': '2', 'c':'3'}
print(p[d])
如果输入 a
,则输出为1.
If you enter a
, then you get 1 as output.
希望这会有所帮助!!!!
Hope this Helps!!!!
这篇关于通过从用户输入中获取键来从python中的字典中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文