Python如何获得目录 /二进制树的所有分支组合?我三月
我有一个词典结构,我想在三月的时候井井有条,
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
打印:
Try:
Prints: