Dict.has_key()方法
Python dictionary (Dictionary) has_key () function is used to determine whether a key exists in the dictionary, if the key is in the dictionary dict returns true, otherwise returns false.
has_key () method syntax: dict . has_key ( Key )
* Key - key to find in the dictionary.
* If the key is in the dictionary returns true, otherwise false.
Example:
The following examples illustrate has_key () function using the method:
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7} print "Value : %s" % dict.has_key('Age') print "Value : %s" % dict.has_key('Sex')
The results:
Value : True
Value : False
Another note:
the Python 3.X is not included in has_key () function is __contains __ (key) Alternative:
dict3={'name':'Z','Age':. 7 , 'class' : 'First' };
Print ( "Value:" , dict3 . __contains__ ( 'name' ))
Print ( "Value:" , dict3 . __contains__ ( 'Sex' ))
execution results:
Value:True
Value:False
reference Links:https://www.runoob.com/python/att-dictionary-has_key.html