{"id":1079352,"date":"2025-01-08T12:20:09","date_gmt":"2025-01-08T04:20:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1079352.html"},"modified":"2025-01-08T12:20:11","modified_gmt":"2025-01-08T04:20:11","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e9%94%ae%e5%80%bc%e5%a2%9e%e5%8a%a0%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1079352.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u952e\u503c\u589e\u52a0\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182423\/55ec2b5c-4dbe-4a32-b955-b68530367bca.webp\" alt=\"python\u5982\u4f55\u7ed9\u952e\u503c\u589e\u52a0\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u7ed9\u952e\u503c\u589e\u52a0\u6570\u636e\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u96c6\u5408\u7c7b\u578b\u3001\u4f7f\u7528<code>defaultdict<\/code>\u3001\u4f7f\u7528<code>setdefault<\/code>\u3002<\/strong> \u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u76f4\u63a5\u8d4b\u503c\u9002\u7528\u4e8e\u6570\u636e\u7c7b\u578b\u662f\u53ef\u53d8\u7c7b\u578b\u7684\u60c5\u51b5\uff0c\u4f7f\u7528<code>defaultdict<\/code>\u5219\u53ef\u4ee5\u907f\u514d\u952e\u4e0d\u5b58\u5728\u65f6\u62a5\u9519\u7684\u60c5\u51b5\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u5c55\u5f00\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5bf9\u952e\u503c\u4e3a\u5217\u8868\u3001\u5b57\u5178\u7b49\u53ef\u53d8\u7c7b\u578b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u5bf9\u5e94\u7684\u503c\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u60f3\u8981\u5f80\u8fd9\u4e2a\u5217\u8868\u4e2d\u6dfb\u52a0\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;key1&#39;: [1, 2, 3],<\/p>\n<p>    &#39;key2&#39;: [4, 5, 6]<\/p>\n<p>}<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>data[&#39;key1&#39;].append(7)<\/p>\n<p>print(data)  # \u8f93\u51fa {&#39;key1&#39;: [1, 2, 3, 7], &#39;key2&#39;: [4, 5, 6]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u76f4\u63a5\u64cd\u4f5c\u5b57\u5178\u952e\u5bf9\u5e94\u7684\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u6dfb\u52a0\u6570\u636e\u3002\u4e0d\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u8981\u6c42\u5b57\u5178\u4e2d\u7684\u503c\u5fc5\u987b\u662f\u53ef\u53d8\u7c7b\u578b\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u96c6\u5408\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u952e\u5bf9\u5e94\u7684\u503c\u662f\u96c6\u5408\uff08<code>set<\/code>\uff09\uff0c\u4f7f\u7528<code>add<\/code>\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u6dfb\u52a0\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u6709\u53bb\u91cd\u7684\u6548\u679c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u8bc1\u552f\u4e00\u6027\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;key1&#39;: {1, 2, 3},<\/p>\n<p>    &#39;key2&#39;: {4, 5, 6}<\/p>\n<p>}<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>data[&#39;key1&#39;].add(7)<\/p>\n<p>print(data)  # \u8f93\u51fa {&#39;key1&#39;: {1, 2, 3, 7}, &#39;key2&#39;: {4, 5, 6}}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u4e0d\u4ec5\u53ef\u4ee5\u65b9\u4fbf\u5730\u6dfb\u52a0\u6570\u636e\uff0c\u8fd8\u80fd\u4fdd\u8bc1\u6570\u636e\u7684\u552f\u4e00\u6027\uff0c\u4e0d\u4f1a\u51fa\u73b0\u91cd\u590d\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>defaultdict<\/code><\/h3>\n<\/p>\n<p><p><code>defaultdict<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u5b83\u53ef\u4ee5\u4e3a\u5b57\u5178\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c<code>defaultdict<\/code>\u4f1a\u81ea\u52a8\u521b\u5efa\u8fd9\u4e2a\u952e\u5e76\u5c06\u5176\u503c\u8bbe\u4e3a\u9ed8\u8ba4\u503c\uff0c\u907f\u514d\u4e86\u952e\u4e0d\u5b58\u5728\u65f6\u62a5\u9519\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>data = defaultdict(list)<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>data[&#39;key1&#39;].append(1)<\/p>\n<p>data[&#39;key1&#39;].append(2)<\/p>\n<p>data[&#39;key2&#39;].append(3)<\/p>\n<p>print(data)  # \u8f93\u51fa defaultdict(&lt;class &#39;list&#39;&gt;, {&#39;key1&#39;: [1, 2], &#39;key2&#39;: [3]})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>defaultdict<\/code>\u9002\u7528\u4e8e\u5b57\u5178\u7684\u503c\u9700\u8981\u9891\u7e41\u521d\u59cb\u5316\u7684\u60c5\u51b5\uff0c\u6bd4\u5982\u5728\u5904\u7406\u5d4c\u5957\u5b57\u5178\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>setdefault<\/code><\/h3>\n<\/p>\n<p><p><code>setdefault<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5728\u5b57\u5178\u4e2d\u952e\u4e0d\u5b58\u5728\u65f6\uff0c\u7ed9\u8fd9\u4e2a\u952e\u8bbe\u5b9a\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u4e0e<code>defaultdict<\/code>\u7c7b\u4f3c\uff0c\u4f46<code>setdefault<\/code>\u662f\u5b57\u5178\u7684\u5185\u7f6e\u65b9\u6cd5\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {}<\/p>\n<h2><strong>\u4f7f\u7528setdefault\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>data.setdefault(&#39;key1&#39;, []).append(1)<\/p>\n<p>data.setdefault(&#39;key1&#39;, []).append(2)<\/p>\n<p>data.setdefault(&#39;key2&#39;, []).append(3)<\/p>\n<p>print(data)  # \u8f93\u51fa {&#39;key1&#39;: [1, 2], &#39;key2&#39;: [3]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>setdefault<\/code>\u65b9\u6cd5\u4e5f\u662f\u7528\u4e8e\u7ed9\u5b57\u5178\u952e\u503c\u8bbe\u5b9a\u9ed8\u8ba4\u503c\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u952e\u4e0d\u5b58\u5728\u65f6\u9700\u8981\u521d\u59cb\u5316\u503c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>update<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>update<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c<code>update<\/code>\u4f1a\u66f4\u65b0\u952e\u5bf9\u5e94\u7684\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c<code>update<\/code>\u4f1a\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;key1&#39;: 1,<\/p>\n<p>    &#39;key2&#39;: 2<\/p>\n<p>}<\/p>\n<h2><strong>\u66f4\u65b0\u6570\u636e<\/strong><\/h2>\n<p>data.update({&#39;key1&#39;: 3, &#39;key3&#39;: 4})<\/p>\n<p>print(data)  # \u8f93\u51fa {&#39;key1&#39;: 3, &#39;key2&#39;: 2, &#39;key3&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>update<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6279\u91cf\u66f4\u65b0\u6216\u6dfb\u52a0\u952e\u503c\u5bf9\u7684\u60c5\u51b5\uff0c\u4f7f\u7528\u7b80\u5355\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>merge<\/code>\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u4e24\u4e2a\u5b57\u5178\u5408\u5e76\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u7684\u89e3\u5305\u64cd\u4f5c\u7b26<code><\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data1 = {&#39;key1&#39;: 1, &#39;key2&#39;: 2}<\/p>\n<p>data2 = {&#39;key2&#39;: 3, &#39;key3&#39;: 4}<\/p>\n<h2><strong>\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\n<p>merged_data = {&lt;strong&gt;data1, &lt;\/strong&gt;data2}<\/p>\n<p>print(merged_data)  # \u8f93\u51fa {&#39;key1&#39;: 1, &#39;key2&#39;: 3, &#39;key3&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u7684\u60c5\u51b5\uff0c\u4f7f\u7528\u89e3\u5305\u64cd\u4f5c\u7b26\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5b57\u5178\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528for\u5faa\u73af\u6279\u91cf\u6dfb\u52a0\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5b57\u5178\u4e2d\u7684\u591a\u4e2a\u952e\u503c\u8fdb\u884c\u64cd\u4f5c\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6279\u91cf\u6dfb\u52a0\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;key1&#39;: [1, 2],<\/p>\n<p>    &#39;key2&#39;: [3, 4]<\/p>\n<p>}<\/p>\n<h2><strong>\u6279\u91cf\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>for key in data:<\/p>\n<p>    data[key].append(5)<\/p>\n<p>print(data)  # \u8f93\u51fa {&#39;key1&#39;: [1, 2, 5], &#39;key2&#39;: [3, 4, 5]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5b57\u5178\u4e2d\u7684\u591a\u4e2a\u952e\u8fdb\u884c\u76f8\u540c\u64cd\u4f5c\u7684\u60c5\u51b5\uff0c\u4f7f\u7528for\u5faa\u73af\u53ef\u4ee5\u65b9\u4fbf\u5730\u6279\u91cf\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u91cd\u7528\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6dfb\u52a0\u6570\u636e\u7684\u64cd\u4f5c\u5c01\u88c5\u6210\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_data(data, key, value):<\/p>\n<p>    if key in data:<\/p>\n<p>        data[key].append(value)<\/p>\n<p>    else:<\/p>\n<p>        data[key] = [value]<\/p>\n<p>data = {<\/p>\n<p>    &#39;key1&#39;: [1, 2],<\/p>\n<p>    &#39;key2&#39;: [3, 4]<\/p>\n<p>}<\/p>\n<h2><strong>\u4f7f\u7528\u5c01\u88c5\u7684\u51fd\u6570\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>add_data(data, &#39;key1&#39;, 5)<\/p>\n<p>add_data(data, &#39;key3&#39;, 6)<\/p>\n<p>print(data)  # \u8f93\u51fa {&#39;key1&#39;: [1, 2, 5], &#39;key2&#39;: [3, 4], &#39;key3&#39;: [6]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u64cd\u4f5c\u5c01\u88c5\u6210\u51fd\u6570\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u8fdb\u884c\u76f8\u540c\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u7c7b\u548c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u5b9a\u4e49\u7c7b\u548c\u65b9\u6cd5\u6765\u7ba1\u7406\u5b57\u5178\u6570\u636e\u7684\u6dfb\u52a0\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class DataManager:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.data = {}<\/p>\n<p>    def add_data(self, key, value):<\/p>\n<p>        if key in self.data:<\/p>\n<p>            self.data[key].append(value)<\/p>\n<p>        else:<\/p>\n<p>            self.data[key] = [value]<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61\u5e76\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>manager = DataManager()<\/p>\n<p>manager.add_data(&#39;key1&#39;, 1)<\/p>\n<p>manager.add_data(&#39;key1&#39;, 2)<\/p>\n<p>manager.add_data(&#39;key2&#39;, 3)<\/p>\n<p>print(manager.data)  # \u8f93\u51fa {&#39;key1&#39;: [1, 2], &#39;key2&#39;: [3]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\u548c\u65b9\u6cd5\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5c01\u88c5\u590d\u6742\u903b\u8f91\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u751f\u6210\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u6765\u52a8\u6001\u5730\u6dfb\u52a0\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def data_generator():<\/p>\n<p>    yield (&#39;key1&#39;, 1)<\/p>\n<p>    yield (&#39;key1&#39;, 2)<\/p>\n<p>    yield (&#39;key2&#39;, 3)<\/p>\n<p>data = {}<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>for key, value in data_generator():<\/p>\n<p>    if key in data:<\/p>\n<p>        data[key].append(value)<\/p>\n<p>    else:<\/p>\n<p>        data[key] = [value]<\/p>\n<p>print(data)  # \u8f93\u51fa {&#39;key1&#39;: [1, 2], &#39;key2&#39;: [3]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u5668\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u751f\u6210\u6570\u636e\u7684\u60c5\u51b5\uff0c\u4f7f\u7528\u65b9\u4fbf\u4e14\u5185\u5b58\u5360\u7528\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u9012\u5f52<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u9012\u5f52\u6765\u5904\u7406\u5b57\u5178\u7684\u5d4c\u5957\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_data_recursive(data, key_path, value):<\/p>\n<p>    key = key_path[0]<\/p>\n<p>    if len(key_path) == 1:<\/p>\n<p>        if key in data:<\/p>\n<p>            data[key].append(value)<\/p>\n<p>        else:<\/p>\n<p>            data[key] = [value]<\/p>\n<p>    else:<\/p>\n<p>        if key not in data:<\/p>\n<p>            data[key] = {}<\/p>\n<p>        add_data_recursive(data[key], key_path[1:], value)<\/p>\n<p>data = {}<\/p>\n<h2><strong>\u4f7f\u7528\u9012\u5f52\u6dfb\u52a0\u5d4c\u5957\u6570\u636e<\/strong><\/h2>\n<p>add_data_recursive(data, [&#39;key1&#39;, &#39;subkey1&#39;], 1)<\/p>\n<p>add_data_recursive(data, [&#39;key1&#39;, &#39;subkey1&#39;], 2)<\/p>\n<p>add_data_recursive(data, [&#39;key2&#39;, &#39;subkey2&#39;], 3)<\/p>\n<p>print(data)  # \u8f93\u51fa {&#39;key1&#39;: {&#39;subkey1&#39;: [1, 2]}, &#39;key2&#39;: {&#39;subkey2&#39;: [3]}}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9012\u5f52\u9002\u7528\u4e8e\u5904\u7406\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u7684\u60c5\u51b5\uff0c\u4f7f\u7528\u9012\u5f52\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u591a\u5c42\u7ea7\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528<code>Ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nMap<\/code><\/h3>\n<\/p>\n<p><p><code>ChainMap<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u5b57\u5178\u7ec4\u5408\u6210\u4e00\u4e2a\u89c6\u56fe\u3002\u53ef\u4ee5\u4f7f\u7528<code>ChainMap<\/code>\u6765\u7ba1\u7406\u591a\u4e2a\u5b57\u5178\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import ChainMap<\/p>\n<p>data1 = {&#39;key1&#39;: 1}<\/p>\n<p>data2 = {&#39;key2&#39;: 2}<\/p>\n<p>combined_data = ChainMap(data1, data2)<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e\u5230\u5176\u4e2d\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>data1[&#39;key3&#39;] = 3<\/p>\n<p>print(combined_data)  # \u8f93\u51fa ChainMap({&#39;key1&#39;: 1, &#39;key3&#39;: 3}, {&#39;key2&#39;: 2})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>ChainMap<\/code>\u9002\u7528\u4e8e\u9700\u8981\u7ec4\u5408\u591a\u4e2a\u5b57\u5178\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u591a\u4e2a\u5b57\u5178\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\uff0c<code>pandas<\/code>\u5e93\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u7528\u6765\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;key1&#39;: [1, 2], &#39;key2&#39;: [3, 4]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>df = df.append({&#39;key1&#39;: 5, &#39;key2&#39;: 6}, ignore_index=True)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u8f93\u51fa<\/strong><\/h2>\n<h2><strong>   key1  key2<\/strong><\/h2>\n<h2><strong>0     1     3<\/strong><\/h2>\n<h2><strong>1     2     4<\/strong><\/h2>\n<h2><strong>2     5     6<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u60c5\u51b5\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528<code>NumPy<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>NumPy<\/code>\u5e93\u9002\u7528\u4e8e\u5904\u7406\u6570\u503c\u578b\u6570\u636e\uff0c\u53ef\u4ee5\u7528\u6765\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u7ec4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([[1, 2], [3, 4]])<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>data = np.append(data, [[5, 6]], axis=0)<\/p>\n<p>print(data)<\/p>\n<h2><strong>\u8f93\u51fa<\/strong><\/h2>\n<h2><strong>[[1 2]<\/strong><\/h2>\n<h2><strong> [3 4]<\/strong><\/h2>\n<h2><strong> [5 6]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>NumPy<\/code>\u5e93\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u5904\u7406\u7684\u60c5\u51b5\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\u6765\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u636e\u3002\u4ee5<code>SQLite<\/code>\u4e3a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u548c\u6e38\u6807<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;:memory:&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>cursor.execute(&#39;CREATE TABLE data (key TEXT, value INTEGER)&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&#39;INSERT INTO data (key, value) VALUES (?, ?)&#39;, (&#39;key1&#39;, 1))<\/p>\n<p>cursor.execute(&#39;INSERT INTO data (key, value) VALUES (?, ?)&#39;, (&#39;key1&#39;, 2))<\/p>\n<p>cursor.execute(&#39;INSERT INTO data (key, value) VALUES (?, ?)&#39;, (&#39;key2&#39;, 3))<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM data&#39;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>print(rows)  # \u8f93\u51fa [(&#39;key1&#39;, 1), (&#39;key1&#39;, 2), (&#39;key2&#39;, 3)]<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u6570\u636e\u5e93\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u4f7f\u7528\u7f13\u5b58<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u9ad8\u9891\u8bbf\u95ee\u7684\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u6765\u63d0\u9ad8\u6570\u636e\u8bbf\u95ee\u7684\u6548\u7387\u3002\u4ee5<code>functools.lru_cache<\/code>\u4e3a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import lru_cache<\/p>\n<p>@lru_cache(maxsize=32)<\/p>\n<p>def get_data(key):<\/p>\n<p>    return key * 2<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>print(get_data(1))  # \u8f93\u51fa 2<\/p>\n<p>print(get_data(2))  # \u8f93\u51fa 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u7f13\u5b58\u9002\u7528\u4e8e\u9ad8\u9891\u8bbf\u95ee\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6570\u636e\u8bbf\u95ee\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u5e76\u53d1\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u6765\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u3002\u4ee5<code>threading<\/code>\u4e3a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>data = {&#39;key1&#39;: []}<\/p>\n<p>def add_data(key, value):<\/p>\n<p>    data[key].append(value)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1 = threading.Thread(target=add_data, args=(&#39;key1&#39;, 1))<\/p>\n<p>thread2 = threading.Thread(target=add_data, args=(&#39;key1&#39;, 2))<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<h2><strong>\u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<\/strong><\/h2>\n<p>thread1.join()<\/p>\n<p>thread2.join()<\/p>\n<p>print(data)  # \u8f93\u51fa {&#39;key1&#39;: [1, 2]}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u9002\u7528\u4e8e\u5e76\u53d1\u5904\u7406\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u5c31\u662fPython\u4e2d\u7ed9\u952e\u503c\u589e\u52a0\u6570\u636e\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e3a\u5b57\u5178\u7684\u952e\u503c\u6dfb\u52a0\u65b0\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8bbf\u95ee\u5b57\u5178\u7684\u952e\u6765\u6dfb\u52a0\u6570\u636e\u3002\u5982\u679c\u952e\u5df2\u5b58\u5728\uff0c\u53ef\u4ee5\u5c06\u65b0\u6570\u636e\u8ffd\u52a0\u5230\u5f53\u524d\u503c\u4e2d\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u5178<code>data = {&#39;a&#39;: [1, 2]}<\/code>\uff0c\u8981\u4e3a\u952e<code>&#39;a&#39;<\/code>\u6dfb\u52a0\u65b0\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>data[&#39;a&#39;].append(3)<\/code>\uff0c\u5982\u679c\u8981\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e<code>&#39;b&#39;<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>data[&#39;b&#39;] = [4]<\/code>\u3002<\/p>\n<p><strong>\u5728\u5b57\u5178\u4e2d\u5982\u4f55\u5904\u7406\u91cd\u590d\u952e\u7684\u503c\uff1f<\/strong><br \/>\u5b57\u5178\u4e2d\u7684\u952e\u662f\u552f\u4e00\u7684\uff0c\u5982\u679c\u5c1d\u8bd5\u4e3a\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u952e\u6dfb\u52a0\u503c\uff0c\u65b0\u7684\u503c\u5c06\u8986\u76d6\u65e7\u7684\u503c\u3002\u5982\u679c\u9700\u8981\u4fdd\u5b58\u591a\u4e2a\u503c\uff0c\u53ef\u4ee5\u5c06\u8be5\u952e\u7684\u503c\u8bbe\u4e3a\u5217\u8868\u6216\u96c6\u5408\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>data[&#39;key&#39;] = data.get(&#39;key&#39;, []) + [new_value]<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u65b0\u503c\u88ab\u6dfb\u52a0\u5230\u73b0\u6709\u7684\u5217\u8868\u4e2d\u3002<\/p>\n<p><strong>Python\u4e2d\u4f7f\u7528defaultdict\u6765\u7b80\u5316\u952e\u503c\u6dfb\u52a0\u7684\u8fc7\u7a0b\u5417\uff1f<\/strong><br \/>\u4f7f\u7528<code>collections.defaultdict<\/code>\u53ef\u4ee5\u7b80\u5316\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u6570\u636e\u7684\u8fc7\u7a0b\u3002\u5f53\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u65b0\u6761\u76ee\u3002\u6bd4\u5982\uff0c<code>from collections import defaultdict<\/code>\u548c<code>data = defaultdict(list)<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>data[&#39;key&#39;].append(new_value)<\/code>\u6765\u6dfb\u52a0\u6570\u636e\uff0c\u800c\u4e0d\u9700\u8981\u624b\u52a8\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ed9\u952e\u503c\u589e\u52a0\u6570\u636e\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u96c6\u5408\u7c7b\u578b\u3001\u4f7f\u7528defaultdict\u3001\u4f7f\u7528setdef [&hellip;]","protected":false},"author":3,"featured_media":1079362,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079352"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1079352"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079352\/revisions"}],"predecessor-version":[{"id":1079365,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079352\/revisions\/1079365"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1079362"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1079352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1079352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1079352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}