V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
P9
V2EX  ›  Python

如何可以简单地嵌套生成一个字典

  •  
  •   P9 · Jan 21, 2014 · 7387 views
    This topic created in 4492 days ago, the information mentioned may be changed or developed.
    希望能做到:
    In [24]: c =dict()
    In [24]: c.people.name = 'roy'

    In [25]: c
    Out[25]: {'people': {'name': 'roy'}}

    你的方案是?

    附上我的:http://scriptogr.am/pison
    6 replies    1970-01-01 08:00:00 +08:00
    gcweb
        1
    gcweb  
       Jan 21, 2014
    是否一定需要这样的数据结构才能解决问题呢?

    我总觉得需要这种hack的代码,本身结构上就需要调整。。。
    pohuty
        2
    pohuty  
       Jan 22, 2014   ❤️ 1
    In [2]: from collections import defaultdict

    In [3]: a = defaultdict(dict)

    In [4]: a['hell']['good']= 1

    In [5]: a
    Out[5]: defaultdict(<type 'dict'>, {'hell': {'good': 1}})
    P9
        3
    P9  
    OP
       Jan 22, 2014
    @gcweb 并非说一定,只是提供一种思路~ 如果能用更好的数据结构,自然不用如此麻烦的做了
    P9
        4
    P9  
    OP
       Jan 22, 2014
    @pohuty 这个方式倒也不错,直接用了自带属性。
    gully
        5
    gully  
       Jan 24, 2014
    P9
        6
    P9  
    OP
       Jan 27, 2014
    @gully 哈哈哈,我喜欢这个想法!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   857 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 20:31 · PVG 04:31 · LAX 13:31 · JFK 16:31
    ♥ Do have faith in what you're doing.