Python:将元组列表转换为dict

时间:2018-05-08 07:01:00

标签: python dictionary treeview imap

我收到来自imap服务器的回复,如下所示。其中包含特定用户帐户的所有文件夹。

imap.list_folders()输出:

[((b'\\HasChildren',), b'.', 'welcome'), ((b'\\HasChildren',), b'.', 'welcome.welcomeqqqqq'), ((b'\\HasNoChildren',), b'.', 'welcome.welcomeqqqqq.downunder'), ((b'\\HasNoChildren',), b'.', 'test2'), ((b'\\HasNoChildren',), b'.', 'Arunnnnnnnnn')]

此处welcome是文件夹名称。 而HasChildren表示该文件夹中有一个子文件夹。 welcome.welcomeqqqqqwelcome的孩子。父和子用点分隔。 我想以递归方式从文件夹列表中形成树视图格式。

预期产出:

[
    {
        "folder": "Top",
        "parent": "None",
        "children": [
            {
                "folder": "Folder 1",
                "parent": "Top",
                "children": [
                    {
                        "folder": "Sub Folder 1",
                        "parent": "Folder 1",
                        "children": []
                    },
                    {
                        "folder": "Sub Folder 2",
                        "parent": "Folder 1",
                        "children": [
                            {
                                "folder": "Sub Folder Sub Folder 1",
                                "parent": "Sub Folder 2",
                                "children": []
                            },
                            {
                                "folder": "Sub Folder Sub Folder 2",
                                "parent": "Sub Folder 2",
                                "children": []
                            }
                        ]
                    }
                ]
            },
            {
                "folder": "Folder 2",
                "parent": "Top",
                "children": []
            }
        ]
    }
]

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:4)

您可以使用递归函数执行此操作:

folders = [
            ((b'\\HasChildren',), b'.', 'welcome'), 
            ((b'\\HasChildren',), b'.', 'welcome.welcomeqqqqq'), 
            ((b'\\HasNoChildren',), b'.', 'welcome.welcomeqqqqq.downunder'), 
            ((b'\\HasNoChildren',), b'.', 'test2'), 
            ((b'\\HasNoChildren',), b'.', 'Arunnnnnnnnn')
]

def make_my_list_of_dict(my_list_of_folders, check_parent = None):
    my_list = []
    for e in my_list_of_folders: 
        split_char = e[1].decode()
        parent = e[2].split(split_char)[-2] if split_char in e[2] else None
        folder = e[2].split(split_char)[-1]
        if parent == check_parent:
            if e[0][0] == b'\\HasChildren':
                children = make_my_list_of_dict(my_list_of_folders, folder)
                my_list.append({'folder':folder, 'parent':parent, 'children':children})
            else:
                my_list.append({'folder':folder, 'parent':parent, 'children':[]})
    return my_list

result = make_my_list_of_dict(folders)
print(result)

输出:

[
    {'folder': 'welcome', 
     'parent': None, 
     'children': [
                     {'folder': 'welcomeqqqqq', 
                      'parent': 'welcome', 
                      'children': [
                                      {'folder': 'downunder', 
                                       'parent': 'welcomeqqqqq', 
                                       'children': []
                                      }
                                  ]
                     }
                 ]
    }, 

    {'folder': 'test2', 
     'parent': None, 
     'children': []
    }, 

    {'folder': 'Arunnnnnnnnn', 
     'parent': None, 
     'children': []
    }
]