python中列表的类别树实现

时间:2014-10-20 13:48:49

标签: python tree treeview categories

我试图在Python中实现具有无限深度子类别的类别树,我有多个列表元素,我必须从中进行此操作。

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

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

我希望输出为:

>home
>   desktop
>        mouse
>            wireless
>            wired
>   laptop
>       mouse
>       13-inch

我明白我应该使用递归函数迭代列表并制作一些神奇的东西。

为实现这一目标,我将分两步完成此任务: 1.将此嵌套列表转换为嵌套字典(仅用于保持层次结构) 2.将嵌套的dict转换为上面解释的所需格式。

Step1:这是我将嵌套列表转换为嵌套字典的代码:

>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.elements应携带父母制作锚标签(如上段所述)

2 个答案:

答案 0 :(得分:0)

对于任何有同样问题的人..我想出了实现这个的方法:),这里是display_recursively()的改变代码:

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

答案 1 :(得分:0)

通过这样做可以获得相同的输出 -

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)