当前位置: 首页>>代码示例>>Python>>正文


Python Trie.get_prefix方法代码示例

本文整理汇总了Python中trie.Trie.get_prefix方法的典型用法代码示例。如果您正苦于以下问题:Python Trie.get_prefix方法的具体用法?Python Trie.get_prefix怎么用?Python Trie.get_prefix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在trie.Trie的用法示例。


在下文中一共展示了Trie.get_prefix方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestTrie

# 需要导入模块: from trie import Trie [as 别名]
# 或者: from trie.Trie import get_prefix [as 别名]

#.........这里部分代码省略.........
        self.assertRaises(KeyError, self._square_brackets, "Foo")
        self.assertEquals(0, len(self.trie))
        self.assertEquals(0, self.trie.nodeCount())
        self.assertFalse("Foo" in self.trie)

    def test_MixedTypes(self):
        self.trie["Foo"] = True
        self.trie[[1, 2, 3]] = True
        self.assertTrue(self.trie["Foo"])
        self.assertTrue(self.trie[[1, 2, 3]])
        self.assertTrue([1, 2, 3] in self.trie)
        self.assertTrue("Foo" in self.trie)
        del self.trie[[1, 2, 3]]
        self.assertFalse([1, 2, 3] in self.trie)

    def test_Iteration(self):
        self.trie["Foo"] = True
        self.trie["Bar"] = True
        self.trie["Grok"] = True
        for k in self.trie:
            self.assertTrue(k in self.trie)
            self.assertTrue(self.trie[k])

    def test_Addition(self):
        self.trie["Foo"] = True
        t2 = Trie()
        t2["Food"] = True
        t3 = t2 + self.trie
        self.assertTrue("Foo" in self.trie)
        self.assertFalse("Food" in self.trie)
        self.assertTrue("Food" in t2)
        self.assertFalse("Foo" in t2)
        self.assertTrue("Foo" in t3)
        self.assertTrue("Food" in t3)

    def test_Subtraction(self):
        self.trie["Food"] = True
        self.trie["Foo"] = True
        t2 = Trie()
        t2["Food"] = True
        t3 = self.trie - t2
        t4 = t2 - self.trie
        self.assertTrue("Food" in self.trie)
        self.assertTrue("Foo" in self.trie)
        self.assertTrue("Food" in t2)
        self.assertTrue("Foo" in t3)
        self.assertFalse("Food" in t3)
        self.assertFalse("Foo" in t4)
        self.assertFalse("Food" in t4)

    def test_SelfAdd(self):
        self.trie["Foo"] = True
        t2 = Trie()
        t2["Food"] = True
        self.assertTrue("Foo" in self.trie)
        self.assertFalse("Food" in self.trie)
        self.assertTrue("Food" in t2)
        self.assertFalse("Foo" in t2)
        self.trie += t2
        self.assertTrue("Foo" in self.trie)
        self.assertTrue("Food" in self.trie)

    def test_SelfSub(self):
        self.trie["Foo"] = True
        self.trie["Food"] = True
        t2 = Trie()
        t2["Food"] = True
        self.assertTrue("Food" in self.trie)
        self.assertTrue("Foo" in self.trie)
        self.assertTrue("Food" in t2)
        self.trie -= t2
        self.assertFalse("Food" in self.trie)
        self.assertTrue("Foo" in self.trie)
        self.assertTrue("Food" in t2)

    def test_SelfGet(self):
        self.trie["Foo"] = True
        self.assertTrue(self.trie["Foo"])
        self.assertRaises(KeyError, self._square_brackets, "Food")
        self.assertEquals("Bar", self.trie.get("Food", "Bar"))
        self.assertEquals("Bar", self.trie.get("Food", default="Bar"))
        self.assertTrue(self.trie.get("Foo"))
        self.assertTrue(self.trie.get("Food") is None)

    def test_get_prefix(self):
        self.trie["123"] = 1
        self.trie["123456"] = 2
        self.trie["123457"] = 3
        self.trie["1234578"] = 4
        self.assertEquals(None, self.trie.get_prefix("7"))
        self.assertEquals(None, self.trie.get_prefix("1"))
        self.assertEquals(None, self.trie.get_prefix("12"))
        self.assertEquals(1, self.trie.get_prefix("123"))
        self.assertEquals(1, self.trie.get_prefix("1234"))
        self.assertEquals(1, self.trie.get_prefix("12345"))
        self.assertEquals(2, self.trie.get_prefix("123456"))
        self.assertEquals(3, self.trie.get_prefix("123457"))
        self.assertEquals(4, self.trie.get_prefix("1234578"))
        self.assertEquals(4, self.trie.get_prefix("12345789"))
        self.assertEquals(4, self.trie.get_prefix("12345789999"))
开发者ID:olegoga,项目名称:python-trie,代码行数:104,代码来源:trie_unittest.py


注:本文中的trie.Trie.get_prefix方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。