python怎么生成一个嵌套字典,不想使用defaultdict实现

0
Python C/C++ 嵌套字典 28285 次浏览

比如原生的dict属性

test=dict()
test['a']=1         # 这个可以实现
test['x']['y']=2    # 这个就会报错

我想生成一个递归字典,如果没有这个键则创建,哪种方法能简洁点,优雅点~~多谢了

请尽量让自己的答案能够对别人有帮助

18个答案

默认排序 按投票排序
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=”http://www.chappelleroofing.com”>roofing st. petersburg</a>

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

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

<a href=”http://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

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

http://cdkfpgs.wikidot.com/start
http://xakfpgs.wikidot.com/start
http://kmkfpgs.wikidot.com/start
http://dgkfpgs.wikidot.com/start
http://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.