「python技巧」判断字典或列表中是否包含某个特定的元素或者key

一、python2.x

cookie = {'name': 'ttcid', 'path': '/', 'secure': False}
result = cookie.has_key('name');
print(result)

二、python3.x

cookie = {'name': 'ttcid', 'path': '/', 'secure': False}
result = cookie.__contains__('name');
print(result)

三、in 或者not in [推荐]

cookie = {'name': 'ttcid', 'path': '/', 'secure': False}
result = 'domain' in cookie.keys();
print(result)

猜你喜欢

转载自blog.csdn.net/weixin_41635750/article/details/108227380