博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现一个列表变成字典的转换
阅读量:6161 次
发布时间:2019-06-21

本文共 3976 字,大约阅读时间需要 13 分钟。

列表格式如下:

data = [

    (None,'A'),

    ('A','A1'),

    ('A','A1-1'),

    ('A1','A2'),

    ('A1-1','A2-3'),

    ('A2-3','A3-4'),

    ('A1','A2-2'),

    ('A2','A3'),

    ('A2-2','A3-3'),

    ('A3','A4'),

    (None,'B'),

    ('B','B1'),

    ('B1','B2'),

    ('B1','B2-2'),

    ('B2','B3'),

    (None,'C'),

    ('C','C1'),

]

转换后的结果为:

data_dic = {

    'A': {

        'A1': {

            'A2':{

                'A3':{

                    'A4':{}

                }

            },

            'A2-2':{

                'A3-3':{}

            }

        }

    },

    'B':{

        'B1':{

            'B2':{

                'B3':{}

            },

            'B2-2':{}

        }

    },

    'C':{

        'C1':{}

    }

}

实现方式代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
#!/usr/bin/env python
    
# -*- coding:utf-8 -*-
     
    
tree_search(d_dic,parent,son):
        
k,v_dic d_dic.items():
            
=
= 
parent: d_dic[k][son] 
= 
{}
                
(,son)
                
: ()
                
tree_search(d_dic[k],parent,son)
     
     
     
    
data_dic 
= 
{}
     
    
item data:
        
parent,son 
= 
item
        
parent : data_dic[son] 
= 
{}
        
: tree_search(data_dic,parent,son)
    
k,v data_dic.items():
        
(k,v)

调试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>>> 
for 
item 
in 
data:
...      parent,son 
= 
item
...      
print 
parent,
"---"
,son
... 
None 
-
-
- 
A
-
-
- 
A1
-
-
- 
A1
-
1
A1 
-
-
- 
A2
A1
-
1 
-
-
- 
A2
-
3
A2
-
3 
-
-
- 
A3
-
4
A1 
-
-
- 
A2
-
2
A2 
-
-
- 
A3
A2
-
2 
-
-
- 
A3
-
3
A3 
-
-
- 
A4
None 
-
-
- 
B
-
-
- 
B1
B1 
-
-
- 
B2
B1 
-
-
- 
B2
-
2
B2 
-
-
- 
B3
None 
-
-
- 
C
-
-
- 
C1
>>>

输出结果如下:


('find parent of: ', 'A1')

('find parent of: ', 'A1-1')

going to further layer...

('find parent of: ', 'A2')

going to further layer...

going to further layer...

going to further layer...

('find parent of: ', 'A2-3')

going to further layer...

going to further layer...

going to further layer...

going to further layer...

('find parent of: ', 'A3-4')

going to further layer...

('find parent of: ', 'A2-2')

going to further layer...

going to further layer...

('find parent of: ', 'A3')

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

('find parent of: ', 'A3-3')

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

('find parent of: ', 'A4')

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

('find parent of: ', 'B1')

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

('find parent of: ', 'B2')

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

('find parent of: ', 'B2-2')

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

('find parent of: ', 'B3')

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

going to further layer...

('find parent of: ', 'C1')

('A', {'A1': {'A2': {'A3': {'A4': {}}}, 'A2-2': {'A3-3': {}}}, 'A1-1': {'A2-3': {'A3-4': {}}}})

('C', {'C1': {}})

('B', {'B1': {'B2-2': {}, 'B2': {'B3': {}}}})

      本文转自027ryan  51CTO博客,原文链接:http://blog.51cto.com/ucode/1868782
,如需转载请自行联系原作者
你可能感兴趣的文章
会计基础_001
查看>>
小程序: 查看正在写的页面
查看>>
Jenkins持续集成环境部署
查看>>
MWeb 1.4 新功能介绍二:静态博客功能增强
查看>>
预处理、const与sizeof相关面试题
查看>>
爬虫豆瓣top250项目-开发文档
查看>>
有趣的数学书籍
查看>>
teamviewer 卸载干净
查看>>
eclipse的maven、Scala环境搭建
查看>>
架构师之路(一)- 什么是软件架构
查看>>
USACO 土地购买
查看>>
【原创】远景能源面试--一面
查看>>
B1010.一元多项式求导(25)
查看>>
10、程序员和编译器之间的关系
查看>>
配置 RAILS FOR JRUBY1.7.4
查看>>
修改GRUB2背景图片
查看>>
Ajax异步
查看>>
好记性不如烂笔杆-android学习笔记<十六> switcher和gallery
查看>>
JAVA GC
查看>>
3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
查看>>