{"id":1132323,"date":"2025-01-08T20:56:03","date_gmt":"2025-01-08T12:56:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132323.html"},"modified":"2025-01-08T20:56:05","modified_gmt":"2025-01-08T12:56:05","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e5%ad%97%e5%85%b8%e4%b8%ad%e6%b7%bb%e5%8a%a0%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132323.html","title":{"rendered":"python\u5982\u4f55\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101903\/9cf5316c-b87f-45ef-86ab-12362450949b.webp\" alt=\"python\u5982\u4f55\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u6570\u636e\u7c7b\u578b\" \/><\/p>\n<p><h2>PYTHON \u5982\u4f55\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u6570\u636e\u7c7b\u578b<\/h2>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528update\u65b9\u6cd5\u3001\u5d4c\u5957\u5b57\u5178\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u76f4\u63a5\u8d4b\u503c<\/strong>\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u64cd\u4f5c\u7b80\u5355\u4e14\u76f4\u89c2\u3002\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6570\u636e\u7c7b\u578b\u7684\u4e00\u81f4\u6027\u4ee5\u53ca\u952e\u7684\u552f\u4e00\u6027\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u662f\u6700\u7b80\u5355\u548c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\uff0c\u53ef\u4ee5\u5c06\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {}  # \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>my_dict[&#39;int&#39;] = 1  # \u6dfb\u52a0\u6574\u6570\u7c7b\u578b\u6570\u636e<\/p>\n<p>my_dict[&#39;float&#39;] = 1.1  # \u6dfb\u52a0\u6d6e\u70b9\u6570\u7c7b\u578b\u6570\u636e<\/p>\n<p>my_dict[&#39;str&#39;] = &#39;hello&#39;  # \u6dfb\u52a0\u5b57\u7b26\u4e32\u7c7b\u578b\u6570\u636e<\/p>\n<p>my_dict[&#39;list&#39;] = [1, 2, 3]  # \u6dfb\u52a0\u5217\u8868\u7c7b\u578b\u6570\u636e<\/p>\n<p>my_dict[&#39;dict&#39;] = {&#39;nested_key&#39;: &#39;nested_value&#39;}  # \u6dfb\u52a0\u5d4c\u5957\u5b57\u5178\u7c7b\u578b\u6570\u636e<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528update\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u8d4b\u503c\u5916\uff0cPython\u5b57\u5178\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>update<\/code>\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u5bf9\u952e\u503c\u5bf9\u3002<code>update<\/code>\u65b9\u6cd5\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5b57\u5178\u6216\u8005\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {}  # \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>my_dict.update({&#39;int&#39;: 1, &#39;float&#39;: 1.1, &#39;str&#39;: &#39;hello&#39;, &#39;list&#39;: [1, 2, 3], &#39;dict&#39;: {&#39;nested_key&#39;: &#39;nested_value&#39;}})<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>update<\/code>\u65b9\u6cd5\u53ef\u4ee5\u540c\u65f6\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\u63d0\u9ad8\u4e86\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5d4c\u5957\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u6709\u65f6\u9700\u8981\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6bd4\u5982\u5d4c\u5957\u5b57\u5178\u3002\u5d4c\u5957\u5b57\u5178\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6784\u5efa\u5c42\u6b21\u5316\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4fbf\u4e8e\u7ba1\u7406\u548c\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {}  # \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>my_dict[&#39;nested_dict&#39;] = {<\/p>\n<p>    &#39;int&#39;: 1,<\/p>\n<p>    &#39;float&#39;: 1.1,<\/p>\n<p>    &#39;str&#39;: &#39;hello&#39;,<\/p>\n<p>    &#39;list&#39;: [1, 2, 3],<\/p>\n<p>    &#39;dict&#39;: {&#39;nested_key&#39;: &#39;nested_value&#39;}<\/p>\n<p>}<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5d4c\u5957\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6ee1\u8db3\u66f4\u590d\u6742\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u9ed8\u8ba4\u5b57\u5178<\/h3>\n<\/p>\n<p><p>Python\u7684<code>collections<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>defaultdict<\/code>\u7684\u7c7b\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7b80\u5316\u5b57\u5178\u7684\u64cd\u4f5c\u3002<code>defaultdict<\/code>\u5141\u8bb8\u6211\u4eec\u4e3a\u5b57\u5178\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u5f53\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\u8be5\u952e\u5e76\u8d4b\u4e88\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>my_dict = defaultdict(list)  # \u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u503c\u4e3a\u5217\u8868\u7684\u5b57\u5178<\/p>\n<p>my_dict[&#39;key1&#39;].append(1)  # \u76f4\u63a5\u6dfb\u52a0\u6570\u636e\u5230\u5217\u8868\u4e2d<\/p>\n<p>my_dict[&#39;key2&#39;].extend([2, 3, 4])  # \u6269\u5c55\u5217\u8868<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>defaultdict<\/code>\u53ef\u4ee5\u7b80\u5316\u5b57\u5178\u64cd\u4f5c\uff0c\u907f\u514d\u4e86\u952e\u4e0d\u5b58\u5728\u65f6\u9700\u8981\u624b\u52a8\u521d\u59cb\u5316\u7684\u9ebb\u70e6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u7c7b\u578b\u6ce8\u89e3<\/h3>\n<\/p>\n<p><p>\u5728Python 3.5\u53ca\u5176\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u5f15\u5165\u4e86\u7c7b\u578b\u6ce8\u89e3\uff08Type Hints\uff09\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u7ba1\u7406\u4ee3\u7801\u3002\u867d\u7136\u7c7b\u578b\u6ce8\u89e3\u4e0d\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u8fd0\u884c\uff0c\u4f46\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from typing import Dict, List<\/p>\n<p>def add_data_to_dict() -&gt; Dict[str, List[int]]:<\/p>\n<p>    my_dict: Dict[str, List[int]] = {}<\/p>\n<p>    my_dict[&#39;numbers&#39;] = [1, 2, 3, 4]<\/p>\n<p>    return my_dict<\/p>\n<p>result = add_data_to_dict()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7c7b\u578b\u6ce8\u89e3\uff0c\u6211\u4eec\u53ef\u4ee5\u6e05\u695a\u5730\u77e5\u9053\u5b57\u5178\u4e2d\u952e\u548c\u503c\u7684\u7c7b\u578b\uff0c\u4fbf\u4e8e\u4ee3\u7801\u7684\u9605\u8bfb\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u6570\u636e\u7c7b\u578b\u65f6\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u8003\u8651\u6027\u80fd\u95ee\u9898\u3002\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6548\u7387\u3002\u76f4\u63a5\u8d4b\u503c\u548c<code>update<\/code>\u65b9\u6cd5\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6027\u80fd\u76f8\u5dee\u4e0d\u5927\uff0c\u4f46\u5728\u5904\u7406\u6781\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c<code>update<\/code>\u65b9\u6cd5\u53ef\u80fd\u4f1a\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u914d\u7f6e\u6587\u4ef6<\/strong>\uff1a\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u5404\u79cd\u914d\u7f6e\u53c2\u6570\u3002\u901a\u8fc7\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u548c\u8bbf\u95ee\u914d\u7f6e\u53c2\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u805a\u5408<\/strong>\uff1a\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u805a\u5408\u5230\u4e00\u4e2a\u5b57\u5178\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f13\u5b58\u673a\u5236<\/strong>\uff1a\u5728\u5b9e\u73b0\u7f13\u5b58\u673a\u5236\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u7f13\u5b58\u6570\u636e\u3002\u901a\u8fc7\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u7f13\u5b58\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def manage_configurations():<\/p>\n<p>    config = {}  # \u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>    # \u6dfb\u52a0\u914d\u7f6e\u53c2\u6570<\/p>\n<p>    config[&#39;database&#39;] = {<\/p>\n<p>        &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>        &#39;port&#39;: 3306,<\/p>\n<p>        &#39;user&#39;: &#39;admin&#39;,<\/p>\n<p>        &#39;password&#39;: &#39;password&#39;<\/p>\n<p>    }<\/p>\n<p>    config[&#39;api&#39;] = {<\/p>\n<p>        &#39;endpoint&#39;: &#39;https:\/\/api.example.com&#39;,<\/p>\n<p>        &#39;timeout&#39;: 30<\/p>\n<p>    }<\/p>\n<p>    config[&#39;features&#39;] = {<\/p>\n<p>        &#39;feature_x&#39;: True,<\/p>\n<p>        &#39;feature_y&#39;: False<\/p>\n<p>    }<\/p>\n<p>    # \u6253\u5370\u914d\u7f6e\u53c2\u6570<\/p>\n<p>    for key, value in config.items():<\/p>\n<p>        print(f&quot;{key}: {value}&quot;)<\/p>\n<p>manage_configurations()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u8bbf\u95ee\u5404\u79cd\u914d\u7f6e\u53c2\u6570\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528update\u65b9\u6cd5\u3001\u5d4c\u5957\u5b57\u5178\u7b49\u591a\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u5408\u7406\u5229\u7528\u5b57\u5178\u7684\u6570\u636e\u7ed3\u6784\u7279\u6027\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u5b57\u5178\u4e2d\u5982\u4f55\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u4f7f\u7528\u65b0\u7684\u952e\u540d\u5e76\u5c06\u5176\u8d4b\u503c\u4e3a\u60f3\u8981\u5b58\u50a8\u7684\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u8981\u6dfb\u52a0\u4e00\u4e2a\u952e<code>&quot;age&quot;<\/code>\u5e76\u5c06\u5176\u503c\u8bbe\u4e3a<code>30<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;}\nmy_dict[&#39;age&#39;] = 30\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5b57\u5178<code>my_dict<\/code>\u73b0\u5728\u5c31\u5305\u542b\u4e86<code>&#39;name&#39;<\/code>\u548c<code>&#39;age&#39;<\/code>\u4e24\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u5d4c\u5957\u6570\u636e\u7ed3\u6784\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u9700\u8981\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5217\u8868\u6216\u53e6\u4e00\u4e2a\u5b57\u5178\u4f5c\u4e3a\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u503c\u7684\u5217\u8868\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict[&#39;hobbies&#39;] = [&#39;reading&#39;, &#39;coding&#39;, &#39;hiking&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>my_dict<\/code>\u5c06\u5305\u542b\u4e00\u4e2a\u540d\u4e3a<code>&#39;hobbies&#39;<\/code>\u7684\u952e\uff0c\u5176\u503c\u662f\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5df2\u5b58\u5728\u67d0\u4e2a\u952e\uff1f<\/strong><br \/>\u5728\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u6570\u636e\u4e4b\u524d\uff0c\u68c0\u67e5\u67d0\u4e2a\u952e\u662f\u5426\u5df2\u5b58\u5728\u662f\u4e2a\u597d\u4e60\u60ef\u3002\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u68c0\u67e5\u952e<code>&#39;name&#39;<\/code>\u662f\u5426\u5728\u5b57\u5178\u4e2d\uff1a  <\/p>\n<pre><code class=\"language-python\">if &#39;name&#39; in my_dict:\n    print(&quot;\u952e &#39;name&#39; \u5df2\u5b58\u5728\u3002&quot;)\nelse:\n    my_dict[&#39;name&#39;] = &#39;Bob&#39;\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u786e\u4fdd\u60a8\u4e0d\u4f1a\u610f\u5916\u8986\u76d6\u5b57\u5178\u4e2d\u7684\u73b0\u6709\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PYTHON \u5982\u4f55\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u6570\u636e\u7c7b\u578b \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u76f4\u63a5\u8d4b\u503c\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1132332,"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\/1132323"}],"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=1132323"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132323\/revisions"}],"predecessor-version":[{"id":1132334,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132323\/revisions\/1132334"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132332"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}