{"id":986775,"date":"2024-12-27T07:47:54","date_gmt":"2024-12-26T23:47:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/986775.html"},"modified":"2024-12-27T07:47:57","modified_gmt":"2024-12-26T23:47:57","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e5%ad%97%e5%85%b8%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/986775.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6e\u5b57\u5178\u7684"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063208\/2b9efb85-3276-4964-bed4-4a61b857da67.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6e\u5b57\u5178\u7684\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bbe\u7f6e\u5b57\u5178\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7{}\u521b\u5efa\u5b57\u5178\u3001\u4f7f\u7528dict()\u51fd\u6570\u521b\u5efa\u5b57\u5178\u3001\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u65b9\u5f0f\u6dfb\u52a0\u6216\u66f4\u65b0\u5143\u7d20\u3001\u4f7f\u7528update()\u65b9\u6cd5\u5408\u5e76\u5b57\u5178\u3002<\/strong> \u5176\u4e2d\uff0c\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u65b9\u5f0f\u6dfb\u52a0\u6216\u66f4\u65b0\u5143\u7d20\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u5b57\u5178\u7684\u952e\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20\uff0c\u6bd4\u5982<code>dict[key] = value<\/code>\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u503c\u5c31\u4f1a\u88ab\u66f4\u65b0\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u65b0\u589e\u8be5\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u65b9\u5f0f\u6dfb\u52a0\u6216\u66f4\u65b0\u5143\u7d20\u662f\u7075\u6d3b\u4e14\u7b80\u5355\u7684\u3002\u4f60\u53ea\u9700\u8981\u77e5\u9053\u952e\u540d\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u5feb\u901f\u5730\u5bf9\u5b57\u5178\u8fdb\u884c\u64cd\u4f5c\u3002\u6bd4\u5982\u8bf4\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b58\u50a8\u5b66\u751f\u6210\u7ee9\u7684\u5b57\u5178\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>students[&#39;John&#39;] = 90<\/code>\u7684\u65b9\u5f0f\u6765\u66f4\u65b0John\u7684\u6210\u7ee9\u3002\u5982\u679cJohn\u8fd8\u4e0d\u5728\u5b57\u5178\u91cc\uff0c\u8fd9\u884c\u4ee3\u7801\u4f1a\u81ea\u52a8\u628aJohn\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u5c06\u4ed6\u7684\u6210\u7ee9\u8bbe\u7f6e\u4e3a90\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5728\u52a8\u6001\u4fee\u6539\u5b57\u5178\u5185\u5bb9\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5404\u79cd\u65b9\u6cd5\u8bbe\u7f6ePython\u5b57\u5178\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528\u82b1\u62ec\u53f7{}\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u82b1\u62ec\u53f7{}\u662f\u521b\u5efa\u5b57\u5178\u7684\u6700\u76f4\u89c2\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\u76f4\u89c2\uff0c\u800c\u4e14\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6548\u7387\u6700\u9ad8\u3002<\/p>\n<\/p>\n<p><h2>1. \u521b\u5efa\u7a7a\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u4e24\u4e2a\u82b1\u62ec\u53f7<code>{}<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\u3002\u7a7a\u5b57\u5178\u5728\u521d\u59cb\u5316\u65f6\u6ca1\u6709\u4efb\u4f55\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u5728\u4e4b\u540e\u52a8\u6001\u6dfb\u52a0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">empty_dict = {}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u521b\u5efa\u5e26\u6709\u521d\u59cb\u503c\u7684\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u82b1\u62ec\u53f7\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u521b\u5efa\u5b57\u5178\u65f6\u76f4\u63a5\u63d0\u4f9b\u521d\u59cb\u7684\u952e\u503c\u5bf9\u3002\u952e\u548c\u503c\u4e4b\u95f4\u7528\u5192\u53f7\u5206\u9694\uff0c\u591a\u4e2a\u952e\u503c\u5bf9\u7528\u9017\u53f7\u5206\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u5728\u9700\u8981\u4e00\u5f00\u59cb\u5c31\u521d\u59cb\u5316\u5b57\u5178\u5185\u5bb9\u7684\u573a\u666f\u4e0b\u4f7f\u7528\u3002\u82b1\u62ec\u53f7\u65b9\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u901a\u5e38\u7528\u4e8e\u5b57\u5178\u5185\u5bb9\u76f8\u5bf9\u8f83\u5c11\u7684\u60c5\u51b5\uff0c\u65b9\u4fbf\u8fdb\u884c\u624b\u52a8\u7f16\u8f91\u548c\u68c0\u67e5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528dict()\u51fd\u6570\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p><p><code>dict()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5728\u4ece\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3a\u5b57\u5178\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h2>1. \u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u521b\u5efa\u5b57\u5178<\/h2>\n<\/p>\n<p><p><code>dict()<\/code>\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5173\u952e\u5b57\u53c2\u6570\u7684\u5f62\u5f0f\u76f4\u63a5\u521b\u5efa\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u5f0f\u8981\u6c42\u952e\u662f\u5408\u6cd5\u7684Python\u6807\u8bc6\u7b26\uff08\u901a\u5e38\u662f\u5b57\u7b26\u4e32\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = dict(name=&#39;Alice&#39;, age=30, city=&#39;New York&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u521b\u5efa\u5b57\u5178<\/h2>\n<\/p>\n<p><p><code>dict()<\/code>\u8fd8\u53ef\u4ee5\u4ece\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u6216\u5143\u7ec4\uff09\u521b\u5efa\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u9700\u8981\u4ece\u73b0\u6709\u7684\u6570\u636e\u7ed3\u6784\u4e2d\u521b\u5efa\u5b57\u5178\u65f6\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rs = [(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 30), (&#39;city&#39;, &#39;New York&#39;)]<\/p>\n<p>person = dict(pairs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u9002\u5408\u5728\u5904\u7406\u6570\u636e\u8f6c\u6362\u548c\u89e3\u6790\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u65b9\u5f0f\u6dfb\u52a0\u6216\u66f4\u65b0\u5143\u7d20<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u52a8\u6001\u5730\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\u3002\u901a\u8fc7\u76f4\u63a5\u4f7f\u7528\u952e\u540d\u6765\u8bbe\u7f6e\u503c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>1. \u6dfb\u52a0\u65b0\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u952e\u4e0d\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff0c\u4f7f\u7528<code>dict[key] = value<\/code>\u8bed\u6cd5\u4f1a\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = {&#39;name&#39;: &#39;Alice&#39;}<\/p>\n<p>person[&#39;age&#39;] = 30  # \u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u66f4\u65b0\u73b0\u6709\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u8bed\u6cd5\u4f1a\u66f4\u65b0\u8be5\u952e\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person[&#39;name&#39;] = &#39;Bob&#39;  # \u66f4\u65b0\u5df2\u6709\u952e\u7684\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u5408\u5728\u9700\u8981\u5bf9\u5b57\u5178\u5185\u5bb9\u8fdb\u884c\u52a8\u6001\u66f4\u65b0\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528update()\u65b9\u6cd5\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u5f0f\u6765\u5408\u5e76\u6216\u66f4\u65b0\u5b57\u5178\u5185\u5bb9\u3002\u5b83\u53ef\u4ee5\u5c06\u53e6\u4e00\u4e2a\u5b57\u5178\u6216\u4e00\u4e2a\u952e\u503c\u5bf9\u5e8f\u5217\u6dfb\u52a0\u5230\u5f53\u524d\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>1. \u4f7f\u7528\u53e6\u4e00\u4e2a\u5b57\u5178\u66f4\u65b0<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4f20\u9012\u53e6\u4e00\u4e2a\u5b57\u5178\u7ed9<code>update()<\/code>\u65b9\u6cd5\uff0c\u80fd\u591f\u5c06\u5176\u6240\u6709\u7684\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u5f53\u524d\u5b57\u5178\u4e2d\u3002\u5982\u679c\u6709\u76f8\u540c\u7684\u952e\uff0c\u5219\u66f4\u65b0\u5176\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}<\/p>\n<p>updates = {&#39;age&#39;: 31, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>person.update(updates)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u952e\u503c\u5bf9\u5e8f\u5217\u66f4\u65b0<\/h2>\n<\/p>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5982\u5217\u8868\u6216\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person.update([(&#39;country&#39;, &#39;USA&#39;), (&#39;age&#39;, 32)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>update()<\/code>\u65b9\u6cd5\u9002\u5408\u5728\u9700\u8981\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u5185\u5bb9\u6216\u4ece\u5176\u4ed6\u7ed3\u6784\u4e2d\u6279\u91cf\u66f4\u65b0\u5b57\u5178\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u5b57\u5178\u7684\u5176\u4ed6\u5e38\u7528\u64cd\u4f5c<\/strong><\/h2>\n<p><p>\u9664\u4e86\u8bbe\u7f6e\u5b57\u5178\uff0cPython\u4e2d\u8fd8\u6709\u8bb8\u591a\u5e38\u7528\u7684\u5b57\u5178\u64cd\u4f5c\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7ba1\u7406\u548c\u4f7f\u7528\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h2>1. \u5220\u9664\u952e\u503c\u5bf9<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u6216<code>pop()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">del person[&#39;age&#39;]  # \u5220\u9664\u6307\u5b9a\u952e<\/p>\n<p>age = person.pop(&#39;age&#39;, None)  # \u5220\u9664\u952e\u5e76\u8fd4\u56de\u5176\u503c\uff0c\u82e5\u4e0d\u5b58\u5728\u5219\u8fd4\u56deNone<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u68c0\u67e5\u952e\u7684\u5b58\u5728\u6027<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5305\u542b\u7279\u5b9a\u7684\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if &#39;name&#39; in person:<\/p>\n<p>    print(&quot;Name is present in the dictionary&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3. \u83b7\u53d6\u5b57\u5178\u7684\u952e\u3001\u503c\u548c\u9879<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u7684<code>keys()<\/code>\u3001<code>values()<\/code>\u548c<code>items()<\/code>\u65b9\u6cd5\u5206\u522b\u8fd4\u56de\u5b57\u5178\u7684\u952e\u96c6\u5408\u3001\u503c\u96c6\u5408\u548c\u952e\u503c\u5bf9\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = person.keys()<\/p>\n<p>values = person.values()<\/p>\n<p>items = person.items()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u5b57\u5178\u7684\u5e94\u7528\u573a\u666f<\/strong><\/h2>\n<p><p>\u5b57\u5178\u5728Python\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u5176\u7075\u6d3b\u6027\u548c\u9ad8\u6548\u6027\u4f7f\u5176\u6210\u4e3a\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h2>1. \u914d\u7f6e\u7ba1\u7406<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u5e38\u7528\u4e8e\u5b58\u50a8\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u53c2\u6570\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u7ec4\u7ec7\u548c\u8bbf\u95ee\u914d\u7f6e\u4fe1\u606f\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<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u6570\u636e\u5206\u7ec4\u4e0e\u5206\u7c7b<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u5c06\u6570\u636e\u5206\u7ec4\u6216\u5206\u7c7b\uff0c\u4f7f\u5f97\u53ef\u4ee5\u901a\u8fc7\u952e\u5feb\u901f\u8bbf\u95ee\u7279\u5b9a\u7c7b\u522b\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = {<\/p>\n<p>    &#39;classA&#39;: [&#39;John&#39;, &#39;Alice&#39;],<\/p>\n<p>    &#39;classB&#39;: [&#39;Bob&#39;, &#39;Charlie&#39;]<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3. \u7f13\u5b58\u4e0e\u6620\u5c04<\/h2>\n<\/p>\n<p><p>\u5b57\u5178\u80fd\u591f\u9ad8\u6548\u5730\u5b9e\u73b0\u6570\u636e\u7f13\u5b58\u548c\u6620\u5c04\uff0c\u5728\u9700\u8981\u9891\u7e41\u67e5\u627e\u548c\u66f4\u65b0\u6570\u636e\u7684\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cache = {}<\/p>\n<p>def get_data(key):<\/p>\n<p>    if key in cache:<\/p>\n<p>        return cache[key]<\/p>\n<p>    else:<\/p>\n<p>        # Simulate data fetching<\/p>\n<p>        data = fetch_data_from_source(key)<\/p>\n<p>        cache[key] = data<\/p>\n<p>        return data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u5b57\u5178\u7684\u6df1\u5165\u7406\u89e3\u548c\u7075\u6d3b\u4f7f\u7528\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8Python\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5b57\u5178\u4f5c\u4e3aPython\u4e2d\u7684\u91cd\u8981\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\u4f7f\u5176\u5728\u5404\u79cd\u7f16\u7a0b\u4efb\u52a1\u4e2d\u90fd\u80fd\u53d1\u6325\u91cd\u8981\u4f5c\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u5927\u62ec\u53f7 <code>{}<\/code> \u6216\u8005\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u521b\u5efa\u3002\u4f7f\u7528\u5927\u62ec\u53f7\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u952e\u503c\u5bf9\uff0c\u4f8b\u5982\uff1a<code>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/code>\u3002\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f20\u9012\u5173\u952e\u5b57\u53c2\u6570\u6216\u8005\u5143\u7ec4\u5217\u8868\uff0c\u4f8b\u5982\uff1a<code>my_dict = dict(name=&#39;Alice&#39;, age=25)<\/code>\u3002<\/p>\n<p><strong>Python\u5b57\u5178\u7684\u952e\u548c\u503c\u53ef\u4ee5\u662f\u4ec0\u4e48\u7c7b\u578b\uff1f<\/strong><br \/>Python\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u8005\u5143\u7ec4\uff0c\u800c\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u5217\u8868\u3001\u5b57\u5178\u6216\u81ea\u5b9a\u4e49\u5bf9\u8c61\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97\u5b57\u5178\u975e\u5e38\u9002\u5408\u5b58\u50a8\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u8981\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8d4b\u503c\u8bed\u6cd5\uff0c\u4f8b\u5982\uff1a<code>my_dict[&#39;city&#39;] = &#39;New York&#39;<\/code>\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u8d4b\u503c\u8bed\u6cd5\u5c06\u4f1a\u66f4\u65b0\u8be5\u952e\u5bf9\u5e94\u7684\u503c\u3002\u5982\u679c\u9700\u8981\u66f4\u65b0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>update()<\/code> \u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<code>my_dict.update({&#39;age&#39;: 26, &#39;country&#39;: &#39;USA&#39;})<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u7f6e\u5b57\u5178\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u82b1\u62ec\u53f7{}\u521b\u5efa\u5b57\u5178\u3001\u4f7f\u7528dict()\u51fd\u6570\u521b\u5efa\u5b57\u5178\u3001\u901a\u8fc7\u952e\u503c\u5bf9\u7684\u65b9\u5f0f\u6dfb\u52a0 [&hellip;]","protected":false},"author":3,"featured_media":986779,"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\/986775"}],"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=986775"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986775\/revisions"}],"predecessor-version":[{"id":986785,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986775\/revisions\/986785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/986779"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=986775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=986775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=986775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}