列表格式如下:

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/envpython#-*-coding:utf-8-*-tree_search(d_dic,parent,son):k,v_dicd_dic.items():k==parent:d_dic[k][son]={}(,son):()tree_search(d_dic[k],parent,son)data_dic={}itemdata:parent,son=itemparent:data_dic[son]={}:tree_search(data_dic,parent,son)k,vdata_dic.items():(k,v)


调试结果:

>>>foritemindata:...parent,son=item...printparent,"---",son...None---AA---A1A---A1-1A1---A2A1-1---A2-3A2-3---A3-4A1---A2-2A2---A3A2-2---A3-3A3---A4None---BB---B1B1---B2B1---B2-2B2---B3None---CC---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': {}}}})