Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在字典中查找集合的最大深度_Python_Dictionary_Set_Max_Depth First Search - Fatal编程技术网

Python 在字典中查找集合的最大深度

Python 在字典中查找集合的最大深度,python,dictionary,set,max,depth-first-search,Python,Dictionary,Set,Max,Depth First Search,我有一个字典,其中键是一个字符串,键的值是一组也包含键的字符串(单词链接)。我很难找到一个图的最大深度,这将是字典中元素最多的集合,我也尝试打印出这个最大深度图 现在我的代码打印: {'DOG': [], 'HIPPOPOTIMUS': [], 'POT': ['SUPERPOT', 'HIPPOPOTIMUS'], 'SUPERPOT': []} 1 其中1是我的最大字典深度。我原以为深度是两层,但“POT”的图形似乎只有一层 如何从字典中的键集中找到最大值集 import pprin

我有一个字典,其中键是一个字符串,键的值是一组也包含键的字符串(单词链接)。我很难找到一个图的最大深度,这将是字典中元素最多的集合,我也尝试打印出这个最大深度图

现在我的代码打印:

{'DOG': [],
 'HIPPOPOTIMUS': [],
 'POT': ['SUPERPOT', 'HIPPOPOTIMUS'],
 'SUPERPOT': []}
1
其中1是我的最大字典深度。我原以为深度是两层,但“POT”的图形似乎只有一层

如何从字典中的键集中找到最大值集

import pprint

def dict_depth(d, depth=0):
    if not isinstance(d, dict) or not d:
        return depth
    print max(dict_depth(v, depth+1) for k, v in d.iteritems())


def main():
    for keyCheck in wordDict:
        for keyCompare in wordDict:
            if keyCheck in keyCompare:
                if keyCheck != keyCompare:
                    wordDict[keyCheck].append(keyCompare)

if __name__ == "__main__":
    #load the words into a dictionary
    wordDict = dict((x.strip(), []) for x in open("testwordlist.txt"))
    main()
    pprint.pprint (wordDict)
    dict_depth(wordDict)
testwordlist.txt:

POT
SUPERPOT
HIPPOPOTIMUS
DOG

很抱歉,我的示例不会用python编写,因为我的python已经生锈了,但您应该了解这一点

假设这是一个二叉树:
(用c++编写)

简单。现在,让我们将其展开得更多,而不仅仅是左和右。
(戈朗代码)

其思想是,您只需查看每一个字典,直到没有更多的字典可查看为止。在您遍历的每个级别上添加1。

字典的“深度”自然是1加上其条目的最大深度。您已将非字典的深度定义为零。由于顶级词典不包含任何自己的词典,因此词典的深度显然是1。您的函数正确地报告该值

但是,编写函数时并不希望使用您提供的数据格式。我们可以很容易地得出子串链的深度不止一个的输入。例如:

DOG DOGMA DOGMATIC DOGHOUSE POT
这里给出的
word\u chain\u length
函数不是特别有效。如果一个字符串是多个单词的子字符串,它可能会多次计算同一链的长度。动态规划是一种简单的方法来缓解这种情况,我将把它留作练习。

非常感谢,对于链长度和字典中“图”的深度之间的差异,我仍然有点困惑。字典的最大深度是1,因为没有嵌套键,但是最大链不应该是4吗?狗->教条->教条->狗屋
func depthfunc(Dic dic) (int){
   if dic == nil {
     return 0
   }
   level := make([]int,0)
   for key, anotherDic := range dic{
      depth := 1
      if ok := anotherDic.(Dic); ok { // check if it does down further
        depth = 1 + depthfunc(anotherDic)
      }
        level = append(level, depth)          
   }

   //find max
   max := 0
   for _, value := range level{
     if value > max {
       max = value
     }
   }
   return max
}
DOG DOGMA DOGMATIC DOGHOUSE POT {'DOG': ['DOGMATIC', 'DOGMA', 'DOGHOUSE'], 'DOGHOUSE': [], 'DOGMA': ['DOGMATIC'], 'DOGMATIC': [], 'POT': []} 1
def word_chain_length(d, w):
    if len(d[w]) == 0:
        return 0
    return 1 + max(word_chain_length(d, ww) for ww in d[w])

def dict_depth(d):
    print(max(word_chain_length(d, w) for w in d))