check key is present in python dictionary

This question already has an answer here:

  • Check if a given key already exists in a dictionary 18 answers
  • How to check if a key exists in an inner dictionary inside a dictionary in python? 3 answers

  • if 'node2' in data:
        print "node2 Present"
    else:
        print "node2Not present"
    

    这是确定密钥是否在字典中的完美适当方式,不幸'node2'是, 'node2'不在你的字典中, ' node2'是(注意空间):

    if ' node2' in data:
        print "node2 Present"
    else:
        print "node2Not present"
    

    data = {'node2': {'Status': ' online', 'TU': ' 900', 'Link': ' up', 'Port': ' a0a-180', 'MTU': ' 9000'}, ' node1': {'Status': ' online', 'TU': ' 900', 'Link': ' up', 'Port': ' a0a-180', 'MTU': ' 9000'}}
    
        if data.has_key('node2'):
            print "== if condition =="
        else:
            print "== else =="
    
    链接地址: http://www.djcxy.com/p/28896.html

    上一篇: 如何获得Youtube缩略图

    下一篇: 检查密钥存在于Python字典中