相关疑难解决方法(0)

获取字典键的类型

是否有一种"干净"的方式来获取python3中字典键的类型?

例如,我想确定这个词典中的一个是否具有类型的键str:

d1 = { 1:'one', 2:'two', 5:'five' }
d2 = { '1':'one', '2':'two', '5':'five' }
Run Code Online (Sandbox Code Playgroud)

有几种方法可以实现这一点,例如,使用一些方法:

isinstance(list(d2.keys())[0], type('str'))
Run Code Online (Sandbox Code Playgroud)

但这很烦人,因为d2.keys()它不可索引,所以你需要将它转换为一个列表,只是为了提取一个键的值.

那python3有什么作用get_key_type(d2)吗?如果没有,是否有更好(更清洁)的方式来询问字典的键是否类型str

python dictionary types python-3.x

7
推荐指数
2
解决办法
7906
查看次数

标签 统计

dictionary ×1

python ×1

python-3.x ×1

types ×1