您好,登錄后才能下訂單哦!
列表格式如下:
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'),
]
轉(zhuǎn)換后的結(jié)果為:
data_dic = {
'A': {
'A1': {
'A2':{
'A3':{
'A4':{}
}
},
'A2-2':{
'A3-3':{}
}
}
},
'B':{
'B1':{
'B2':{
'B3':{}
},
'B2-2':{}
}
},
'C':{
'C1':{}
}
}
實現(xiàn)方式代碼如下:
#!/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)
調(diào)試結(jié)果:
>>> 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 >>>
輸出結(jié)果如下:
('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': {}}}})
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。