请教一下,python
内给定以下dict
, 已知"Huber"= "Tucker"
, 求"Huber"= "Tucker"
所在dict
内的data_id
及其在"Armstrong"
内的索引
{
"Armstrong": [
{
"date_id": "1",
"show_name": "Quynn",
"description": "Quynn",
"commid": "Ward",
"Mooney": {
"Carpenter": "Bolton",
"Amena": "Elijah",
"Rice": {
"Kitra": false,
"Huber": "Shafira",
"novalidate": "Valdez0"
}
},
"Graham": "Mariko",
"status": {
"status": "OFF",
},
"Alyssa": {
"Little": "Noelani",
"device_id": "Sparks",
},
"Judah": "Huff"
},
{
"date_id": "2",
"show_name": "Holland",
"description": "Holland",
"commid": "Spencer",
"Mooney": {
"Carpenter": "Illana",
"Amena": "Rosa",
"Rice": {
"Kitra": false,
"Huber": "Tucker",
"novalidate": "Evelyn"
}
},
"Graham": "Janna",
"status": {
"status": "ON",
},
"Alyssa": {
"Little": "Drew",
"device_id": "Herman",
},
"Judah": "England"
}
# ....很多
],
"page": {
"offset": 0,
"limit": 100,
"total": 2
}
}
1
wannaspring 2022-02-08 11:26:47 +08:00
def fun (targetv, dicts):
if isinstance(dicts, dict): for key,value in d.items(): if value == targetv: return key if isinstance(value, dict): fun(targetv, value) if isinstance(dicts, list): |
2
wannaspring 2022-02-08 11:27:45 +08:00
不是很熟悉 python ,但是这样是不是可以?,剩下的 list 自己补一下
|
3
NessajCN 2022-02-08 11:56:41 +08:00
for r in dict["Armstrong"]:
if r["Mooney"]["Rice"]["Huber"]=="Tucker": print r["date_id"] print dict["Armstrong"].index(r) |
4
ykk 2022-02-08 13:43:17 +08:00
字典是 O(n) 遍历就完了,已知某些规律可以缩小遍历空间
|
5
seven123 2022-02-08 14:08:39 +08:00
遍历就完了
|
6
julyclyde 2022-02-08 15:49:06 +08:00
这是不是传说中的“倒排索引”?
|
7
wsgfz000 2022-02-08 16:17:17 +08:00
试试 bidict
|
8
Latin 2022-02-08 17:36:10 +08:00
|
9
SenLief 2022-02-08 18:41:43 +08:00
这不是遍历吗
|
10
wellsc 2022-02-08 18:57:40 +08:00 via iPhone
倒排索引
|