Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 解析嵌套字典_Python_Dictionary - Fatal编程技术网

Python 解析嵌套字典

Python 解析嵌套字典,python,dictionary,Python,Dictionary,我想为所有URL条目解析以下嵌套字典。应将其写入列表中: {u'_id': ObjectId('56a22819ffd6f'), u'books': [{u'id': {u'id': u'4311'}, u'link': {u'name': u'Operating Business', u'url': u'http://ffff'}}, {u'id': {u'id': u'4310'}, u'link': {u'name': u'Operating Business',

我想为所有URL条目解析以下嵌套字典。应将其写入
列表中

{u'_id': ObjectId('56a22819ffd6f'),
 u'books': [{u'id': {u'id': u'4311'},
   u'link': {u'name': u'Operating Business',
    u'url': u'http://ffff'}},
  {u'id': {u'id': u'4310'},
   u'link': {u'name': u'Operating Business',
    u'url': u'http://zzzzz'}},
  {u'id': {u'id': u'7462'},
   u'link': {u'name': u'European Credit Trading',
    u'url': u'http://xxxx'}},
  {u'id': {u'id': u'3258'},
   u'link': {u'name': u'Operating Business',
    u'url': u'http://dddddd'}},
  {u'id': {u'id': u'7463'},
   u'link': {u'name': u'US Credit Trading',
    u'url': u'http://aaaaa'}}],
 u'created': datetime.datetime(2016, 1, 2, 13, 1, 12, 744000),
 u'id': u'lingering-smoke',
 u'valuationDate': datetime.datetime(170, 1, 1, 0, 0, 16, 821000)}
我该怎么做呢?

让dd=dict

dd = {full dictionary}
url_list = []
for ll in dd['books']:
    url_list.append(ll['link']['url'])

print url_list

[u'http://ffff', u'http://zzzzz', u'http://xxxx', u'http://dddddd', u'http://aaaaa']

假设@gkusner使用相同的
变量
,则可采用另一种方法来解决该问题,例如

>>> dd = {full dictionary}
>>> [ll['link']['url'] for ll in ['books']]
[u'http://ffff', u'http://zzzzz', u'http://xxxx', u'http://dddddd', u'http://aaaaa']

你试过什么吗?你错过了的:。这当然是一个正确的解决方案-唯一的缺点是列表理解对于初学者来说可能很难理解-我试图首先得到答案:}