我有一个问题,我必须创建一个函数,从任意数量的元素的字典创建一个树。 词典示例:
D1 = {'name': 'musica',
'children': [
{'name': 'rock',
'children': [
{'name': 'origini', 'children': []},
{'name': 'rock&roll', 'children': []},
{'name': 'hard rock', 'children': []}]},
{'name': 'jazz',
'children': [
{'name': 'origini',
'children': [{'name': '1900',
'children': [{'name': 'origini', 'children': []}]}]},
{'name': 'ragtime', 'children': []},
{'name': 'swing', 'children': []}]}]}
D2 = {'name': 'html',
'children': [
{'name': 'head',
'children': [
{'name': 'meta', 'children': []},
{'name': 'title', 'children': []},
{'name': 'style', 'children': []}]},
{'name': 'body',
'children': [
{'name': 'h1', 'children': []},
{'name': 'section',
'children': [
{'name': 'p',
'children': [
{'name': 'strong', 'children': []},
{'name': 'b', 'children': []},
{'name': 'em', 'children': []},
{'name': 'i', 'children': []}]},
{'name': 'p',
'children': [
{'name': 'q', 'children': []},
{'name': 'code', 'children': []},
{'name': 'kbd', 'children': []}]},
{'name': 'p',
'children': [
{'name': 'sup', 'children': []},
{'name': 'sub', 'children': []}]},
{'name': 'p',
'children': [
{'name': 'span', 'children': []}]}]},
{'name': 'footer',
'children': [
{'name': 'a',
'children': [
{'name': 'strong', 'children': []}]},
{'name': 'a',
'children': [
{'name': 'strong', 'children': []}]}]}]}]}
D3 = {'name': 'Giovanni di Bicci',
'children': [
{'name': 'Cosimo il vecchio',
'children': [
{'name': 'Piero il gottuso',
'children': [
{'name': 'Lorenzo il magnifico',
'children': [
{'name': 'Piero II',
'children': [
{'name': 'Lorenzo II', 'children': []}]},
{'name': 'Papa Leone X', 'children': []},
{'name': 'Giuliano', 'children': []}]}]},
{'name': 'Giovanni Carlo', 'children': []}]},
{'name': 'Lorenzo',
'children': [
{'name': 'Pierfrancesco',
'children': [
{'name': 'Lorenzo', 'children': []},
{'name': 'Giovanni',
'children': [
{'name': 'Giovanni dalle Bande Nere',
'children': [
{'name': 'Lorenzino', 'children': []},
{'name': 'Cosimo I',
'children': [
{'name': 'Francesco I',
'children': [
{'name': 'Maria', 'children': []}]},
{'name': 'Ferdinando I',
'children': {}}]}]}]}]}]}]}
任何解决方案?
非常感谢
谢谢大家的答案。
现在我写完整个练习,这样你就可以更好地理解和回答我:实际上我已经用这些方法实现了一个TNode类:
class TNode(object):
def __init__(self, name, Sinistra= None, Destra= None):
self._name = name
self.Destra = Destra
self.Sinistra = Sinistra
self._children = []
self._copy = []
self.c = c
def add(self, c):
self._children.append(c)
def children(self):
self._copy = self._children
return self._copy
def height(self):
h = 1
for node in self._children:
h = max(h, node.height() + 1)
return h
def count(self):
c = 1
for node in self._children:
c += node.count()
return c
def count_by_name(self, name):
lst = []
if self._name == name:
lst += [self]
for node in self._children:
lst += node.count_by_name(name)
return lst
def leaves(self):
leaves_s = []
if not self._children:
leaves_s.append(self.c)
for node in self._children:
leaves_s.update(node.leaves())
return len(leaves_s)
def paths(self, name):
paths_s = set()
if self._name == name:
paths_s.add((name,))
for node in self._children:
for j in node.paths(name):
paths_s.add((self._name,)+j)
return paths_s
我还需要创建一个函数create_tree(d),它取一个字典" d"表示树,使用TNode类型的节点创建相应的树并返回根。该功能必须按照键列表中列出的相同顺序添加子项。' children'。
很抱歉,如果我最初没有写完这一切。
我无法创建引用该类的函数,该函数从字典中创建树。
我使用Python 2.7
感谢。
答案 0 :(得分:0)
您可以使用defaultdict
:
from collections import defaultdict
def Tree():
return defaultdict(Tree)
然后使用它:
>>> tree = Tree()
>>> tree['house']['car']['red']['hubcap'] = 1950