从字典创建列表

时间:2018-08-01 10:08:20

标签: python-3.x list 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
 }

有必要写下列表中的所有路径。 在此示例中,应如下所示:

["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"]

我该怎么做?

我的代码:

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)

它会给我这样的信息

['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']

但必须返回

["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"]

1 个答案:

答案 0 :(得分:1)

这是一个使用生成器的解决方案:我们递归地探索字典,在下降时构建路径。每次碰到结构的叶子时,我们yield当前路径。

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']]