Trie树(oversimplified python version)
来源:互联网 发布:淘宝网上卖什么赚钱 编辑:程序博客网 时间:2024/06/09 21:24
为了快速地对字符串进行匹配,trie树能够担当此任。以下是用pyhton写的一个简单的例子,凑活能用。
#!/usr/bin/env pythonimport sys, pickle, reclass TrieNode(object): def __init__(self): self.value = None self.children = {}class Trie(object): def __init__(self): self.root = TrieNode() def add(self, key): node = self.root for char in key: if char not in node.children: child = TrieNode() node.children[char] = child node = child else: node = node.children[char] node.value = key def search(self, key): '''return all partially matched strings with the input key''' node = self.root matches = [] for char in key: if char not in node.children: break node = node.children[char] if node.value: matches.append(node.value) return matchesdef gen_trie(input_file, output_file): trie = Trie() with open(input_file) as f: for line in f: line = line.strip() trie.add(line) with open(output_file, 'wb') as f: pickle.dump(trie, f)if __name__ == '__main__': gen_trie('your_key_list', 'output_trie_file')
- Trie树(oversimplified python version)
- Trie树(oversimplified python version)
- Trie树python实现
- 飘逸的python - 实现trie树
- hihoCoder 1014 Trie树【python版】
- hihoCoder1014 Trie树 [Trie]
- python version
- TRIE树
- TRIE树
- TRIE树
- trie 树
- Trie树
- Trie树
- Trie树
- Trie树
- trie树
- trie树
- Trie 树
- 查RPM包的网站
- android JNI
- 一个实现恢复删除机制(do undo)的设计
- java 中的final解析
- LINUX驱动分析之RTC(二)
- Trie树(oversimplified python version)
- 静态成员函数
- LINUX驱动分析之RTC(三)
- PHP获取星期的方法及代码
- 关于iOS应用设计的一些最佳实践
- hdu 4347 【KD-TREE】
- LINUX驱动分析之RTC(四)
- hdu 1789
- jsp介绍