如何直接在python中查找多级dict的元素

时间:2018-08-02 02:56:20

标签: python dictionary

class Trie:
    root = {}
    END = '/'
    def add(self, wordtuple):
        node = self.root
        for c in wordtuple[0]:
            node = node.setdefault(c,{})
        node[self.END] = (wordtuple[1])

    def find_equal(self, word):
        node = self.root
        for c in word:
            if c not in node:
                return None           
            node = node[c]

        return node['/']

我有一棵特里树,并构造了一个嵌套字典,例如root = {'a':{'b':{'m':{'/':(4),'f':{'/':(5)}}}}},并且我有一个前缀字符串abm,但是我想让所有以abm开头的字符串都像字符串abmf,我该怎么办?

0 个答案:

没有答案