- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在学习 python 时创建了一个 trie 树,这是 trie 输出
{'a': {'b': {'c': {'_': '_'}}}, 'b': {'a': {'x': {'_': '_'}, 'r': {'_': '_', 'z': {'_': '_'}}, 'z': {'_': '_'}}}, 'h': {'e': {'l': {'l': {'o': {'_': '_'}}}}}}
我无法从 trie 中列出所有单词,我显然不理解一些简单的东西,下面是我创建 trie 并添加到 trie 以及检查 trie 中是否存在单词的代码.方法列表是我列出单词的糟糕尝试,目前它只获取每个单词的第一个字母。任何建议都会很棒。
# Make My trie
def make_trie(*args):
"""
Make a trie by given words.
"""
trie = {}
for word in args:
if type(word) != str:
raise TypeError("Trie only works on str!")
temp_trie = trie
for letter in word:
temp_trie = temp_trie.setdefault(letter, {})
temp_trie = temp_trie.setdefault('_', '_')
return trie
# Is a word in the trie
def in_trie(trie, word):
"""
Detect if word in trie.
:param word:
:param trie:
"""
if type(word) != str:
raise TypeError("Trie only works on str!")
temp_trie = trie
for letter in word:
if letter not in temp_trie:
return False
temp_trie = temp_trie[letter]
return True
# add to the trie
def add(trie, *args):
for word in args:
if type(word) != str:
raise TypeError("Trie only works on str!")
temp_trie = trie
for letter in word:
temp_trie = temp_trie.setdefault(letter, {})
temp_trie = temp_trie.setdefault('_', '_')
return trie
# My Attempt to list out words
def list(obj, text, words):
str = ""
temp_trie = obj
for index, word in enumerate(temp_trie):
print(temp_trie[word])
if __name__ == '__main__':
trie = make_trie('hello', 'abc', 'baz', 'bar', 'barz')
# print(trie)
# get_file()
words = []
# list(trie, "", words)
print(in_trie(trie, 'bar'))
print(in_trie(trie, 'bab'))
print(in_trie(trie, 'zzz'))
add(trie, "bax")
print(in_trie(trie, 'bax'))
print(in_trie(trie, 'baz'))
print(trie)
list(trie, "", 'hello')
我想要的预期输出是 trie 中存在的单词列表像这样
content = ['你好', 'abc', 'baz', 'bar', 'barz']
最佳答案
你应该写一个搜索树的递归函数
def list_words(trie):
my_list = []
for k,v in trie.items():
if k != '_':
for el in list_words(v):
my_list.append(k+el)
else:
my_list.append('')
return my_list
示例输出
>>> trie = {'a': {'b': {'c': {'_': '_'}}}, 'b': {'a': {'x': {'_': '_'}, 'r': {'_': '_', 'z': {'_': '_'}}, 'z': {'_': '_'}}}, 'h': {'e': {'l': {'l': {'o': {'_': '_'}}}}}}
>>> print(list_words(trie))
['abc', 'hello', 'bax', 'barz', 'bar', 'baz']
关于Python 特里树 : how to traverse it to build list of all words?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36977439/
我想知道是否可以构造以下函数 ix :: (Applicative a, Traversable t) => Int -> (v -> a v) -> (t v -> a (t v)) 对除第i个元素
精简版。 Scala 中的大多数泛型集合都有一个 map实际上,该方法将返回相同类型的集合。 (例如, List[A].map(f:A=>B) 返回一个 List[B] 。)Scala 集合库是明确设
早上好/下午好/晚上好。 因此,我们的数据结构类(class)给了我们一个作业,使用以下算法在 java 中分割灰度图像: Input: A gray-scale image with P pixel
我一直在想为什么 Traversable 类型类需要 Functor 和 Foldable,而不仅仅是 Foldable ,因为它不使用仿函数的任何部分? class (Functor t, Fold
JavaScript 和 CSS 在遍历 HTML 元素时都使用自己的 DOM 树。 在JavaScript中,我们可以使用它自己的DOM遍历方法如 element.parentNode elemen
如何在nodejs遍历模块中中断/停止遍历? 我正在遍历 json,在某些情况下,我想停止遍历。 遍历(req.body).forEach(函数(xnode){ log.info("检查 Node :
自然法规定: t . traverse f == traverse (t . f) -- for every applicative transformer t 现在对于法律的 RHS,如果 f 的类
我想在 Java8 节点流中转换树。 这是一个存储数据的节点树,可以选择: public class SelectTree { private D data; private boolean
有人可以向我解释一下,typeclass Traversable 的目的是什么? ? 类型类定义是: class (Functor t, Foldable t) => Traversable (t :
我正在研究Scala 2.8集合类的源代码。我对 scala.collection.Traversable 的层次结构有疑问。查看以下声明: package scala.collection
在我的 ArangoDB 图中,我有一个主题、与该主题关联的消息线程以及这些消息线程内的消息。我想以返回与消息线程关联的数据以及消息线程内的消息计数的方式遍历该图。 数据的结构相当简单:我有主题节点、
问题 1 - 在Scala文档中,我发现Traversable是一个带有抽象方法foreach的trait: http://www.scala-lang.org/docu/files/collecti
在 https://babeljs.io/当我想编译我的 ES6 代码时,将显示此错误: traverse.clearCache is not a function 我的代码是一个简单的代码,例如:
我有以下实现目录遍历的模块: module Walk ( walk ) where import Control.Monad import Contro
我试图弄清楚如何编写一个适用于任何 Traversable[_] 的函数式交换函数,给定一个集合和要交换的索引。我想出了以下内容: def swap[A, CC List(5,2,3,4,1) 我想
假设我们有一些类 Foo这样 Foo f 的实例为我们提供实现 Functor f 所需的一切, Foldable f和 Traversable f .为避免重复实例,可以见证 Foo 之间的这种关系
我记得在某处读到这样的类型不能是 Traversable : data Bar a = Bar a deriving(Show) instance Functor Bar where fmap f
我有一个玫瑰树结构,我想写一个 Traversable 例如。所以我从以下几点开始: data Tree a = Tree a [Tree a] deriving (Show) instance Fu
我正在努力解决如何使用 jQuery 选择特定元素。此处概述的条件: 在我的函数中的相关点,this 引用了一个span。 该 span 可能是子级、孙级或曾孙级,但在层次结构中的某个位置,它由 td
lens报价 holesOf ,这是这个假设函数的一个更通用和更强大的版本: holesList :: Traversable t => t a -> [(a, a -> t a)
我是一名优秀的程序员,十分优秀!