1

我是新来的,对 python 很陌生!

我们有一个作业,我已经能够完成其余的工作,但还有一个问题:如果我有这样的树层次结构:

root = [
    parent1 = [
        child1,
        child2 = [
            sub_child
        ]
        child3
    ],
    parent2 = [
        child1,
        child2
    ]
]

而且它们都是一个名为的类的实例TreeHierarchyClass,并且它们都有一个名称属性,我怎样才能找到我输入的名称呢?

我尝试使用 for 循环,但无法知道我需要多少?取名字很简单:

name = input("Enter name: ")
if name == TreeHierarchyObject.name:
    print("Found it!")

但是我如何循环遍历对象?

4

2 回答 2

4

你应该在这里使用简单的递归。该方法在一定程度上取决于您的子对象如何附加到父对象。

如果它们在列表中self.children,则此方法有效,我建议这样做。只需在您的类中定义以下方法:

def findObjectByName(self, name):
    if self.name == name:
        return self
    else:
        for child in self.children:
            match = child.findObjectByName(name)
            if match:
                return match

编辑: 要使其适用于任何属性,而不仅仅是名称,请getattr()改用:

def findObject(self, attr, value):
    if getattr(self, attr) == value:
        return self
    else:
        for child in self.children:
            match = child.findObject(attr, value)
            if match:
                return match

只需调用root.findObjectByName("Sub Child!")或使用第二种方法:root.findObject("name", "Sub Child!")

于 2012-12-04T20:23:03.780 回答
2

您可以使用recursion,也可以使用iteration. 无论哪种方式都无所谓。但是您需要一种搜索​​树的策略。

以下是一些通过图表的策略:

主要思想是不要两次通过同一个节点/叶子,这对于树来说是微不足道的,但coloring对于图来说却需要:

您可以使用一些设计模式,例如visitor模式,您可以添加一个方法.visit()TreeHierarchyClass访问其子节点,并添加一个方法来按名称查找节点。

例子:

# imagine we got this class
class TreeHierarchyClass(object):
    def __init__(self, value):
        self.children = []
        self.value = value
        if self.value == 13:
            self.name = 'the lucky one.'
    def add(self, value):
        self.children.append(type(self)(value))

您可以通过以下方式访问所有节点:

def visit(tree):
    visited = set()
    nonvisited = set()
    nonvisited.update(tree.children)
    while nonvisited:
        item = nonvisited.pop()
        # already seen
        if item in visited:
            continue
        # mark item
        visited.add(item)
        yield item
        # add children
        nonvisited.update(item.children)

让我们构建一个示例树结构:

root = TreeHierarchyClass(0)

for i in range(10):
    root.add(i)

for i in range(10):
    root.children[1].add(i + 10)

现在让我们找到一些项目:

def find(name):
    for item in visit(root):
        print 'checking item with value %d' % item.value,
        if getattr(item, 'name', None) == name:
            print '- found it.'
            break
        else:
            print '- nope, keep searching.'
    else:
        print 'Sorry, not found.'

find('the lucky one.')
find('the lost one.')

这个例子将打印:

>>> find('the lucky one.')
checking item with value 7 - nope, keep searching.
checking item with value 0 - nope, keep searching.
checking item with value 1 - nope, keep searching.
checking item with value 12 - nope, keep searching.
checking item with value 2 - nope, keep searching.
checking item with value 9 - nope, keep searching.
checking item with value 19 - nope, keep searching.
checking item with value 3 - nope, keep searching.
checking item with value 11 - nope, keep searching.
checking item with value 4 - nope, keep searching.
checking item with value 14 - nope, keep searching.
checking item with value 5 - nope, keep searching.
checking item with value 6 - nope, keep searching.
checking item with value 15 - nope, keep searching.
checking item with value 8 - nope, keep searching.
checking item with value 16 - nope, keep searching.
checking item with value 13 - found it.
>>> find('the lost one.')
checking item with value 7 - nope, keep searching.
checking item with value 0 - nope, keep searching.
checking item with value 1 - nope, keep searching.
checking item with value 12 - nope, keep searching.
checking item with value 2 - nope, keep searching.
checking item with value 9 - nope, keep searching.
checking item with value 19 - nope, keep searching.
checking item with value 3 - nope, keep searching.
checking item with value 11 - nope, keep searching.
checking item with value 4 - nope, keep searching.
checking item with value 14 - nope, keep searching.
checking item with value 5 - nope, keep searching.
checking item with value 6 - nope, keep searching.
checking item with value 15 - nope, keep searching.
checking item with value 8 - nope, keep searching.
checking item with value 16 - nope, keep searching.
checking item with value 13 - nope, keep searching.
checking item with value 17 - nope, keep searching.
checking item with value 10 - nope, keep searching.
checking item with value 18 - nope, keep searching.
Sorry, not found.
于 2012-12-04T20:53:35.017 回答