相关疑难解决方法(0)

在嵌套的python词典和列表中查找所有出现的键

我有一个这样的字典:

{ "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" }] } ] } 
Run Code Online (Sandbox Code Playgroud)

基本上是具有任意深度的嵌套列表,字典和字符串的字典.

遍历此方法以提取每个"id"键的值的最佳方法是什么?我想实现相当于XPath查询,如"// id"."id"的值始终是一个字符串.

所以从我的例子来看,我需要的输出基本上是:

["abcde", "qwerty", "xyz", "fghi", "asdf", "yuiop"]
Run Code Online (Sandbox Code Playgroud)

订单并不重要.

python recursion dictionary traversal

68
推荐指数
8
解决办法
6万
查看次数

标签 统计

dictionary ×1

python ×1

recursion ×1

traversal ×1