我正在使用此处找到的已回答问题中的代码。
存储婚姻信息的最蟒蛇方式是什么?我希望可以从存储在类中的任何已婚人士那里询问 self.husband 或 self.wife,并递归地从这些人那里询问 self.children。
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 thedictionary storing parents and children. Need 'parent' key to become 'parents' key which stores a list of two people.