网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

实现一个列表变成字典的转换-创新互联

列表格式如下:

10年积累的成都做网站、成都网站制作、成都外贸网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有白银区免费网站建设让你可以放心的选择与我们合作。

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

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


当前题目:实现一个列表变成字典的转换-创新互联
文章位置:http://bjjierui.cn/article/dgohgh.html

其他资讯