python在列表中获取字典值 [英] python getting dictionary values in lists

查看:1790
本文介绍了python在列表中获取字典值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曾经在桌子上敲我的头! 我有一个在json中下载的网址,然后parsed_json = json.loads(response_body)到python字典.问题是所需的数据(嵌入字典的数据在列表中)在名为list的字典中.如果我进行字典计数或键或其他任何操作,则实际上只有37个字典时,我只会获得第3级.

Been banging my head on desk! I have an url that is downloaded in json, then parsed_json = json.loads(response_body) to python dictionary. The issue is that data(embedded in dicts is in list) that is needed is in a dictionary called list. If I do a dict count or key or anything I just get the first level of 3 when there is actually 37 dicts .

for key, value in parsed_json.iteritems() :
    print key, value

一个星期后,我无法弄清楚如何从列表中的字典中获取所有值,可以通过使用类似方法获得一个值,但是我无法增加计数.

After a week I cannot figure out how to get all values from dicts in the list, I can get one by using something like this but I cannot increment the count.

print parsed_json['list'][(1)]['value']

这是一个digi xbee云,在获得值之后,需要对某些数字进行一些数学运算,因为结果以MV而非C度为单位. 任何获得这些价值的方向都值得赞赏.

This is a digi xbee cloud get, after getting the values there needs to be some math done on some, as the results are in MV's not C degrees. Any direction to get the values greatly appreciated.

数据段:

{u'count': 37,
 u'list': [{u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/management/connections',
            u'id': u'00000000-00000000-00409DFF-FF818A13/management/connections',
            u'server_timestamp': u'2015-11-21T04:21:45.407Z',
            u'timestamp': u'2015-11-21T04:21:45.269Z',
            u'type': u'JSON',
            u'value': u'{"connectTime":"2015-11-21T04:21:45.269Z","type":"Ethernet","remoteIp":"72.38.16.255","localIp":"192.168.2.106","session":"17c6e5d2-3d9d-439b-aa8a-36f050bf8b9c"}'},
           {u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD1',
            u'id': u'00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD1',
            u'server_timestamp': u'2015-11-22T08:14:15.945Z',
            u'timestamp': u'2015-11-22T08:14:09.101Z',
            u'type': u'INTEGER',
            u'value': u'1'},
           {u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD2',
            u'id': u'00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD2',
            u'server_timestamp': u'2015-11-22T08:14:15.964Z',
            u'timestamp': u'2015-11-22T08:14:09.377Z',
            u'type': u'INTEGER',
            u'value': u'613'},
           {u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD3',
            u'id': u'00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD3',
            u'server_timestamp': u'2015-11-22T08:14:15.930Z',
            u'timestamp': u'2015-11-22T08:14:08.854Z',
            u'type': u'INTEGER',
            u'value': u'852'},
           {u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:50]!/AD1',
            u'id': u'00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:50]!/AD1',
            u'server_timestamp': u'2015-11-22T08:14:15.834Z',
            u'timestamp': u'2015-11-22T08:14:07.477Z',
            u'type': u'INTEGER',
            u'value': u'0'},
           {u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:50]!/AD2',

推荐答案

这应该有效:

for index in range(parsed_json['count']):
    print(parsed_json['list'][index]['value'])

或更简单:

for item in parsed_json['list']:
    print(item['value'])

您可以通过遍历items()

for entry in parsed_json['list']:
    for key, value in entry.items():
        print(key)
        print('    ', value)

在Python 2中编写print ' ', value,因为print是Python 3中的函数,但在Python 2中仍然是语句.如果您不熟悉Python,请从Python 3开始.

In Python 2 write print ' ', value because print is a function in Python 3 but still a statement in Python 2. If you are new to Python, start with Python 3. Python 2 is the legacy Python.

这篇关于python在列表中获取字典值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆