91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

實現一個列表變成字典的轉換

發布時間:2020-07-16 22:53:30 來源:網絡 閱讀:689 作者:027ryan 欄目:開發技術

列表格式如下:

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': {}}}})


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

龙井市| 茶陵县| 建水县| 格尔木市| 义马市| 马尔康县| 阿克陶县| 西丰县| 庆元县| 石首市| 镇雄县| 乐至县| 白玉县| 嘉祥县| 清苑县| 射洪县| 稷山县| 达拉特旗| 阿图什市| 洱源县| 修武县| 浦东新区| 宣恩县| 平山县| 诏安县| 龙海市| 灵宝市| 昭平县| 盐池县| 安化县| 徐水县| 乡宁县| 赤城县| 澄城县| 化州市| 梁山县| 和林格尔县| 梅河口市| 游戏| 遂宁市| 木兰县|