Python 3.x 判断 dict 是否包含某个键

Python 3.x不再支持 has_key() 函数,而被__contains__('key')所替代,会返回bool,可以用其做判断。

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

转载于:https://www.cnblogs.com/Demo-simple/p/11065530.html

猜你喜欢

转载自blog.csdn.net/weixin_33901641/article/details/94215273