且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python检查该键是否在字典中定义

更新时间:2023-11-25 23:18:34

使用中的 operator: p>

Use the in operator:

if b in a:

演示:

>>> a = {'foo': 1, 'bar': 2}
>>> 'foo' in a
True
>>> 'spam' in a
False

你真的想开始阅读Python教程, 词典部分涵盖了这个主题。

You really want to start reading the Python tutorial, the section on dictionaries covers this very subject.