python - 隐藏的字典键

标签 python dictionary

我有一个字典是从我无权访问代码的函数传递给我的。在这本字典中有一个键叫做'time'。 .我可以print d['time']它打印出我期望的值。但是,当我遍历字典时,将跳过此键。还有 d.keys()不包括它。如果重要,其他键是数字。

我该如何重新创建它?你如何在不知道名字的情况下看到隐藏的 key ?这可以撤消吗?

print type(d)返回 <type 'dict'>

最佳答案

简单复现:

class fake_dict(dict):
    def __getitem__(self, item):
        if item == 'time':
            return 'x' 


d = fake_dict({'a': 'b', 'c': 'd'})
assert isinstance(d, dict)
assert 'time' not in d.keys()
print d  # {'a': 'b', 'c': 'd'}
assert d['time'] == 'x'

关于python - 隐藏的字典键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30674560/

相关文章:

python - 如何对 Pandas 的多索引进行分组?

python - Pygame Sprite 辅助

python - 在 Python 中将字母转换为数字

objective-c - 从属性列表中读取 NSDictionary 键

C++ Qt : Can't compile GoldenDict

python - 基于键的字典的唯一列表

python - 在数据框的两列之间运行基本关联

python - 使用 re.split 在 Python 中将文件拆分为多行

python - 根据某些条件分配 pandas DataFrame 单元格的内容而不使用循环

c# - 字典c#的算术运算