将嵌套字典转换为列表
我知道这里有很多字典可以列出问题,但我找不到适合我的情况所需的信息,所以我问一个新问题。
一些背景:我为我的模型使用分层包,生成树结构的内置函数输出一个嵌套循环来指示父母、孩子等。我的目标是将逻辑保留在视图中并输出一个列表,以便我可以简单地在我的模板中循环它。
这是我的数据,采用树结构:
1
-1.1
--1.1.1
---1.1.1.1
--1.1.2
-1.2
--1.2.1
--1.2.2
-1.3
这是我得到的嵌套字典
{
<Part: 1.1>:
{
<Part: 1.1.1>:
{
<Part: 1.1.1.1>: {}
},
<Part: 1.1.2>: {}
},
<Part: 1.2>:
{
<Part: 1.2.1>: {},
<Part: 1.2.2>: {}
},
<Part: 1.3>: {}
}
,或者如果您不喜欢我尝试分解它的方式,这是我在一行中得到的内容:
{<Part: 1.1>: {<Part: 1.1.1>: {<Part: 1.1.1.1>: {}}, <Part: 1.1.2>: {}}, <Part: 1.2>: {<Part: 1.2.1>: {}, <Part: 1.2.2>: {}}, <Part: 1.3>: {}}
我的内容d 喜欢的是得到:
[<Part: 1.1>, <Part: 1.1.1>, <Part: 1.1.1.1>, <Part: 1.1.2>, <Part: 1.2>, <Part: 1.2.2>, <Part: 1.2.1>, <Part: 1.3>,]
我尝试迭代 dict.items
中的键,但随后我只得到顶级键 (1.1, 1.2, 1.3)
我需要做什么才能更深入?
谢谢!
I know there are many dict to list questions on here but I can't quite find the information I need for my situation so I'm asking a new quetion.
Some background: I'm using a hierarchical package for my models and the built-in function which generates the tree structure outputs a nested loop to indicate parents, children, etc. My goal is to keep the logic in views and output a list so that I can simply loop over it in my templates.
Here is my data, in the tree structure:
1
-1.1
--1.1.1
---1.1.1.1
--1.1.2
-1.2
--1.2.1
--1.2.2
-1.3
Here is the nested dictionary I am getting as a result
{
<Part: 1.1>:
{
<Part: 1.1.1>:
{
<Part: 1.1.1.1>: {}
},
<Part: 1.1.2>: {}
},
<Part: 1.2>:
{
<Part: 1.2.1>: {},
<Part: 1.2.2>: {}
},
<Part: 1.3>: {}
}
or if you don't like the way I tried to break it up, here is what I get in a single line:
{<Part: 1.1>: {<Part: 1.1.1>: {<Part: 1.1.1.1>: {}}, <Part: 1.1.2>: {}}, <Part: 1.2>: {<Part: 1.2.1>: {}, <Part: 1.2.2>: {}}, <Part: 1.3>: {}}
What I'd like is to get:
[<Part: 1.1>, <Part: 1.1.1>, <Part: 1.1.1.1>, <Part: 1.1.2>, <Part: 1.2>, <Part: 1.2.2>, <Part: 1.2.1>, <Part: 1.3>,]
I've tried just iterating over the key in dict.items
but then I only get the top level keys (1.1, 1.2, 1.3)
What do I need to do to get deeper?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为递归可以成为你的朋友:
I think recursion can be your friend :
之前的所有解决方案都递归地构建大量列表,然后将它们扩展到越来越大的列表,直到得到最终答案。虽然它有效,但从性能角度来看它并不是最佳的,因为它创建了许多您实际上不需要的列表,并且涉及将相同的项目多次扩展到其父列表中。有些解决方案还忘记对键进行排序。
根据 python -m timeit -s "import flatten" "flatten.test()",此方法每个循环使用 8.57 usecs,而 Cédrics 答案的每个循环使用 14.4 usecs,当更新为也排序时正确输出(
for key, value insorted(father.items()):
)All of the previous solutions build lots of lists recursively and then extend them back into bigger and bigger lists until you have the final answer. While it works, it is not optimal from a performance perspective, since it creates lots of lists that you really don't need, and involves extending the same items many times into their parent lists. Some solutions also forget to sort on the keys.
According to
python -m timeit -s "import flatten" "flatten.test()"
, this method uses 8.57 usecs per loop, compared with 14.4 usecs per loop for Cédrics answer, when updated to also sort the output properly (for key, value in sorted(father.items()):
)起始...呃,我的意思是,递归。试试这个:
Inception... er, I mean, recursion. Try this:
根据Mihai的回答:你需要对你的钥匙进行排序,否则你可能会遇到问题:
Based on Mihai's answer: you need to sort your keys, else you will probably have problems:
这个问题不能通过简单的字典迭代来解决。您需要一种名为 递归下降 的算法,
该算法已经过测试并且正在工作
希望这会有所帮助
This problem can't be solved by simple iteration over the dictionary. You need a type of algorithm called recursive descent
This has been tested and is working
Hope this helps