python中列表的分类树实现

2024-04-25 15:23:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Python中实现具有无限深度子类别的目录树,我有多个列表元素,我必须从这些元素中进行创建。在

让我详细解释一下,这是我的清单。在

>mylists = [
>['home', 'desktop', 'mouse', 'wireless'],
>['home', 'desktop', 'mouse', 'wired'],
>['home', 'laptop', 'mouse'],
>['home', 'laptop', '13-inch'],
>]

我希望输出是:

^{pr2}$

我知道我应该使用递归函数来迭代列表,并使一些东西变魔术。在

为此,我分两步来完成这项任务: 1将这个嵌套列表转换成嵌套字典(只是为了保持层次结构) 2将嵌套dict转换为上面解释的所需格式。在

步骤1:以下是我将嵌套列表转换为嵌套dict的代码:

>def make_rec_dict(dict):
>    d = {}
>    for path in dict:
>        current_level = d
>        for part in path:
>            if part not in current_level:
>                current_level[part] = {}
>            current_level = current_level[part]
>            #print part
>    return d
>
>make_rec_dict(mylists)
>{'home': {'laptop': {'mouse': {}, '13-inch': {}}, 'desktop': {'mouse': {'wireless': {}, 'wired': {}}}}}

第2步: 以所需格式显示

spaces = { 1 : '', 2 : '>>>>', 3 : '>>>>>>>>', 4 : '>>>>>>>>>>>>', 5 : '>>>>>>>>>>>>>>>>>>>>'}
def display_recusively(dictionary, level=0):
    if type(dictionary) is dict: 
        values = []  # get all the values and parse each again
        for key, value in dictionary.iteritems():
            if value != '':
                print spaces[level], key
                values.append(value)
                level = level + 1
                return display_recusively(values, level)
            elif value == '':  # this is the last child
                print spaces[level], key

    elif type(dictionary) is list: 
        for d in dictionary:
            return display_recusively(d, level)
    else:
        print dictionary

但该代码的缺点是,我无法获取子元素相对于父元素的链接。我的意思是鼠标和鼠标应该是不同的,上面代码的缺点是它脱离了循环。。在

因此,请向我建议或纠正我实现以下目标的更好方法:

  • 1.带深度级别的格式化类别树
  • 2.构件应带有制作锚定标签的母材(如最后一段所述)

Tags: in元素home列表fordictionaryvaluecurrent
2条回答

这样可以得到相同的输出-

my_lists = [['home', 'desktop', 'mouse', 'wireless'], ['home', 'desktop', 'mouse', 'wired'],
            ['home', 'laptop', 'mouse'], ['home', 'laptop', '13-inch']]

path_list = []

for lists in my_lists:

    path = ''
    for i in range(len(lists)):
        path = path + lists[i]
        if path not in path_list:
            print '  '*i + lists[i]
            path_list.append(path)

对于任何有同样问题的人。。我找到了实现这一点的方法:),这里是display_recursive()的更改代码:

def display_recusively(dictionary, level=0):
    if type(dictionary) is dict: 
        values = []  # get all the values and parse each again
        for key, value in dictionary.iteritems():
            parent = key
            if value != '': # recurse only when value is dict
                print spaces[level], key 
                values.append(value)
                level = level + 1
                display_recusively(values, level) 
                level = level -1 
                values = [] #sanitise the list
            elif value == '':  # this is the last child
                print spaces[level], key , "<>"

    elif type(dictionary) is list: 
        for d in dictionary:
            display_recusively(d, level)
            level = level +1
    else:
        print dictionary

相关问题 更多 >