python - 无法将具有 int 类型键的 dict 分配给 EasyDict
问题描述
我使用EasyDict
并想为其分配一个dict
with 类型的int
键
from easydict import EasyDict as edict
cfg = edict()
cfg.tt = {'0': 'aeroplane'} # this is ok
cfg.tt = {0: 'aeroplane'} # this raises error, but this is what I want to use!
如果我想分配dict
我想要的我该怎么办,谢谢
解决方案
这是因为EasyDict
正在将任何值转换dict
为EasyDict
. 并从键中创建一个属性。 int
值不能是属性,所以这不起作用。您可以安装PermissiveDict
,它的作用与将值转换为自己的类型几乎相同,EasyDict
但不会尝试将其转换为自己的类型。
pip install permissive-dict
你的例子:
from permissive_dict import PermissiveDict
cfg = PermissiveDict()
cfg.tt = {'0': 'aeroplane'} # this is ok
cfg.tt = {0: 'aeroplane'} # this does not raise errors, but this is what I want to use!
cfg.tt[0]) == cfg.TT[0] == cfg.tT[0] == cfg.Tt[0] == 'aeroplane'
推荐阅读
- haskell - 从 Haskell 的列表中返回序列的第一次迭代
- r - 在R中将分组对组合成一个组
- php - 如何通过 sharedprefrence 保存列表数据
- rust - 为什么 Rust 迭代器的 FnMut 是谓词而不是 Fn?
- python - 不能将 2 定义为素数
- python-3.x - 如果python中相同值的条件不同
- java - 如何从 Firebase 列表视图中的管理员帐户中删除用户帐户?
- c - 在不使用繁忙等待实现的情况下休眠线程的最佳方法是什么?
- java - 控制器和检票口页面的设计模式帮助
- python-3.x - 如何从 Python 3 中的脚本连接 Google Datastore