常规的做法
if ‘key1‘ in dict.keys(): print "blah" else: print "boo"
这种会生成一个list,影响性能
优雅的做法
if ‘key1‘ in dict: print "blah" else: print "boo"
另一种做法
dict.has_key(key)
不过has_key已经废弃,在python 3.0中将彻底移除
参考来源:http://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary
原文:http://pcliuyang.blog.51cto.com/8343567/1624430