{"id":2698,"date":"2022-03-04T15:22:24","date_gmt":"2022-03-04T07:22:24","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2698"},"modified":"2022-03-13T14:26:03","modified_gmt":"2022-03-13T06:26:03","slug":"pythontree","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/03\/04\/pythontree\/","title":{"rendered":"python \u5b57\u5178\u6811\u3001\u524d\u7f00\u5339\u914d"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" width=\"1024\" height=\"695\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-1024x695.png\" alt=\"\" class=\"wp-image-2700\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-1024x695.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-300x203.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image-768x521.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/03\/image.png 1122w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>\u5728python\u4e2d\u53ef\u4ee5\u7528\u5b57\u5178\u6765\u5b9e\u73b0\u5b57\u5178\u6811\u8fd9\u4e00\u7ed3\u6784\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Trie:\n\n    def __init__(self):\n        \"\"\"\n        Initialize your data structure here.\n        \"\"\"\n        self.root = {}\n        self.isEnd = '#'\n\n    def insert(self, word: str) -&gt; None:\n        \"\"\"\n        Inserts a word into the trie.\n        \"\"\"\n        node = self.root\n        for char in word:\n            if char not in node:\n                node&#91;char] = {}  #\u5982\u679c\u6ca1\u6709\u8fd9\u4e00\u5b57\u6bcd\u5219\u65b0\u5efa\u4e00\u4e2a\u5b57\u5178\n            node = node&#91;char]\n        node&#91;self.isEnd] = '#'   #\u52a0\u4e0a\u7ed3\u675f\u7b26\n\n    def search(self, word: str) -&gt; bool:\n        \"\"\"\n        Returns if the word is in the trie.\n        \"\"\"\n        node = self.root\n        for char in word:\n            if char not in node:\n                return False\n            node = node&#91;char]\n        return self.isEnd in node  #\u5982\u679c\u6ca1\u6709# \u8bf4\u660e\u53ea\u662f\u524d\u7f00\n\n\n    def startsWith(self, prefix: str) -&gt; bool:\n        \"\"\"\n        Returns if there is any word in the trie that starts with the given prefix.\n        \"\"\"\n        node = self.root\n        for char in prefix:\n            if char not in node:\n                return False\n            node = node&#91;char]\n        return True\n\n\n\n# Your Trie object will be instantiated and called as such:\n# obj = Trie()\n# obj.insert(word)\n# param_2 = obj.search(word)\n# param_3 = obj.startsWith(prefix)<\/code><\/pre>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><strong>2.\u91c7\u7528defaultdict\u521b\u5efatrie<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\nfrom collections import defaultdict\nfrom functools import reduce\nTrieNode = lambda: defaultdict(TrieNode)\nclass Trie:\n    def __init__(self):\n        self.trie = TrieNode()\n    def insert(self, word):\n        reduce(dict.__getitem__, word, self.trie)&#91;'end'] = True\n    def search(self, word):\n        return reduce(lambda d,k: d&#91;k] if k in d else TrieNode(), word, self.trie).get('end', False)\n    def startsWith(self, word):\n        return bool(reduce(lambda d,k: d&#91;k] if k in d else TrieNode(), word, self.trie).keys())<\/code><\/pre>\n\n\n\n<p>collections\uff0c\u89e3\u91ca\u662f\u6570\u636e\u7c7b\u578b\u5bb9\u5668\u6a21\u5757\u3002\u8fd9\u91cc\u9762\u6709\u4e00\u4e2acollections.defaultdict()\u7ecf\u5e38\u88ab\u7528\u5230<\/p>\n\n\n\n<p>\u8fd9\u91cc\u7684<em>defaultdict(function_factory)<\/em>\u6784\u5efa\u7684\u662f\u4e00\u4e2a\u7c7b\u4f3c<em>dictionary<\/em>\u7684\u5bf9\u8c61\uff0c\u5176\u4e2d<em>keys<\/em>\u7684\u503c\uff0c\u81ea\u884c\u786e\u5b9a\u8d4b\u503c\uff0c\u4f46\u662f<em>values<\/em>\u7684\u7c7b\u578b\uff0c\u662f<em>function_factory<\/em>\u7684\u7c7b\u5b9e\u4f8b\uff0c\u800c\u4e14\u5177\u6709\u9ed8\u8ba4\u503c\u3002\u6bd4\u5982<em>default(int)<\/em>\u5219\u521b\u5efa\u4e00\u4e2a\u7c7b\u4f3c<em>dictionary<\/em>\u5bf9\u8c61\uff0c\u91cc\u9762\u4efb\u4f55\u7684<em>values<\/em>\u90fd\u662f<em>int<\/em>\u7684\u5b9e\u4f8b\uff0c\u800c\u4e14\u5c31\u7b97\u662f\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684<em>key<\/em>,&nbsp;<em>d[key]<\/em>&nbsp;\u4e5f\u6709\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u8fd9\u4e2a\u9ed8\u8ba4\u503c\u662f<em>int()<\/em>\u7684\u9ed8\u8ba4\u503c0.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728python\u4e2d\u53ef\u4ee5\u7528\u5b57\u5178\u6765\u5b9e\u73b0\u5b57\u5178\u6811\u8fd9\u4e00\u7ed3\u6784\u3002 2.\u91c7\u7528defaultdict\u521b\u5efatrie collecti &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/03\/04\/pythontree\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">python \u5b57\u5178\u6811\u3001\u524d\u7f00\u5339\u914d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,8],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2698"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2698"}],"version-history":[{"count":7,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2698\/revisions"}],"predecessor-version":[{"id":2925,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2698\/revisions\/2925"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2698"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}