计算树中具有相同名称的节点数

时间:2015-11-20 18:50:10

标签: python python-2.7

我尝试计算树中具有相同名称的节点数,但有困难。这就是我尝试过的:

musics = {'genre':'music', 'children':[{'genre':'Pop', 'children':[{'genre':'Eurobeat','children':[]},
                                                           {'genre':'Austropop','children':[]},
                                                           {'genre':'hard rock', 'children':[]}]},
                               {'genre':'Latin', 'children':[{'genre':'Eurobeat', 'children':[{'genre':'Chicha', 'children':[{'genre':'Eurobeat','children':[]}]}]},
                                                           {'genre':'Bachata', 'children':[]}, {'genre':'Criolla', 'children':[]}]}]}


class MUsicNode(object):
    def __init__(self, genre):
        self.genre = genre
        self.children = []

    def add(self,x):
        self.children.append(x)

    def count_name(self,genre):
        name_count = 0
        for node in self.children:
            if node.genre == genre:
                print "same genre"
                print "word = ", genre
                name_count+=1
            node.count_name(genre)
        return name_count



def create_tree(musics):
    for key,value in musics.items():
        if key == 'genre':
            node = value  
            var = MUsicNode(node)
        if key == 'children':
            kid = value

    for n in kid:
        var.add(create_tree(n))
    return var


Tree = create_tree(musics)

print Tree.count_name('Eurobeat'), "<---COUNT FOR NAME 'Eurobeat' "

&#34;欧陆节拍&#34;必须是3,但我的输出是:

same genre

word =  Eurobeat

same genre

word =  Eurobeat

same genre

word =  Eurobeat

0 <---COUNT FOR NAME 'Eurobeat' 

2 个答案:

答案 0 :(得分:1)

name_count更改为函数参数,这样您就可以设置默认值0并传入递归调用的当前计数:

def count_name(self,genre,name_count = 0):

    for node in self.children:
        if node.genre == genre:
            print ("same genre") #you probably know this but take this
            print ("word = ", genre) #and this line out to only print `3`
            name_count+=1
        name_count = node.count_name(genre,name_count)
    return name_count

这将产生:

>>> musics = {'genre':'music', 'children':[{'genre':'Pop', 'children':[{'genre':'Eurobeat','children':[]},
                                                           {'genre':'Austropop','children':[]},
                                                           {'genre':'hard rock', 'children':[]}]},
                               {'genre':'Latin', 'children':[{'genre':'Eurobeat', 'children':[{'genre':'Chicha', 'children':[{'genre':'Eurobeat','children':[]}]}]},
                                                           {'genre':'Bachata', 'children':[]}, {'genre':'Criolla', 'children':[]}]}]}

>>> tree = create_tree(musics)
>>> tree.count_name('Eurobeat')
same genre
word =  Eurobeat
same genre
word =  Eurobeat
same genre
word =  Eurobeat
3

答案 1 :(得分:1)

你忘记了递归步骤。或者更好的是,你拥有它(node.count_name(genre)),但你没有使用它的结果,所以就像你根本不这样做!

你应该“捕获”递归调用的结果并使用它来产生“一般”结果。我不会告诉你如何不破坏乐趣,但这很容易。

或许,您可能没有忘记它,但您认为递归调用可以查看并操作父调用中定义的name_count变量,因此基本上是static variable。在python中,没有直接等价的静态变量,例如,C。每个局部变量都是automatic,因此在每次调用时都会重置,即使调用来自同一个函数。