-1

有家庭成员的字典如何创建它的树?

字典的结构如下所示:

{'parent':'Smith', 'children':[
    {'parent':'Connor', 'children':[
        {'parent':'Alexis','children':[
            {'parent':'Joe', 'children':[
                {'parent':'Clark','children':[]}]}]},
        {'parent':'Sue','children':[]}]},
    {'parent':'Cooper', 'children':[
        {'parent':'Max','children':[
            {'parent':'Luis', 'children':[]},]},
        {'parent':'Elvis', 'children':[]},
        {'parent':'Steven', 'children':[]}]}]}

创建家谱后,我如何检查一些日期,例如:如何检查有多少成员获得了整个家庭或单个家庭的树。

检查整个家谱根或家庭的某些部分有多大。

如何将新成员添加到现有位置或家谱中的新位置?

编辑

添加树的示例:

Smith
    Conor
        Alexis
            Joe
                Clark
        Sue
    Cooper
        Max
            Luis
        Elvis
        Steven

与计算机系统目录的样式相同。

4

2 回答 2

2

这只是inspectorG4dget 的答案,几乎就在那里,但需要一些改动:

class Person:
    ID = itertools.count()
    def __init__(self, name, parent=None, level=0):
        self.id = self.__class__.ID.next() # next(self.__class__.ID) in python 2.6+
        self.parent = parent
        self.name = name
        self.level = level
        self.children = []

def createTree(d, parent=None, level=0):
    if d:
        member = Person(d['parent'], parent, level)
        level = level + 1
        member.children = [createTree(child, member, level) for child in d['children']]
        return member

t = createTree(my_tree)          # my_tree is the name of your dictionary
def printout(parent, indent=0):
    print '\t'*indent, parent.name
    for child in parent.children:
        printout(child, indent+1)        
printout(t)

根据上面的评论,您需要import itertools在程序开始时。

编辑:展平树的功能应该用于您想做的所有其他事情:

def flatten(parent):
    L = [parent]
    for child in parent.children:
        L += flatten(child)
    return L
flattened_tree = flatten(t)
print "All members: ", [person.name for person in flattened_tree]
print "Number of members:", len(flattened_tree)
print "Number of levels:", max([person.level for person in flattened_tree]) + 1
cooper = flattened_tree[6]
cooper_fl = flatten(cooper)
print "Members below Cooper: ", [person.name for person in cooper_fl]
print "Number:", len(cooper_fl)
levels = [person.level for person in cooper_fl]
print "Number of levels:", max(levels) - min(levels) + 1
于 2012-12-02T18:01:14.887 回答
0

未经测试,但这应该可以

class Person:
    ID = itertools.count()
    def __init__(self):
        self.id = next(self.__class__.ID)
        self.parent = None
        self.children = []

def createTree(familyTreeDict, parent=None):
    if not familyTreeDict:
        return []
    else:
        members = []
        for name familyTreeDict:
            members.append(Person(name))
            members[-1].parent = parent
            for child in familyTreeDict[name]:
                members[-1].children.append(createTree(child, members[-1]))
        return members

然后,如果你想打印一个树结构,给定输出createTree

def printout(family, indent=0):
    for parent in family:
        print '\t'*indent, parent.name
        for child in parent.children:
            printout(child, indent+1)

希望这可以帮助

于 2012-12-02T17:08:51.967 回答