{"id":974208,"date":"2024-12-27T06:03:18","date_gmt":"2024-12-26T22:03:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/974208.html"},"modified":"2024-12-27T06:03:21","modified_gmt":"2024-12-26T22:03:21","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e5%ad%97%e5%85%b8%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/974208.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u5b57\u5178\u8d4b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200029\/8fb32fba-731e-408c-b998-24b3ac29734b.webp\" alt=\"python\u5982\u4f55\u7ed9\u5b57\u5178\u8d4b\u503c\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7ed9\u5b57\u5178\u8d4b\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u952e\u503c\u5bf9\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528update\u65b9\u6cd5\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3002<\/strong>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\u3002\u8981\u4e3a\u5b57\u5178\u8d4b\u503c\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u6307\u5b9a\u952e\u548c\u503c\u7684\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5b57\u5178\u7279\u6709\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u76f4\u63a5\u4f7f\u7528\u952e\u503c\u5bf9\u8d4b\u503c\u662f\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u4e3a\u5b57\u5178\u4e2d\u7684\u67d0\u4e2a\u952e\u8bbe\u7f6e\u4e00\u4e2a\u503c\uff0c\u6216\u66f4\u65b0\u5df2\u6709\u952e\u7684\u503c\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>update()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u540c\u65f6\u66f4\u65b0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u800c\u5b57\u5178\u63a8\u5bfc\u5f0f\u5219\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u8d4b\u503c\u573a\u666f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u76f4\u63a5\u4f7f\u7528\u952e\u503c\u5bf9\u8d4b\u503c<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u4f7f\u7528\u952e\u503c\u5bf9\u7684\u65b9\u5f0f\u8d4b\u503c\u662f\u5b57\u5178\u64cd\u4f5c\u4e2d\u6700\u5e38\u7528\u7684\u5f62\u5f0f\u3002\u5b83\u7684\u8bed\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u5373\u901a\u8fc7\u6307\u5b9a\u5b57\u5178\u7684\u952e\u6765\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {}<\/p>\n<p>my_dict[&#39;key1&#39;] = &#39;value1&#39;<\/p>\n<p>my_dict[&#39;key2&#39;] = &#39;value2&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u76f4\u63a5\u5728\u5b57\u5178\u5bf9\u8c61\u4e0a\u4f7f\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u6765\u6307\u5b9a\u952e\uff0c\u7136\u540e\u901a\u8fc7\u7b49\u53f7<code>=<\/code>\u6765\u8d4b\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5e0c\u671b\u66f4\u65b0\u6216\u8005\u8bbe\u7f6e\u5355\u4e2a\u952e\u503c\u5bf9\u7684\u573a\u666f\u3002\u6b64\u5916\uff0c\u5f53\u952e\u5df2\u7ecf\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u65f6\uff0c\u8fd9\u79cd\u64cd\u4f5c\u4f1a\u66f4\u65b0\u8be5\u952e\u7684\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u65b0\u589e\u8be5\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528update\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u662f\u4e00\u79cd\u66f4\u4e3a\u7075\u6d3b\u7684\u5b57\u5178\u8d4b\u503c\u65b9\u5f0f\u3002\u5b83\u5141\u8bb8\u901a\u8fc7\u4f20\u9012\u53e6\u4e00\u4e2a\u5b57\u5178\u6216\u53ef\u8fed\u4ee3\u7684\u952e\u503c\u5bf9\u6765\u66f4\u65b0\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u5c24\u5176\u9002\u7528\u4e8e\u9700\u8981\u66f4\u65b0\u591a\u4e2a\u952e\u503c\u5bf9\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;key1&#39;: &#39;value1&#39;}<\/p>\n<p>update_dict = {&#39;key2&#39;: &#39;value2&#39;, &#39;key3&#39;: &#39;value3&#39;}<\/p>\n<p>my_dict.update(update_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>update()<\/code>\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728\u4e00\u6b21\u64cd\u4f5c\u4e2d\u66f4\u65b0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u7b80\u6d01\u4e14\u5177\u5907\u66f4\u9ad8\u7684\u53ef\u8bfb\u6027\u3002\u6b64\u5916\uff0c<code>update()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u63a5\u53d7\u5173\u952e\u5b57\u53c2\u6570\u7684\u5f62\u5f0f\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u8c03\u7528\u65f6\u76f4\u63a5\u4f20\u9012\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u5728\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u57fa\u7840\u4e0a\u521b\u5efa\u5b57\u5178\u3002\u5b83\u5141\u8bb8\u4ee5\u4e00\u79cd\u58f0\u660e\u6027\u548c\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u751f\u6210\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&#39;key1&#39;, &#39;key2&#39;, &#39;key3&#39;]<\/p>\n<p>values = [&#39;value1&#39;, &#39;value2&#39;, &#39;value3&#39;]<\/p>\n<p>my_dict = {k: v for k, v in zip(keys, values)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>zip()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u4e24\u4e2a\u5217\u8868<code>keys<\/code>\u548c<code>values<\/code>\u8fdb\u884c\u914d\u5bf9\uff0c\u800c\u5b57\u5178\u63a8\u5bfc\u5f0f\u5219\u5c06\u8fd9\u4e9b\u914d\u5bf9\u751f\u6210\u4e00\u4e2a\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4e8e\u4ece\u4e24\u4e2a\u76f8\u5173\u8054\u7684\u5217\u8868\u521b\u5efa\u5b57\u5178\u7684\u573a\u666f\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f7b\u677e\u5730\u52a0\u5165\u6761\u4ef6\u5224\u65ad\u6765\u751f\u6210\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u5b57\u5178\u8d4b\u503c\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u65b9\u5f0f\u6765\u8fdb\u884c\u5b57\u5178\u8d4b\u503c\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u6765\u9009\u62e9\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>setdefault\u65b9\u6cd5<\/strong>\uff1a\u8be5\u65b9\u6cd5\u7528\u4e8e\u5728\u5b57\u5178\u4e2d\u63d2\u5165\u952e\u503c\u5bf9\uff0c\u5e76\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u8bbe\u5b9a\u9ed8\u8ba4\u503c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">my_dict = {&#39;key1&#39;: &#39;value1&#39;}<\/p>\n<p>my_dict.setdefault(&#39;key2&#39;, &#39;default_value&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>fromkeys\u65b9\u6cd5<\/strong>\uff1a\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u5176\u4e2d\u6bcf\u4e2a\u952e\u90fd\u5173\u8054\u5230\u540c\u4e00\u4e2a\u503c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">keys = [&#39;key1&#39;, &#39;key2&#39;, &#39;key3&#39;]<\/p>\n<p>my_dict = dict.fromkeys(keys, &#39;initial_value&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u89e3\u5305\u8d4b\u503c<\/strong>\uff1a\u4ecePython 3.5\u5f00\u59cb\uff0c\u53ef\u4ee5\u4f7f\u7528\u89e3\u5305\u64cd\u4f5c\u7b26<code><\/code>\u5c06\u5b57\u5178\u89e3\u5305\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">dict1 = {&#39;key1&#39;: &#39;value1&#39;}<\/p>\n<p>dict2 = {&#39;key2&#39;: &#39;value2&#39;, dict1}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5b57\u5178\u8d4b\u503c\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\u65f6\uff0c\u9700\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u952e\u7684\u552f\u4e00\u6027<\/strong>\uff1a\u5b57\u5178\u4e2d\u7684\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5982\u679c\u5728\u8d4b\u503c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u91cd\u590d\u952e\uff0c\u540e\u8005\u7684\u503c\u4f1a\u8986\u76d6\u524d\u8005\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u952e\u7684\u4e0d\u53ef\u53d8\u6027<\/strong>\uff1a\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff0c\u4f8b\u5982\u6574\u6570\u3001\u5b57\u7b26\u4e32\u6216\u5143\u7ec4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u952e\u503c\u5bf9\u7684\u987a\u5e8f<\/strong>\uff1a\u4ecePython 3.7\u5f00\u59cb\uff0c\u5b57\u5178\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\uff0c\u8fd9\u610f\u5473\u7740\u5b57\u5178\u4f1a\u6309\u7167\u952e\u503c\u5bf9\u63d2\u5165\u7684\u987a\u5e8f\u8fdb\u884c\u5b58\u50a8\u548c\u904d\u5386\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5bf9\u5b57\u5178\u8d4b\u503c\u65b9\u6cd5\u7684\u5168\u9762\u4ecb\u7ecd\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u5b57\u5178\u3002\u8fd9\u4e0d\u4ec5\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u591f\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u63d0\u9ad8\u6548\u7387\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u5c06\u6709\u52a9\u4e8e\u60a8\u66f4\u597d\u5730\u5229\u7528Python\u5b57\u5178\u8fd9\u4e00\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5411\u5b57\u5178\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u65b0\u952e\u5e76\u8d4b\u503c\u7684\u65b9\u5f0f\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5411\u4e00\u4e2a\u540d\u4e3a<code>my_dict<\/code>\u7684\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a<code>&#39;age&#39;<\/code>\u7684\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<code>my_dict[&#39;age&#39;] = 25<\/code>\u3002\u8fd9\u6837\uff0c\u5b57\u5178\u4e2d\u5c31\u4f1a\u5305\u542b\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\uff1a<code>{&#39;age&#39;: 25}<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\u66f4\u65b0\u5df2\u6709\u7684\u503c\u6709\u4ec0\u4e48\u65b9\u6cd5\uff1f<\/strong><br \/>\u8981\u66f4\u65b0\u5b57\u5178\u4e2d\u5df2\u6709\u7684\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u952e\u6765\u8bbf\u95ee\u5e76\u8d4b\u4e88\u65b0\u7684\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\uff0c\u5e76\u60f3\u8981\u66f4\u65b0\u952e<code>&#39;name&#39;<\/code>\u5bf9\u5e94\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict[&#39;name&#39;] = &#39;Alice&#39;<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5c06<code>&#39;name&#39;<\/code>\u7684\u503c\u66f4\u65b0\u4e3a<code>&#39;Alice&#39;<\/code>\uff0c\u800c\u4e0d\u4f1a\u6539\u53d8\u5176\u4ed6\u952e\u503c\u5bf9\u3002<\/p>\n<p><strong>Python\u5b57\u5178\u662f\u5426\u652f\u6301\u6279\u91cf\u8d4b\u503c\uff1f\u5982\u4f55\u5b9e\u73b0\uff1f<\/strong><br \/>Python\u5b57\u5178\u652f\u6301\u6279\u91cf\u8d4b\u503c\uff0c\u53ef\u4ee5\u901a\u8fc7<code>update()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\uff0c\u5e76\u4e14\u60f3\u8981\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_dict.update({&#39;height&#39;: 170, &#39;weight&#39;: 65})<\/code>\u3002\u8fd9\u6837\uff0c\u5b57\u5178\u5c06\u540c\u65f6\u6dfb\u52a0\u6216\u66f4\u65b0\u8fd9\u4e24\u4e2a\u952e\u503c\u5bf9\uff0c\u53d8\u4e3a<code>{&#39;height&#39;: 170, &#39;weight&#39;: 65}<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7ed9\u5b57\u5178\u8d4b\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u952e\u503c\u5bf9\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528update\u65b9\u6cd5\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3002\u5728Python\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":974215,"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\/974208"}],"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=974208"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974208\/revisions"}],"predecessor-version":[{"id":974221,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974208\/revisions\/974221"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/974215"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=974208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=974208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=974208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}