V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
likai
V2EX  ›  Python

怎么把类似{'a':[{'b':'1','d':'3',},{'b':'2','d'='a'},{'b':'3','d':'4'}],'c':'1'}这个遍历出来?

  •  
  •   likai · 2012-01-29 22:35:25 +08:00 · 4296 次点击
    这是一个创建于 4479 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最后结果要是能以
    r.a.b
    r.a.d
    r.c取里面的值..

    弄个小软件来学PY.结果卡在这里了.求解..

    .NND.py2.6.实在是弄不出了.
    8 条回复    1970-01-01 08:00:00 +08:00
    Weakdancer
        1
    Weakdancer  
       2012-01-29 22:42:39 +08:00
    r={'a':[{'b':'1','d':'3',},{'b':'2','d':'a'},{'b':'3','d':'4'}],'c':'1'}

    print r['a']['b']
    print r['a']['d']
    print r['c']
    Weakdancer
        2
    Weakdancer  
       2012-01-29 22:44:06 +08:00
    print r['a'][0]['b']
    print r['a'][0]['d']

    刚才写错了,不好意思
    gonghao
        3
    gonghao  
       2012-01-29 22:46:50 +08:00
    不懂为啥要这样,既然 a 里面是个数组,那么 r.a.b 到底是 1 还是 2 或者 3 呢?

    还有那个 'd'='a' 应该是 'd':'a' 吧?
    clowwindy
        4
    clowwindy  
       2012-01-29 22:59:36 +08:00
    likai
        5
    likai  
    OP
       2012-01-29 23:15:54 +08:00
    不好意思.可能我说得不太明白...我在用tornado在sae上作一个小东西...数据是用JSON取得的.
    结构为

    r = {'a':[{'b':'1','c':'1','d':'1'},{'b':'2','c':'2','d':'2'},{'b':'3','c':'3','d':'3'}],'f':'123456'}
    其中a,b,c,d,f是固定键名.只是值不同.
    a值list中是一个个数据结构相同的dict吧?为什么是数组?
    我如果print r.取得的结果会全部出来.
    如果我 print r['f']就不行了.直接报500了.
    以下是我 的实现代码...
    import os
    import tornado.wsgi
    import sae
    from mod import *


    class MainHandler(tornado.web.RequestHandler):
    def get(self):

    rs = getData(param)
    self.write(rs)#这样数据能全部打出来
    self.write(rs['f'])#这样的话就500错误


    app = tornado.wsgi.WSGIApplication([
    (r"/", MainHandler),
    ])

    application = sae.create_wsgi_app(app)
    reus
        6
    reus  
       2012-01-30 05:04:17 +08:00
    是在本地调试吗?500的话就看看server的错误信息呗
    你确定rs是dict不是str吗?
    ccdjh
        7
    ccdjh  
       2012-01-30 07:38:09 +08:00
    rs 的问题。我直接在sae试了一下。你看一下

    https://gist.github.com/1701355
    likai
        8
    likai  
    OP
       2012-01-30 08:07:57 +08:00
    谢谢各位帮忙,刚仔细看了下返回数据才发现最后一个键值缺少"",
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2209 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 15:57 · PVG 23:57 · LAX 08:57 · JFK 11:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.