Python 键值条件
我有这样的字典:Python 键值条件,python,Python,我有这样的字典: emp_rec1 = { "name":"Mr.Geek", "eid":24, "location":"delhi" } 我想为它写条件 If name=Mr.Geek then prnit (hello) 我如何才能做到这一点?您可以使用emp_rec1['name']访问密钥的值,并使用== 如果您不确定dict中的键,可以使用dict方法 像 您正在添加如何在Python中索引词典吗? In [10]:
emp_rec1 = {
"name":"Mr.Geek",
"eid":24,
"location":"delhi"
}
我想为它写条件
If name=Mr.Geek
then prnit (hello)
我如何才能做到这一点?您可以使用emp_rec1['name']访问密钥的值,并使用== 如果您不确定dict中的键,可以使用dict方法 像
您正在添加如何在Python中索引词典吗?
In [10]: emp_rec1 = {
...: "name":"Mr.Geek",
...: "eid":24,
...: "location":"delhi"
...: }
In [11]: if emp_rec1['name'] == 'Mr.Geek':
...: print('hello')
...:
hello
In [12]:
emp_rec1.get('name') == 'Mr.Geek'