mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-05 05:18:52 +08:00
b745b6f707
Signed-off-by: zhuwenxing <wenxing.zhu@zilliz.com>
30 lines
938 B
Python
30 lines
938 B
Python
|
|
|
|
def findkeys(node, kv):
|
|
# refer to https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-dictionaries-and-lists
|
|
if isinstance(node, list):
|
|
for i in node:
|
|
for x in findkeys(i, kv):
|
|
yield x
|
|
elif isinstance(node, dict):
|
|
if kv in node:
|
|
yield node[kv]
|
|
for j in node.values():
|
|
for x in findkeys(j, kv):
|
|
yield x
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
d = { "id" : "abcde",
|
|
"key1" : "blah",
|
|
"key2" : "blah blah",
|
|
"nestedlist" : [
|
|
{ "id" : "qwerty",
|
|
"nestednestedlist" : [
|
|
{ "id" : "xyz", "keyA" : "blah blah blah" },
|
|
{ "id" : "fghi", "keyZ" : "blah blah blah" }],
|
|
"anothernestednestedlist" : [
|
|
{ "id" : "asdf", "keyQ" : "blah blah" },
|
|
{ "id" : "yuiop", "keyW" : "blah" }] } ] }
|
|
print(list(findkeys(d, 'id'))) |