实现一个列表变成字典的转换
2016-11-03 09:36
537 查看
列表格式如下:
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':{}
}
}
实现方式代码如下:
调试结果:
输出结果如下:
('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': {}}}})
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':{}
}
}
实现方式代码如下:
#!/usr/bin/env python # -*- coding:utf-8 -*- tree_search(d_dic,parent,son): k,v_dic d_dic.items(): k == 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)
调试结果:
>>> for item in data: ... parent,son = item ... print parent,"---",son ... 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 >>>
输出结果如下:
('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': {}}}})
相关文章推荐
- Python实现列表转换成字典数据结构的方法
- python - 列表分组技巧 #写出一段 Python 代码,实现分组一个 list 里面的元素,如 [1,2,3,...100]变成 [[1,2,3],[4,5,6]....]
- iOS实现类似QQ的好友列表,自由展开折叠(在原来TableView的基础上添加一个字典,一个Button)
- 如何将两个列表变成一个python字典
- Python实现列表转换成字典数据结构的方法
- 用PYTHON实现一个扁平化的字典,如{'a': {'b': '1'}}扁平化处理后变成{'a.b': 1}?
- Python读取一个excel,并将其变成一个字典列表
- 一个java实现的简单日历,采用左树右列表的方式实现,具有参考意义
- 一个应用二叉搜索树实现的字典,并存储结构于文件中 选择自 nickhuang2002 的 Blog
- Oracle一个典型行列转换的几种实现方法(转)
- 使用反射机制实现实体列表到DataTable的深层字段转换
- 一个页面搞定几乎所有的列表需求的实现思路和一点代码。
- 一个将数据文件转换成excel文件打印的java实现方法的代码片断(Struts+poi)
- dl,dt,dd实现新闻列表的一个实例
- 自己编写的一个Json工具类,实现了反射将整个Object转换为Json对象的功能,支持Hibernate的延迟加载对象
- 一个页面搞定几乎所有的列表需求的实现思路和一点代码。
- 综合应用WPF/WCF/WF/LINQ之四十一:实现一个简单的DataGrid之ComboBox可选列表的赋值和已选择的值的保存
- 一个实现下拉列表内汉字内容排序的例子
- 将OpenOffice.org变成一个文档格式转换工具
- 自己编写的一个Json工具类,实现了反射将整个Object转换为Json对象的功能,支持Hibernate的延迟加载对象