我试图通过查看特定键从嵌套字典中获取一组数据。你知道吗
我的嵌套词典如下所示:
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"
}
}
}
而我可以使用以下方法执行:
for item in dict_items.get('base_id').values():
print item['data']['created_date']
如何判断/附加属于我所查询的created_date
中的其他信息?你知道吗
例如,我想获取最新的2个信息(使用datetime
模块),在本例中,它将返回属于键some-id-C03
和some-id-B01
的所有信息
这是我期望的输出:
"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"
}
您可以选择与
"data"
键关联的所有值,然后根据月份进行排序:输出:
如果我理解正确,您希望能够查询此数据结构,以便最终得到与某些条件匹配的所有条目。你知道吗
我会这样设置:
然后你可以定义你想要的任何条件。检索在特定日期创建的所有项的示例:
希望这能让你朝着正确的方向前进。你知道吗
相关问题 更多 >
编程相关推荐