Python如何获得目录 /二进制树的所有分支组合?我三月

发布于 2025-01-20 20:00:52 字数 3097 浏览 2 评论 0原文

我有一个词典结构,我想在三月的时候井井有条,

my_data = {
  "my_root": {
      "first_key": {
          "second_key": {
              "bunch": {
                  "bunch1": [
                      "a1",
                      "a2"
                  ],
                  "bunch2": [
                      "b2",
                      "b2"
                  ],
                  "ad": [
                      "ad1"
                  ]
              }
          },
          "another_key": {
              "one": [
                  "1",
                  "2"
              ],
              "two": [
                  "11",
                  "22"
              ]
          }
      }
  }
}

我试图尝试

["my_root"]
["my_root", "first_key"]
["my_root", "first_key", "second_key"]
["my_root", "first_key", "second_key", "bunch"]
["my_root", "first_key", "second_key", "bunch", "bunch1"]
["my_root", "first_key", "second_key", "bunch", "bunch1", "a1"]
["my_root", "first_key", "second_key", "bunch", "bunch1", "a2"]
["my_root", "first_key", "second_key", "bunch", "bunch2"]
["my_root", "first_key", "second_key", "bunch", "bunch2", "b1"]
["my_root", "first_key", "second_key", "bunch", "bunch2", "b2"]
["my_root", "first_key", "second_key", "bunch", "bunch2", "b2"]
["my_root", "first_key", "second_key", "bunch", "ad",     "ad1"]
["my_root", "first_key", "another_key"]
["my_root", "first_key", "another_key", "one"]
["my_root", "first_key", "another_key", "one", "1"]
["my_root", "first_key", "another_key", "one", "2"]
["my_root", "first_key", "another_key", "two", "11"]
["my_root", "first_key", "another_key", "two", "22"]

这样的东西(谢谢Valentin Briukhanov),

  def _ordered_components_by_path(indict, pre=None):
      pre = pre[:] if pre else []
      if isinstance(indict, dict):
          for key, value in indict.items():
              if isinstance(value, dict):
                  for d in _ordered_components_by_path(value, pre + [key]):
                      yield d
              elif isinstance(value, list) or isinstance(value, tuple):
                  for v in value:
                      for d in _ordered_components_by_path(v, pre + [key]):
                          yield d
              else:
                  yield pre + [key, value]
      else:
          yield pre + [indict]

  def ordered_components_by_path():
      return _ordered_components_by_path(my_data)

但是这只是给我所有的目的,就像我在下面的印刷

for i in ordered_components_by_path:
  print(i)

['my_root', 'first_key', 'second_key', 'bunch', 'bunch1', 'a1']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch1', 'a2']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch2', 'b2']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch2', 'b2']
['my_root', 'first_key', 'second_key', 'bunch', 'ad', 'ad1']
['my_root', 'first_key', 'another_key', 'one', '1']
['my_root', 'first_key', 'another_key', 'one', '2']
['my_root', 'first_key', 'another_key', 'two', '11']
['my_root', 'first_key', 'another_key', 'two', '22']

一样有适当的实现方法还是图书馆?谢谢大家。

i have a dictionary structure, that i want to get all the combination in order as i march

my_data = {
  "my_root": {
      "first_key": {
          "second_key": {
              "bunch": {
                  "bunch1": [
                      "a1",
                      "a2"
                  ],
                  "bunch2": [
                      "b2",
                      "b2"
                  ],
                  "ad": [
                      "ad1"
                  ]
              }
          },
          "another_key": {
              "one": [
                  "1",
                  "2"
              ],
              "two": [
                  "11",
                  "22"
              ]
          }
      }
  }
}

i am trying to get

["my_root"]
["my_root", "first_key"]
["my_root", "first_key", "second_key"]
["my_root", "first_key", "second_key", "bunch"]
["my_root", "first_key", "second_key", "bunch", "bunch1"]
["my_root", "first_key", "second_key", "bunch", "bunch1", "a1"]
["my_root", "first_key", "second_key", "bunch", "bunch1", "a2"]
["my_root", "first_key", "second_key", "bunch", "bunch2"]
["my_root", "first_key", "second_key", "bunch", "bunch2", "b1"]
["my_root", "first_key", "second_key", "bunch", "bunch2", "b2"]
["my_root", "first_key", "second_key", "bunch", "bunch2", "b2"]
["my_root", "first_key", "second_key", "bunch", "ad",     "ad1"]
["my_root", "first_key", "another_key"]
["my_root", "first_key", "another_key", "one"]
["my_root", "first_key", "another_key", "one", "1"]
["my_root", "first_key", "another_key", "one", "2"]
["my_root", "first_key", "another_key", "two", "11"]
["my_root", "first_key", "another_key", "two", "22"]

i have try something like this( thanks Valentin Briukhanov)

  def _ordered_components_by_path(indict, pre=None):
      pre = pre[:] if pre else []
      if isinstance(indict, dict):
          for key, value in indict.items():
              if isinstance(value, dict):
                  for d in _ordered_components_by_path(value, pre + [key]):
                      yield d
              elif isinstance(value, list) or isinstance(value, tuple):
                  for v in value:
                      for d in _ordered_components_by_path(v, pre + [key]):
                          yield d
              else:
                  yield pre + [key, value]
      else:
          yield pre + [indict]

  def ordered_components_by_path():
      return _ordered_components_by_path(my_data)

but this one just give the all the ends like i am printing below

for i in ordered_components_by_path:
  print(i)

['my_root', 'first_key', 'second_key', 'bunch', 'bunch1', 'a1']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch1', 'a2']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch2', 'b2']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch2', 'b2']
['my_root', 'first_key', 'second_key', 'bunch', 'ad', 'ad1']
['my_root', 'first_key', 'another_key', 'one', '1']
['my_root', 'first_key', 'another_key', 'one', '2']
['my_root', 'first_key', 'another_key', 'two', '11']
['my_root', 'first_key', 'another_key', 'two', '22']

is there a proper way to do achieve or a library? thanks guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

默嘫て 2025-01-27 20:00:52

尝试:

def get_path(d, curr_path):
    if isinstance(d, dict):
        for k, v in d.items():
            yield curr_path + [k]
            yield from get_path(v, curr_path + [k])
    elif isinstance(d, list):
        for v in d:
            yield from get_path(v, curr_path)
    else:
        yield curr_path + [d]


for p in get_path(my_data, []):
    print(p)

打印:

['my_root']
['my_root', 'first_key']
['my_root', 'first_key', 'second_key']
['my_root', 'first_key', 'second_key', 'bunch']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch1']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch1', 'a1']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch1', 'a2']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch2']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch2', 'b2']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch2', 'b2']
['my_root', 'first_key', 'second_key', 'bunch', 'ad']
['my_root', 'first_key', 'second_key', 'bunch', 'ad', 'ad1']
['my_root', 'first_key', 'another_key']
['my_root', 'first_key', 'another_key', 'one']
['my_root', 'first_key', 'another_key', 'one', '1']
['my_root', 'first_key', 'another_key', 'one', '2']
['my_root', 'first_key', 'another_key', 'two']
['my_root', 'first_key', 'another_key', 'two', '11']
['my_root', 'first_key', 'another_key', 'two', '22']

Try:

def get_path(d, curr_path):
    if isinstance(d, dict):
        for k, v in d.items():
            yield curr_path + [k]
            yield from get_path(v, curr_path + [k])
    elif isinstance(d, list):
        for v in d:
            yield from get_path(v, curr_path)
    else:
        yield curr_path + [d]


for p in get_path(my_data, []):
    print(p)

Prints:

['my_root']
['my_root', 'first_key']
['my_root', 'first_key', 'second_key']
['my_root', 'first_key', 'second_key', 'bunch']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch1']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch1', 'a1']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch1', 'a2']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch2']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch2', 'b2']
['my_root', 'first_key', 'second_key', 'bunch', 'bunch2', 'b2']
['my_root', 'first_key', 'second_key', 'bunch', 'ad']
['my_root', 'first_key', 'second_key', 'bunch', 'ad', 'ad1']
['my_root', 'first_key', 'another_key']
['my_root', 'first_key', 'another_key', 'one']
['my_root', 'first_key', 'another_key', 'one', '1']
['my_root', 'first_key', 'another_key', 'one', '2']
['my_root', 'first_key', 'another_key', 'two']
['my_root', 'first_key', 'another_key', 'two', '11']
['my_root', 'first_key', 'another_key', 'two', '22']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文