<xmp id="gc8um"><menu id="gc8um"></menu>
<tr id="gc8um"></tr>
<tr id="gc8um"></tr>
<object id="gc8um"><center id="gc8um"></center></object>
<rt id="gc8um"><small id="gc8um"></small></rt>
<tr id="gc8um"></tr>
<acronym id="gc8um"><center id="gc8um"></center></acronym>
<samp id="gc8um"></samp>
<option id="gc8um"></option>
<tt id="gc8um"><div id="gc8um"></div></tt>
<acronym id="gc8um"><optgroup id="gc8um"></optgroup></acronym>
<samp id="gc8um"></samp>
<rt id="gc8um"></rt>
<rt id="gc8um"></rt>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt>
<tr id="gc8um"></tr>
<acronym id="gc8um"><optgroup id="gc8um"></optgroup></acronym>
<acronym id="gc8um"><small id="gc8um"></small></acronym>
<sup id="gc8um"><small id="gc8um"></small></sup>
<acronym id="gc8um"><optgroup id="gc8um"></optgroup></acronym>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt>
<optgroup id="gc8um"><menu id="gc8um"></menu></optgroup>
<tr id="gc8um"></tr>
<rt id="gc8um"><small id="gc8um"></small></rt>
<menu id="gc8um"><noscript id="gc8um"></noscript></menu>
<option id="gc8um"></option>
<rt id="gc8um"></rt>
<acronym id="gc8um"><small id="gc8um"></small></acronym>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt><acronym id="gc8um"><small id="gc8um"></small></acronym><rt id="gc8um"></rt>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt><acronym id="gc8um"><small id="gc8um"></small></acronym>
<acronym id="gc8um"><small id="gc8um"></small></acronym>
<object id="gc8um"><small id="gc8um"></small></object>
<tr id="gc8um"></tr>
<rt id="gc8um"><small id="gc8um"></small></rt>
<tr id="gc8um"><optgroup id="gc8um"></optgroup></tr>
<samp id="gc8um"></samp>
<acronym id="gc8um"><small id="gc8um"></small></acronym>

python怎么生成一個嵌套字典,不想使用defaultdict實現

0
Python C/C++ 嵌套字典 14822 次瀏覽

比如原生的dict屬性

test=dict()
test['a']=1         # 這個可以實現
test['x']['y']=2    # 這個就會報錯

我想生成一個遞歸字典,如果沒有這個鍵則創建,哪種方法能簡潔點,優雅點~~多謝了

請盡量讓自己的答案能夠對別人有幫助

5個答案

默認排序按投票排序
0

I do not know what to say really what you share very well and useful to the community, I feel that it makes our community much more developed, thanks enable flash in chrome

0

Is your problem was already done in python dictionary? Could I exploit this info for a reference? I might truly appreciate that. many thanks. tres card game

0

Thanks for sharing this wonderful thing. Keep sharing useful and more visible things like this. Thank you very much.

 vex

 

0

Nice article thank you for sharing Also check! lucky patcher 2019 apk download AND download tutuapp for free AND 8 Ball Pool Hack

1

這樣會有一些問題.
test["x"]["y"]相當于一個鏈式調用,先獲取test["x"],再對["y"]這個key進行賦值,進行第一步的時候你無法獲取后邊是不是要進行賦值,你只能在每次getitem的時候都創建不存在的鍵.
創建這個鍵究竟是什么類型的呢?默認是dict當然很好理解,但是如果他y的位置是一個整數,那要不要創建一個列表呢?
這些你都要考慮清楚.
最后是一個簡單的實現.完全不考慮list的情況.

# coding:utf-8
class MyDict(dict):
    # mydict 可還行

    def __getitem__(self, a):
        if not hasattr(self, a):
            self[a] = MyDict() # 注意這個位置也要生成同樣的類型
        return super(MyDict, self).__getitem__(a)


test = MyDict()

test["a"]["b"]["c"] = 1

print test
# {'a': {'b': {'c': 1}}}

我覺得這種簡單的設計雖然用起來看似簡單,但是局限性也是存在的,一定要小心使用這種trick.

相似問題

相關經驗

相關資訊

相關文檔

平特争霸
<xmp id="gc8um"><menu id="gc8um"></menu>
<tr id="gc8um"></tr>
<tr id="gc8um"></tr>
<object id="gc8um"><center id="gc8um"></center></object>
<rt id="gc8um"><small id="gc8um"></small></rt>
<tr id="gc8um"></tr>
<acronym id="gc8um"><center id="gc8um"></center></acronym>
<samp id="gc8um"></samp>
<option id="gc8um"></option>
<tt id="gc8um"><div id="gc8um"></div></tt>
<acronym id="gc8um"><optgroup id="gc8um"></optgroup></acronym>
<samp id="gc8um"></samp>
<rt id="gc8um"></rt>
<rt id="gc8um"></rt>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt>
<tr id="gc8um"></tr>
<acronym id="gc8um"><optgroup id="gc8um"></optgroup></acronym>
<acronym id="gc8um"><small id="gc8um"></small></acronym>
<sup id="gc8um"><small id="gc8um"></small></sup>
<acronym id="gc8um"><optgroup id="gc8um"></optgroup></acronym>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt>
<optgroup id="gc8um"><menu id="gc8um"></menu></optgroup>
<tr id="gc8um"></tr>
<rt id="gc8um"><small id="gc8um"></small></rt>
<menu id="gc8um"><noscript id="gc8um"></noscript></menu>
<option id="gc8um"></option>
<rt id="gc8um"></rt>
<acronym id="gc8um"><small id="gc8um"></small></acronym>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt><acronym id="gc8um"><small id="gc8um"></small></acronym><rt id="gc8um"></rt>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt><acronym id="gc8um"><small id="gc8um"></small></acronym>
<acronym id="gc8um"><small id="gc8um"></small></acronym>
<object id="gc8um"><small id="gc8um"></small></object>
<tr id="gc8um"></tr>
<rt id="gc8um"><small id="gc8um"></small></rt>
<tr id="gc8um"><optgroup id="gc8um"></optgroup></tr>
<samp id="gc8um"></samp>
<acronym id="gc8um"><small id="gc8um"></small></acronym>
<xmp id="gc8um"><menu id="gc8um"></menu>
<tr id="gc8um"></tr>
<tr id="gc8um"></tr>
<object id="gc8um"><center id="gc8um"></center></object>
<rt id="gc8um"><small id="gc8um"></small></rt>
<tr id="gc8um"></tr>
<acronym id="gc8um"><center id="gc8um"></center></acronym>
<samp id="gc8um"></samp>
<option id="gc8um"></option>
<tt id="gc8um"><div id="gc8um"></div></tt>
<acronym id="gc8um"><optgroup id="gc8um"></optgroup></acronym>
<samp id="gc8um"></samp>
<rt id="gc8um"></rt>
<rt id="gc8um"></rt>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt>
<tr id="gc8um"></tr>
<acronym id="gc8um"><optgroup id="gc8um"></optgroup></acronym>
<acronym id="gc8um"><small id="gc8um"></small></acronym>
<sup id="gc8um"><small id="gc8um"></small></sup>
<acronym id="gc8um"><optgroup id="gc8um"></optgroup></acronym>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt>
<optgroup id="gc8um"><menu id="gc8um"></menu></optgroup>
<tr id="gc8um"></tr>
<rt id="gc8um"><small id="gc8um"></small></rt>
<menu id="gc8um"><noscript id="gc8um"></noscript></menu>
<option id="gc8um"></option>
<rt id="gc8um"></rt>
<acronym id="gc8um"><small id="gc8um"></small></acronym>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt><acronym id="gc8um"><small id="gc8um"></small></acronym><rt id="gc8um"></rt>
<rt id="gc8um"><optgroup id="gc8um"></optgroup></rt><acronym id="gc8um"><small id="gc8um"></small></acronym>
<acronym id="gc8um"><small id="gc8um"></small></acronym>
<object id="gc8um"><small id="gc8um"></small></object>
<tr id="gc8um"></tr>
<rt id="gc8um"><small id="gc8um"></small></rt>
<tr id="gc8um"><optgroup id="gc8um"></optgroup></tr>
<samp id="gc8um"></samp>
<acronym id="gc8um"><small id="gc8um"></small></acronym>
立博网上娱乐违法吗 皇家ag 拼多多签到满十元怎么提现 时时彩期号对应码 10元可提现的棋牌下载中心 街头篮球单机游戏下载 胜负彩比分直播 老时时开奖结果 南京宾馆沐足 南通金游棋牌游戏大厅 扑鱼比赛 什么是股票指数的缺口 金苹果时时彩登录 上门按摩特殊服务 七星南通棋牌 云南11任选5