2012-07-09 75 views
1

我有一个由JSON结果组成的python字典。该字典包含嵌套字典,其中包含嵌套列表,其中包含嵌套字典。还在我这儿?这里有一个例子:循环遍历嵌入在字典中的列表

{'hits':{'results':[{'key1':'value1', 
        'key2':'value2', 
        'key3':{'sub_key':'sub_value'}}, 
        {'key1':'value3', 
        'key2':'value4', 
        'key3':{'sub_key':'sub_value2'}} 
        ]}} 

我想从字典里得到的是每一个sub_keysub_vale并将其存储在不同的列表。无论我尝试什么,我都会收到错误。

这是我最后一次尝试吧:

inner_list=mydict['hits']['results']#This is the list of the inner_dicts 

index = 0 
    for x in inner_list: 
     new_dict[index] = x[u'sub_key'] 
     index = index + 1 

print new_dict 

它印刷的前几个结果,然后开始在原有的字典返回的一切。我无法理解它。如果我用print声明替换new_dict[index]行,它会完美地打印到屏幕上。真的需要一些输入!

for x in inner_list: 
    print x[u'sub_key'] 
+1

请张贴错误的输出。该程序看起来是正确的,所以我有一种感觉,我不明白它在做什么,这是违背你期望 – inspectorG4dget 2012-07-09 18:10:13

+0

@ inspectorG4dget只是在那里运行的确切代码,并有回溯错误'文件“C:\ Python27 \ test.py” ,第24行,主 newresults [index] = x [u'sub_key'] IndexError:列表分配索引超出范围 – adohertyd 2012-07-09 18:16:21

+0

如果'newresults'是您发布为'new_dict'并且是'list'的列表, ,然后尝试'append'ing它,而不是分配给一个尚不存在的索引 – inspectorG4dget 2012-07-09 18:33:34

回答

1

做出一些假设后:

[e['key3']['sub_key'] for e in x['hits']['results']] 

要改变每实例:

for e in x['hits']['results']: 
e['key3']['sub_key'] = 1 
+0

谢谢。就是这样。我最初是一名C程序员,所以我发现Python语法仍然很奇怪。如果我想要改变子键的值呢?在sub_value_y的变化sub_value_y – adohertyd 2012-07-09 18:34:45

+0

@adohertyd:例如'x ['hits'] ['results'] [0] ['key3'] ['sub_key'] = y' – 2012-07-09 18:36:58

+0

但在每个实例中更改它呢?如在循环中? – adohertyd 2012-07-09 18:38:23

1

x是对for x in ...

x={'key1':'value1', 
       'key2':'value2', 
       'key3':{'sub_key':'sub_value'}}, 

通知第一次迭代的字典

,有在x没有钥匙sub_key而是x['key3']['sub_key']

+0

为什么然后,当我做'打印x ['sub_key']'它打印没有问题? – adohertyd 2012-07-09 18:22:44

+0

它不适合我......这会引发错误 ' >>>对于x在内: ...打印X [u'sub_key'] ... 回溯(最近通话最后一个): 文件“”,第2行,在 KeyError:u'sub_key'' – 2012-07-09 18:24:55

+0

从sub_key的前面删除u对不起 – adohertyd 2012-07-09 18:28:54

1
>>> dic={'hits':{'results':[{'key1':'value1', 
        'key2':'value2', 
        'key3':{'sub_key':'sub_value'}}, 
        {'key1':'value3', 
        'key2':'value4', 
        'key3':{'sub_key':'sub_value2'}} 
        ]}} 
>>> inner_list=dic['hits']['results'] 
>>> [x[y]['sub_key'] for x in inner_list for y in x if isinstance(x[y],dict)] 
['sub_value', 'sub_value2'] 

和如果你确定它的key3总是包含在内dict,则:

>>> [x['key3']['sub_key'] for x in inner_list] 
['sub_value', 'sub_value2'] 

不使用List comprehensions

>>> lis=[] 
>>> for x in inner_list: 
    for y in x: 
     if isinstance(x[y],dict): 
      lis.append(x[y]['sub_key']) 


>>> lis 
['sub_value', 'sub_value2'] 
+1

哇,这是一口!我一点都不会让自己变得轻松! – adohertyd 2012-07-09 18:23:41

+0

非常感谢!正如我对Marco说的,我仍然处于C编程模式,所以python的语法对我来说还是有点奇怪。谢谢。 – adohertyd 2012-07-09 18:36:06

+0

是的,列表理解对于一个新的Python程序员来说很难理解,所以我添加了一个简单版本的答案(不使用List理解) – 2012-07-09 18:43:28

1

索引错误来自new_dict[index]其中index大于new_dict的大小。

应该考虑列表理解。它通常更好,但要帮助理解这是如何在循环中起作用的。试试这个。

new_list = [] 
for x in inner_list: 
    new_list.append(x[u'sub_key']) 

print new_list 

如果你想坚持的字典,而是使用索引的关键试试这个:

index = 0 
new_dict = {} 
    for x in inner_list: 
     new_dict[index] = x[u'sub_key'] 
     index = index + 1 

print new_dict 

好了,根据您在下面的意见,我觉得这是你想要的。

inner_list=mydict['hits']['results']#This is the list of the inner_dicts 

new_dict = {} 
for x in inner_list: 
    new_dict[x['key2']] = x['key3']['sub_key'] 

print new_dict 
+0

是的,它是一个更大程序的一部分,我从3本词典中抽取特定元素,将它们放入列表中,并根据特定标准进行比较。如果我想从原始字典的某些元素创建字典,那么从列表中我可以更容易地做到这一点。 – adohertyd 2012-07-09 18:39:48

+0

我会怎么做呢? – adohertyd 2012-07-09 18:42:25

+0

实际上与代码中的语法非常相似,但您需要一个字符串作为键。 new_dict [key] =值。你想使用哪个元素作为键,哪个元素用于该值。 – ChipJust 2012-07-09 18:44:31

1

你忘了嵌套的水平。

for x in inner_list: 
    for y in x: 
     if isinstance(x[y], dict) and 'sub_key' in x[y]: 
      new_dict.append(x[y]['sub_key'])