Dynadot Jtti 华纳云 搬瓦工 腾讯云

Python 3 字典不包含 dict.has_key() 方法

GigsGigsCloud

Python 3 中移除了字典的 has_key() 方法,如果你有代码使用了 has_key(),要么就改用 Python 2,要么就建议使用 in 操作。这也是 Python 官方文档给出的 Python 3 的若干变化之一。今天正好用到了 OrderedDict,搜了下网上代码都是 Python 2 的,于是在此记录一下。

一、has_key() in Python 3

如下阐述:

has_key was removed in Python 3. From the documentation:

  • Removed dict.has_key() – use the in operator instead.

Here’s an example:

if start not in graph:
    return None

也就是说,之后可以用 in 和 not in 来判断字典中是否有某个特定值。

二、参考文献和其他

参考:

学无止境,学海无涯。

Dynadot Hostwinds
赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《Python 3 字典不包含 dict.has_key() 方法
文章链接:https://oldtang.com/2301.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。