max*_*max 6 python dictionary python-3.x
我需要类似的东西defaultdict
.但是,对于任何不在字典中的键,它应该返回键本身.
最好的方法是什么?
Kat*_*iel 13
使用魔法__missing__
:
>>> class KeyDict(dict):
... def __missing__(self, key):
... return key
...
>>> x = KeyDict()
>>> x[2]
2
>>> x[2]=0
>>> x[2]
0
>>>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2176 次 |
最近记录: |