擅长:python、mysql、java
<p>要访问字典中的元素,必须执行以下操作:</p>
<pre><code>dict_name[key_name] #in this case: list1[key3]
</code></pre>
<p>试试这个:</p>
<pre><code>newlist = []
list = [{ "key1":"val11", "key2":"val12", "key3":{dict1} },{ "key1":"val21", "key2":"val22", "key3":{dict1} }]
for innerdict in list:
newlist += list(innerdict["key3"])
print newlist #This will give all the values of key3 in all the dictionaries
</code></pre>
<p>如果列表中的<code>dict1</code>是一个集合,那么上面的示例将很好地工作,但是如果它是一个字典,那么它将给出一个列表,但是只包含键,因此如果它是一个字典,请尝试以下操作:</p>
<pre><code>newlist = []
list = [{ "key1":"val11", "key2":"val12", "key3":{dict1} },{ "key1":"val21", "key2":"val22", "key3":{dict1} }]
for innerdict in list:
newlist.append(innerdict["key3"])
print newlist #This will give all the values of key3 in all the dictionaries
</code></pre>