回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我试图通过查看特定键从嵌套字典中获取一组数据。你知道吗</p>
<p>我的嵌套词典如下所示:</p>
<pre><code>dict_items = {
"base_id": {
"some-id-C03": {
"index": 3,
"role": "admin",
"text": "test_A_02",
"data": {
"comment": "A test",
"version": "1",
"created": "05/09/18 14:18",
"created_by": "John"
},
"type": "subTesting",
},
"some-id-B01": {
"index": 1,
"role": "admin",
"text": "test_B_02",
"data": {
"comment": "B test",
"version": "1",
"created": "05/09/18 14:16",
"created_by": "Pete"
},
"type": "subTesting",
"id": "33441122-b655-8877-ccddeeff88bb"
},
"some-id-A03": {
"index": 1,
"role": "admin",
"text": "test_C_01",
"data": {
"comment": "C test",
"version": "1",
"created": "15/06/18 09:12",
"created_by": "Pete"
},
"type": "subTesting",
"id": "55667788-c122-8877-zzddff00bb11"
}
}
}
</code></pre>
<p>而我可以使用以下方法执行:</p>
<pre><code>for item in dict_items.get('base_id').values():
print item['data']['created_date']
</code></pre>
<p>如何判断/附加属于我所查询的<code>created_date</code>中的其他信息?你知道吗</p>
<p>例如,我想获取最新的2个信息(使用<code>datetime</code>模块),在本例中,它将返回属于键<code>some-id-C03</code>和<code>some-id-B01</code>的所有信息</p>
<p>这是我期望的输出:</p>
<pre><code>"some-id-C03": {
"index": 3,
"role": "admin",
"text": "test_A_02",
"data": {
"comment": "A test",
"version": "1",
"created": "05/09/18 14:18",
"created_by": "John"
},
"type": "subTesting",
},
"some-id-B01": {
"index": 1,
"role": "admin",
"text": "test_B_02",
"data": {
"comment": "B test",
"version": "1",
"created": "05/09/18 14:16",
"created_by": "Pete"
},
"type": "subTesting",
"id": "33441122-b655-8877-ccddeeff88bb"
}
</code></pre>