Python 3.x judge dict contains a key

 

Python 3.x is no longer supported has_key () function, is __contains __ ( 'key') replaced returns BOOL, which can be used to make a judgment.

代码示例:
>>> user = 'dadada'
>>> db = {'dadada':'123545','fff':'111111','ffasafs':'hhh'}
>>> db.__contains__(user)
True
>>> user = 'dadad1a'
>>> db.__contains__(user)
False

Reproduced in: https: //www.cnblogs.com/Demo-simple/p/11065530.html

Guess you like

Origin blog.csdn.net/weixin_33901641/article/details/94215273