篮球比分网逛球街: python怎么生成一個嵌套字典,不想使用defaultdict實現

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

篮球比分188直播 www.703192.live 比如原生的dict屬性

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

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

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

20個答案

默認排序按投票排序
0

Je reviendrai probablement sur votre blog très bientôt. Si vous voulez le meilleur coupe-arbre de la ville, ils peuvent vous aider pour tout service arbre que vous souhaitez.

0

The information you have posted is really good and very helpful, I will often visit your site. mapquest driving directions

0

Excellent post! Thanks for sharing this nested dictionary example! 

tree service Ashburn VA   tree services Alexandria VA

0

Awesome example! Glad to find this article post. Thanks a lot! roofing

0

Thanks for sharing this! Very valuable information posted. industrial floor coating

0

Nice information! Thanks for sharing this attributes. Also need help on my plumber springfield va site. 

0

Thank you for showing me this new code!

Prepper shop UK

0

我喜歡您討論主題出色工作的方式,謝謝分享 click to download

0

Thanks for the share!

<a href=”//www.chappelleroofing.com”>roofing st. petersburg</a>

<a href=”//www.chappelleroofing.com”>roofing bradenton</a>

<a href=”//www.chappelleroofing.com”>roof repair st. petersburg</a>

<a href=”//www.chappelleroofing.com”>roof repair bradenton</a>

0

I am not sure where you were getting all your information because it is really good. Thanks for the great info. Please take a look about pool enclosures saint petersburgpool enclosures clearwaterpool enclosures largo and pool enclosures seminole. Goodluck then!

0

try using ** kwargs

tutuapp

0

本文頁面的內容,我發現內容對我來說非常有趣和有用,謝謝分享  點擊這里

0

The topic you given is awesome and awesome, I like your post. Thank you for sharing.
link here

0

//cdkfpgs.wikidot.com
//xakfpgs.wikidot.com
//kmkfpgs.wikidot.com
//dgkfpgs.wikidot.com
//shkfpgs.wikidot.com
//tjkfpgs.wikidot.com
//jnkfpgs.wikidot.com
//zqkfpgs.wikidot.com
//whkfpgs.wikidot.com
//gykfpgs.wikidot.com
//zhkfpgs.wikidot.com
//hfkfpgs.wikidot.com
//yzkfpgs.wikidot.com
//mmkfpgs.wikidot.com
//lygkfpgs.wikidot.com

//cdkfpgs.wikidot.com/start
//xakfpgs.wikidot.com/start
//kmkfpgs.wikidot.com/start
//dgkfpgs.wikidot.com/start
//shkfpgs.wikidot.com/start

0

very nice information you share with us. thanks for this.

gmail sign up

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.

相似問題

相關經驗

相關資訊

相關文檔

{ganrao} 管家婆期期准免费资料精选图 吉林十一选五开奖号 湖北11选五遗漏查询 股票涨跌与统计学 江苏快三全天计划表 pk10冠军预测网站 极速快3高手计划 江西11选5近1000期走势图 彩吧论坛,论坛首页 北京pk拾直播皇家开奖 大乐透开奖日期 1分快3稳赚规律 腾讯五分彩定位胆怎么玩 广西快乐十分最新开奖一定牛 辽宁11选5技巧 11选5高手中奖案例