Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 键值条件_Python - Fatal编程技术网

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'