如何创建家谱?

时间:2012-12-02 16:17:01

标签: python tree

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

Dictionary的结构如下所示:

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

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

2 个答案:

答案 0 :(得分:2)

这只是检查员G4dget的答案,几乎在那里,但需要一些改动:

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

答案 1 :(得分: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)

希望这有帮助