首页 > 解决方案 > Create a list from a dictioanary

问题描述

I have a dictionary

{'about': {'advertise.html': True, 'staff.html': True, 'vacancy.html': True},
 'articles': {'2017': {'12': {'19': {'900588.html': True}}}},
 'columns': {'2016': {'8': {'5': {'825413.html': True}}},
             '2017': {'9': {'8': {'886260.html': True}}}},
 'culture': {'2012': {'8': {'28': {'595498.html': True}}}},
 'economy': {'2013': {'5': {'23': {'633905.html': True}}},
             '2017': {'12': {'22': {'900782.html': True}}},
             '2018': {'7': {'27': {'934361.html': True},
                            '28': {"1111111.html"}}}},
 'hournews': True
 }

It is necessary to write down all the paths on the list. In this example, it should be like this:

["about","advertise.html"]
["about","staff.html"]
["about", ,"vacancy.html"]
["articles","2017","12","19","900588.html"]
["columns","2016","8","5","825413.html"]
["columns","2017","9","8","886260.html"]
["culture","2012","8","28","595498.html"]
["hournews"]

How can I do that?

my code:

def get_node(path,tree):
    for name,val in tree.items():
        if type(val) == dict:
            path.append(name)
            get_node(path,val)
            path = path[:-1]
    else:
        print(path)
get_node([],tree)

it returns me something like this

['redir', '?source=vz_hour_news', 'news', '2018', '7', 'economy', '2018', '7', 'politics', '2018', '7', 'society', '2018', '7', 'world', '2018', '7', 'incidents', '2018', '6', 'opinions', '2018', '7', 'video', '2018', '6', 'photo', '2018', '7', 'vote', 'sport', '2018', '7', 'columns', '2017', '9', 'culture', '2012', '8', 'articles', '2017', '12']

but must return

["redir","?source=vz_hour_news","&id=934685","&vzurl=news/2018/7/29/934685.html"]
["redir","?source=vz_index_author", "&id=934134", "'&vzurl=opinions/2018/7/25/934134.html"]

标签: python-3.xlistdictionary

解决方案


Here is a solution using a generator: we explore the dict recursively, building the path while going down. Each time we hit a leaf of the structure, we yield the current path.

d = {'about': {'advertise.html': True, 'staff.html': True, 'vacancy.html': True},
 'articles': {'2017': {'12': {'19': {'900588.html': True}}}},
 'columns': {'2016': {'8': {'5': {'825413.html': True}}},
             '2017': {'9': {'8': {'886260.html': True}}}},
 'culture': {'2012': {'8': {'28': {'595498.html': True}}}},
 'economy': {'2013': {'5': {'23': {'633905.html': True}}},
             '2017': {'12': {'22': {'900782.html': True}}},
             '2018': {'7': {'27': {'934361.html': True},
                            '28': {"1111111.html":True}}}},
 'hournews': True
 }



def paths(d, current_path=None):
    if current_path is None:
        current_path = []

    if isinstance(d, dict):
        for key, value in d.items():
            yield from paths(value, current_path + [key])
    else:
        yield current_path

print(list(paths(d)))

#[['about', 'advertise.html'],
# ['about', 'staff.html'],
# ['about', 'vacancy.html'],
# ['articles', '2017', '12', '19', '900588.html'],
# ['columns', '2016', '8', '5', '825413.html'],
# ['columns', '2017', '9', '8', '886260.html'],
# ['culture', '2012', '8', '28', '595498.html'],
# ['economy', '2013', '5', '23', '633905.html'],
# ['economy', '2017', '12', '22', '900782.html'],
# ['economy', '2018', '7', '27', '934361.html'],
# ['economy', '2018', '7', '28', '1111111.html'],
# ['hournews']]

推荐阅读