我有一個問題,我必須創建一個函數,從任何數量的元素的字典中創建一棵樹。 例句詞典:Python - 從字典創建樹
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類型的節點相對應的樹並返回根。該功能必須按照鍵「兒童」列表中列出的順序添加子項。
對不起,如果最初我沒有寫所有的。
我無法創建從字典中創建樹的函數(稱爲類)。
我使用Python 2.7
謝謝。
你嘗試過什麼碼? –
請嘗試更多地格式化您的代碼。 – miku
謝謝,我通過添加應該告訴函數create_tree(d)的類來編輯帖子。 – santejo