如何检查字典中的每个键是否都是整数?

Qui*_*101 2 python dictionary python-3.x

我最近刚刚开始使用 python 编码,我遇到了一个练习,需要使用字典。我特别在练习的部分中遇到困难,我需要检查字典中的每个键是否具有 int 类型。

Cha*_*ont 6

怎么样 :

# returns True if each key in x is an instance of type int, else returns False
all(isinstance(key, int) for key in x.keys())
Run Code Online (Sandbox Code Playgroud)

其中 x 是你的字典