{"id":1018892,"date":"2024-12-27T12:47:49","date_gmt":"2024-12-27T04:47:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1018892.html"},"modified":"2024-12-27T12:47:58","modified_gmt":"2024-12-27T04:47:58","slug":"python-keys%e5%a6%82%e4%bd%95%e5%8c%85%e5%90%ab%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1018892.html","title":{"rendered":"python keys\u5982\u4f55\u5305\u542b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161456\/225ed1a9-d585-4f69-9219-c975b42ffd6a.webp\" alt=\"python keys\u5982\u4f55\u5305\u542b\u503c\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u5b57\u5178\u7684\u952e\u901a\u5e38\u4e0d\u80fd\u76f4\u63a5\u5305\u542b\u503c<\/strong>\uff0c\u56e0\u4e3a\u5b57\u5178\u7684\u8bbe\u8ba1\u662f\u5c06\u952e\u548c\u503c\u5206\u5f00\u5b58\u50a8\uff0c\u4ee5\u4fbf\u5feb\u901f\u67e5\u627e\u548c\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u901a\u8fc7\u4f7f\u7528\u5b57\u5178\u7684\u7279\u6027\u548cPython\u7684\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u7ec4\u7ec7\u6570\u636e\uff0c\u4f7f\u5f97\u503c\u4e0e\u952e\u7d27\u5bc6\u5173\u8054\u3002<strong>\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u5c06\u503c\u5b58\u50a8\u5728\u4e0e\u952e\u76f8\u5173\u8054\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5982\u5217\u8868\u3001\u96c6\u5408\u6216\u5d4c\u5957\u5b57\u5178<\/strong>\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u952e\u4e0e\u4e00\u4e2a\u5217\u8868\u5173\u8054\uff0c\u8fd9\u6837\u8be5\u952e\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u503c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u6280\u672f\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u591a\u4e2a\u503c<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u6bcf\u4e2a\u952e\u53ef\u4ee5\u5173\u8054\u5230\u4e00\u4e2a\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u6216\u96c6\u5408\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u5355\u4e00\u952e\u4e0b\u5b58\u50a8\u591a\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5217\u8868\u4f5c\u4e3a\u503c<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u591a\u4e2a\u503c\u3002\u901a\u8fc7\u5c06\u5b57\u5178\u7684\u503c\u8bbe\u7f6e\u4e3a\u5217\u8868\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u952e\u4e0b\u5b58\u50a8\u591a\u4e2a\u76f8\u5173\u8054\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u6bcf\u4e2a\u952e\u5173\u8054\u5230\u4e00\u4e2a\u5217\u8868<\/p>\n<p>multi_val_dict = {<\/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\u4e00\u4e2a\u503c\u5230\u67d0\u4e2a\u952e\u7684\u5217\u8868\u4e2d<\/strong><\/h2>\n<p>multi_val_dict[&#39;key1&#39;].append(4)<\/p>\n<p>print(multi_val_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u5178\u7684\u6bcf\u4e2a\u952e\u90fd\u5173\u8054\u5230\u4e00\u4e2a\u5217\u8868\uff0c\u901a\u8fc7<code>append<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u96c6\u5408\u4f5c\u4e3a\u503c<\/h4>\n<\/p>\n<p><p>\u96c6\u5408\u4e0e\u5217\u8868\u7c7b\u4f3c\uff0c\u4f46\u96c6\u5408\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\u3002\u8fd9\u5728\u9700\u8981\u5b58\u50a8\u552f\u4e00\u503c\u7684\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u6bcf\u4e2a\u952e\u5173\u8054\u5230\u4e00\u4e2a\u96c6\u5408<\/p>\n<p>multi_val_dict = {<\/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\u4e00\u4e2a\u503c\u5230\u67d0\u4e2a\u952e\u7684\u96c6\u5408\u4e2d<\/strong><\/h2>\n<p>multi_val_dict[&#39;key1&#39;].add(4)<\/p>\n<p>print(multi_val_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528<code>add<\/code>\u65b9\u6cd5\u5c06\u503c\u6dfb\u52a0\u5230\u96c6\u5408\u4e2d\uff0c\u96c6\u5408\u81ea\u52a8\u5904\u7406\u91cd\u590d\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u7ec4\u7ec7\u590d\u6742\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u7684\u5173\u7cfb\u53ef\u80fd\u66f4\u590d\u6742\uff0c\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5d4c\u5957\u5b57\u5178\u7684\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5d4c\u5957\u5b57\u5178\u5141\u8bb8\u5b57\u5178\u7684\u503c\u672c\u8eab\u53c8\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u8fd9\u6837\u53ef\u4ee5\u6784\u5efa\u51fa\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u5b57\u5178<\/p>\n<p>nested_dict = {<\/p>\n<p>    &#39;key1&#39;: {&#39;subkey1&#39;: 1, &#39;subkey2&#39;: 2},<\/p>\n<p>    &#39;key2&#39;: {&#39;subkey1&#39;: 3, &#39;subkey2&#39;: 4}<\/p>\n<p>}<\/p>\n<h2><strong>\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c<\/strong><\/h2>\n<p>value = nested_dict[&#39;key1&#39;][&#39;subkey1&#39;]<\/p>\n<p>print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u901a\u8fc7\u591a\u7ea7\u952e\u8bbf\u95ee\u6570\u636e\uff0c\u5b9e\u73b0\u4e86\u66f4\u590d\u6742\u7684\u6570\u636e\u5173\u8054\u3002<\/p>\n<\/p>\n<p><h4>2. \u52a8\u6001\u521b\u5efa\u5d4c\u5957\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u52a8\u6001\u521b\u5efa\u5d4c\u5957\u5b57\u5178\u3002<code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>defaultdict<\/code>\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316\u8fd9\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u5b57\u5178\uff0c\u5176\u4e2d\u6bcf\u4e2a\u503c\u90fd\u662f\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>nested_dict = defaultdict(dict)<\/p>\n<h2><strong>\u52a8\u6001\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>nested_dict[&#39;key1&#39;][&#39;subkey1&#39;] = 1<\/p>\n<p>nested_dict[&#39;key1&#39;][&#39;subkey2&#39;] = 2<\/p>\n<p>nested_dict[&#39;key2&#39;][&#39;subkey1&#39;] = 3<\/p>\n<p>print(nested_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>defaultdict<\/code>\u81ea\u52a8\u5904\u7406\u4e0d\u5b58\u5728\u7684\u952e\uff0c\u907f\u514d\u4e86\u624b\u52a8\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u7684\u9ebb\u70e6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u5bf9\u8c61\u4f5c\u4e3a\u503c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u590d\u6742\u7684\u7ed3\u6784\u6765\u5b58\u50a8\u6570\u636e\u3002\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\uff0c\u5c06\u8fd9\u4e9b\u7c7b\u7684\u5b9e\u4f8b\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u53ef\u4ee5\u521b\u5efa\u5305\u542b\u591a\u4e2a\u5c5e\u6027\u7684\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u5bf9\u8c61\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class DataRecord:<\/p>\n<p>    def __init__(self, value1, value2):<\/p>\n<p>        self.value1 = value1<\/p>\n<p>        self.value2 = value2<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u503c\u662f\u81ea\u5b9a\u4e49\u5bf9\u8c61<\/strong><\/h2>\n<p>data_dict = {<\/p>\n<p>    &#39;key1&#39;: DataRecord(1, 2),<\/p>\n<p>    &#39;key2&#39;: DataRecord(3, 4)<\/p>\n<p>}<\/p>\n<h2><strong>\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/h2>\n<p>print(data_dict[&#39;key1&#39;].value1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u548c\u53ef\u6269\u5c55\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u590d\u6742\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5bf9\u8c61\u65b9\u6cd5\u548c\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u53ef\u4ee5\u66f4\u6709\u7ec4\u7ec7\u5730\u5904\u7406\u6570\u636e\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class DataRecord:<\/p>\n<p>    def __init__(self, value1, value2):<\/p>\n<p>        self.value1 = value1<\/p>\n<p>        self.value2 = value2<\/p>\n<p>    def update_value(self, new_value1, new_value2):<\/p>\n<p>        self.value1 = new_value1<\/p>\n<p>        self.value2 = new_value2<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u503c\u662f\u81ea\u5b9a\u4e49\u5bf9\u8c61<\/strong><\/h2>\n<p>data_dict = {<\/p>\n<p>    &#39;key1&#39;: DataRecord(1, 2),<\/p>\n<p>    &#39;key2&#39;: DataRecord(3, 4)<\/p>\n<p>}<\/p>\n<h2><strong>\u66f4\u65b0\u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/h2>\n<p>data_dict[&#39;key1&#39;].update_value(10, 20)<\/p>\n<p>print(data_dict[&#39;key1&#39;].value1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u5141\u8bb8\u5b58\u50a8\u591a\u4e2a\u5c5e\u6027\uff0c\u8fd8\u53ef\u4ee5\u96c6\u6210\u65b9\u6cd5\u6765\u64cd\u4f5c\u8fd9\u4e9b\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5143\u7ec4\u4f5c\u4e3a\u952e<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5b57\u5178\u7684\u952e\u4e0d\u80fd\u76f4\u63a5\u5305\u542b\u503c\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u5143\u7ec4\u4f5c\u4e3a\u952e\u7684\u4e00\u90e8\u5206\uff0c\u4ece\u800c\u95f4\u63a5\u5730\u5c06\u591a\u4e2a\u503c\u4e0e\u952e\u5173\u8054\u3002<\/p>\n<\/p>\n<p><h4>1. \u5143\u7ec4\u952e\u7684\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5143\u7ec4\u53ef\u4ee5\u5305\u542b\u591a\u4e2a\u503c\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528\u4f5c\u590d\u5408\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5143\u7ec4\u4f5c\u4e3a\u5b57\u5178\u7684\u952e<\/p>\n<p>tuple_key_dict = {<\/p>\n<p>    (&#39;key1&#39;, 1): &#39;value1&#39;,<\/p>\n<p>    (&#39;key2&#39;, 2): &#39;value2&#39;<\/p>\n<p>}<\/p>\n<h2><strong>\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c<\/strong><\/h2>\n<p>print(tuple_key_dict[(&#39;key1&#39;, 1)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u5728\u5b57\u5178\u7684\u952e\u4e2d\u5b58\u50a8\u591a\u4e2a\u76f8\u5173\u503c\u3002<\/p>\n<\/p>\n<p><h4>2. \u7ed3\u5408\u5143\u7ec4\u952e\u548c\u590d\u6742\u503c<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u5143\u7ec4\u4f5c\u4e3a\u952e\u548c\u590d\u6742\u6570\u636e\u7ed3\u6784\u4f5c\u4e3a\u503c\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6570\u636e\u5173\u8054\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5143\u7ec4\u4f5c\u4e3a\u952e\uff0c\u5217\u8868\u4f5c\u4e3a\u503c<\/p>\n<p>complex_dict = {<\/p>\n<p>    (&#39;key1&#39;, 1): [1, 2, 3],<\/p>\n<p>    (&#39;key2&#39;, 2): [4, 5, 6]<\/p>\n<p>}<\/p>\n<h2><strong>\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u503c<\/strong><\/h2>\n<p>complex_dict[(&#39;key1&#39;, 1)].append(4)<\/p>\n<p>print(complex_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528JSON\u6216\u5176\u4ed6\u5e8f\u5217\u5316\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528JSON\u6216\u5176\u4ed6\u5e8f\u5217\u5316\u683c\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u548c\u4f20\u8f93\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>1. JSON\u683c\u5f0f\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>JSON\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u5e38\u7528\u4e8e\u7f51\u7edc\u6570\u636e\u4f20\u8f93\u548c\u6301\u4e45\u5316\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u6570\u636e\u7ed3\u6784<\/strong><\/h2>\n<p>data_structure = {<\/p>\n<p>    &#39;key1&#39;: {&#39;subkey1&#39;: [1, 2, 3], &#39;subkey2&#39;: [4, 5, 6]},<\/p>\n<p>    &#39;key2&#39;: {&#39;subkey1&#39;: [7, 8, 9], &#39;subkey2&#39;: [10, 11, 12]}<\/p>\n<p>}<\/p>\n<h2><strong>\u5e8f\u5217\u5316\u4e3aJSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_data = json.dumps(data_structure)<\/p>\n<p>print(json_data)<\/p>\n<h2><strong>\u53cd\u5e8f\u5217\u5316\u56dePython\u6570\u636e\u7ed3\u6784<\/strong><\/h2>\n<p>loaded_data = json.loads(json_data)<\/p>\n<p>print(loaded_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>JSON\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u529f\u80fd\u4f7f\u5f97\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u4f20\u8f93\u548c\u5b58\u50a8\u53d8\u5f97\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h4>2. \u7ed3\u5408\u5176\u4ed6\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u9664\u4e86JSON\uff0cPython\u8fd8\u652f\u6301\u591a\u79cd\u5176\u4ed6\u7684\u5e8f\u5217\u5316\u683c\u5f0f\uff0c\u5982YAML\u3001XML\u7b49\uff0c\u8fd9\u4e9b\u683c\u5f0f\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<h2><strong>\u4f7f\u7528YAML\u5e8f\u5217\u5316<\/strong><\/h2>\n<p>yaml_data = yaml.dump(data_structure)<\/p>\n<p>print(yaml_data)<\/p>\n<h2><strong>\u4eceYAML\u52a0\u8f7d<\/strong><\/h2>\n<p>loaded_yaml_data = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(yaml_data)<\/p>\n<p>print(loaded_yaml_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u683c\u5f0f\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u4f18\u52bf\uff0c\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u5904\u7406\u7684\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f18\u5316\u5b57\u5178\u7684\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u4f18\u5316\u5b57\u5178\u7684\u6027\u80fd\u53ef\u80fd\u53d8\u5f97\u5f88\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f18\u5316\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>set<\/code>\u800c\u4e0d\u662f<code>list<\/code>\u6765\u5b58\u50a8\u552f\u4e00\u503c\uff0c\u4f7f\u7528<code>deque<\/code>\u800c\u4e0d\u662f<code>list<\/code>\u6765\u5b9e\u73b0\u961f\u5217\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u53cc\u7aef\u961f\u5217<\/strong><\/h2>\n<p>queue = deque([1, 2, 3])<\/p>\n<h2><strong>\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>queue.append(4)<\/p>\n<p>queue.popleft()<\/p>\n<p>print(queue)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>deque<\/code>\u5728\u961f\u5217\u64cd\u4f5c\u4e0a\u7684\u6027\u80fd\u6bd4\u5217\u8868\u66f4\u597d\u3002<\/p>\n<\/p>\n<p><h4>2. \u6279\u91cf\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\uff0c\u5c3d\u91cf\u4f7f\u7528\u6279\u91cf\u64cd\u4f5c\u6765\u51cf\u5c11\u5faa\u73af\u4e2d\u7684\u64cd\u4f5c\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6279\u91cf\u66f4\u65b0\u5b57\u5178<\/p>\n<p>update_dict = {&#39;key3&#39;: 7, &#39;key4&#39;: 8}<\/p>\n<p>multi_val_dict.update(update_dict)<\/p>\n<p>print(multi_val_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6279\u91cf\u64cd\u4f5c\u901a\u5e38\u6bd4\u9010\u4e2a\u64cd\u4f5c\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>3. \u5185\u5b58\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u5185\u5b58\u7ba1\u7406\u53d8\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u4f7f\u7528\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u53ef\u4ee5\u5e2e\u52a9\u964d\u4f4e\u5185\u5b58\u6d88\u8017\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/p>\n<p>large_data = (x for x in range(1000000))<\/p>\n<h2><strong>\u9010\u4e2a\u5904\u7406\u6570\u636e\u800c\u4e0d\u5360\u7528\u5927\u91cf\u5185\u5b58<\/strong><\/h2>\n<p>for data in large_data:<\/p>\n<p>    process_data(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u5668\u6309\u9700\u751f\u6210\u6570\u636e\uff0c\u907f\u514d\u4e86\u5927\u6570\u636e\u96c6\u5728\u5185\u5b58\u4e2d\u7684\u5b8c\u6574\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b57\u5178\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6848\u4f8b<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u5b9e\u9645\u573a\u666f\u4e2d\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u6848\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1. \u914d\u7f6e\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u5e38\u7528\u4e8e\u5b58\u50a8\u548c\u7ba1\u7406\u7a0b\u5e8f\u7684\u914d\u7f6e\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config = {<\/p>\n<p>    &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>    &#39;port&#39;: 8080,<\/p>\n<p>    &#39;debug&#39;: True<\/p>\n<p>}<\/p>\n<h2><strong>\u8bfb\u53d6\u914d\u7f6e<\/strong><\/h2>\n<p>host = config[&#39;host&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u53ef\u4ee5\u8f7b\u677e\u7ba1\u7406\u548c\u4fee\u6539\u914d\u7f6e\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u6570\u636e\u5206\u7ec4\u548c\u805a\u5408<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u5206\u7ec4\u548c\u805a\u5408\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u5b57\u5178\uff0c\u7528\u4e8e\u5206\u7ec4\u6570\u636e<\/strong><\/h2>\n<p>grouped_data = defaultdict(list)<\/p>\n<h2><strong>\u6a21\u62df\u6570\u636e<\/strong><\/h2>\n<p>data_points = [(&#39;A&#39;, 1), (&#39;B&#39;, 2), (&#39;A&#39;, 3), (&#39;B&#39;, 4)]<\/p>\n<h2><strong>\u5206\u7ec4<\/strong><\/h2>\n<p>for key, value in data_points:<\/p>\n<p>    grouped_data[key].append(value)<\/p>\n<p>print(grouped_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u5206\u7ec4\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u5206\u6790\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>3. \u7f13\u5b58\u673a\u5236<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u4e5f\u5e38\u7528\u4e8e\u5b9e\u73b0\u7b80\u5355\u7684\u7f13\u5b58\u673a\u5236\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cache = {}<\/p>\n<p>def expensive_computation(param):<\/p>\n<p>    if param in cache:<\/p>\n<p>        return cache[param]<\/p>\n<p>    # \u6a21\u62df\u8ba1\u7b97<\/p>\n<p>    result = param  2<\/p>\n<p>    cache[param] = result<\/p>\n<p>    return result<\/p>\n<h2><strong>\u4f7f\u7528\u7f13\u5b58<\/strong><\/h2>\n<p>print(expensive_computation(4))<\/p>\n<p>print(expensive_computation(4))  # \u7b2c\u4e8c\u6b21\u8c03\u7528\u547d\u4e2d\u7f13\u5b58<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7f13\u5b58\u8ba1\u7b97\u7ed3\u679c\uff0c\u53ef\u4ee5\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff0c\u5b57\u5178\u5728Python\u4e2d\u662f\u4e00\u4e2a\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u901a\u8fc7\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u7ec4\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u7ba1\u7406\u548c\u64cd\u4f5c\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u7b56\u7565\u548c\u6280\u672f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u5b57\u5178\u7684\u952e\u662f\u5426\u5305\u542b\u7279\u5b9a\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u5178\u7684\u952e\u5e76\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u68c0\u67e5\u662f\u5426\u6709\u952e\u7684\u503c\u7b49\u4e8e\u7279\u5b9a\u503c\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u7684<code>items()<\/code>\u65b9\u6cd5\u6765\u540c\u65f6\u83b7\u53d6\u952e\u548c\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u6240\u6709\u5305\u542b\u7279\u5b9a\u503c\u7684\u952e\u3002<\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6\u5305\u542b\u7279\u5b9a\u503c\u7684\u6240\u6709\u952e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u5b57\u5178\uff0c\u5176\u4e2d\u5305\u542b\u6240\u6709\u4e0e\u7279\u5b9a\u503c\u5339\u914d\u7684\u952e\u3002\u901a\u8fc7\u904d\u5386\u5b57\u5178\u7684\u9879\uff08\u952e\u503c\u5bf9\uff09\uff0c\u53ef\u4ee5\u7b5b\u9009\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u952e\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u4ece\u800c\u8f7b\u677e\u83b7\u53d6\u6240\u6709\u76f8\u5173\u952e\u3002<\/p>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\uff0c\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u5b58\u5728\u4e8e\u67d0\u4e2a\u952e\u4e2d\uff1f<\/strong><br \/>\u5224\u65ad\u5b57\u5178\u4e2d\u67d0\u4e2a\u952e\u5bf9\u5e94\u7684\u503c\u662f\u5426\u4e0e\u7279\u5b9a\u503c\u76f8\u540c\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u8be5\u952e\uff0c\u5e76\u4e0e\u76ee\u6807\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u9700\u8981\u68c0\u67e5\u591a\u4e2a\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6216\u6761\u4ef6\u8bed\u53e5\u6765\u5b8c\u6210\u8fd9\u4e00\u64cd\u4f5c\uff0c\u4ee5\u786e\u5b9a\u503c\u662f\u5426\u5728\u591a\u4e2a\u952e\u4e2d\u5b58\u5728\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b57\u5178\u7684\u952e\u901a\u5e38\u4e0d\u80fd\u76f4\u63a5\u5305\u542b\u503c\uff0c\u56e0\u4e3a\u5b57\u5178\u7684\u8bbe\u8ba1\u662f\u5c06\u952e\u548c\u503c\u5206\u5f00\u5b58\u50a8\uff0c\u4ee5\u4fbf\u5feb\u901f\u67e5\u627e\u548c\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u901a\u8fc7 [&hellip;]","protected":false},"author":3,"featured_media":1018908,"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\/1018892"}],"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=1018892"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018892\/revisions"}],"predecessor-version":[{"id":1018910,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018892\/revisions\/1018910"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1018908"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1018892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1018892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1018892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}