{"id":1128902,"date":"2025-01-08T20:24:45","date_gmt":"2025-01-08T12:24:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1128902.html"},"modified":"2025-01-08T20:24:47","modified_gmt":"2025-01-08T12:24:47","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%be%e8%ae%a1%e4%b8%80%e4%b8%aa%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1128902.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8bbe\u8ba1\u4e00\u4e2a\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095506\/6fe90a24-761c-4461-8ad9-f3e51e1a087d.webp\" alt=\"python\u4e2d\u5982\u4f55\u8bbe\u8ba1\u4e00\u4e2a\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bbe\u8ba1\u5b57\u5178\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u5b9a\u4e49\u5b57\u5178\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u636e\u3001\u5220\u9664\u5143\u7d20\u3001\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u3002<\/strong>\u5176\u4e2d\uff0c\u5b9a\u4e49\u5b57\u5178\u548c\u6dfb\u52a0\u952e\u503c\u5bf9\u662f\u6700\u5173\u952e\u7684\u90e8\u5206\u3002\u5b9a\u4e49\u5b57\u5178\u662f\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u6216\u5305\u542b\u521d\u59cb\u6570\u636e\u7684\u5b57\u5178\uff0c\u800c\u6dfb\u52a0\u952e\u503c\u5bf9\u662f\u5c06\u5177\u4f53\u7684\u6570\u636e\u5b58\u50a8\u5230\u5b57\u5178\u4e2d\u3002\u4e0b\u9762\u8be6\u7ec6\u89e3\u91ca\u4e00\u4e0b\u5982\u4f55\u5b9a\u4e49\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u53ef\u53d8\u7684\u3001\u65e0\u5e8f\u7684\u96c6\u5408\u7c7b\u578b\uff0c\u5b83\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u50a8\u6570\u636e\u3002\u5b9a\u4e49\u5b57\u5178\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u5305\u542b\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>my_dict = {}<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u521d\u59cb\u6570\u636e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {<\/p>\n<p>    &quot;name&quot;: &quot;Alice&quot;,<\/p>\n<p>    &quot;age&quot;: 25,<\/p>\n<p>    &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5b9a\u4e49\u5b57\u5178\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 <code>dict()<\/code> \u6784\u9020\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528dict()\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u5b57\u5178<\/p>\n<p>my_dict = dict(name=&quot;Alice&quot;, age=25, city=&quot;New York&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6dfb\u52a0\u952e\u503c\u5bf9<\/h3>\n<\/p>\n<p><p>\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u952e\u4f5c\u4e3a\u7d22\u5f15\uff0c\u5e76\u8d4b\u503c\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u952e\u503c\u5bf9<\/p>\n<p>my_dict[&quot;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&quot;] = &quot;alice@example.com&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u6570\u636e\u53ef\u4ee5\u4f7f\u7528\u952e\u4f5c\u4e3a\u7d22\u5f15\uff0c\u5982\u679c\u952e\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\u3002\u4fee\u6539\u6570\u636e\u4e5f\u662f\u901a\u8fc7\u952e\u8bbf\u95ee\u5e76\u91cd\u65b0\u8d4b\u503c\u6765\u5b8c\u6210\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u6570\u636e<\/p>\n<p>print(my_dict[&quot;name&quot;])  # \u8f93\u51fa\uff1aAlice<\/p>\n<h2><strong>\u4fee\u6539\u6570\u636e<\/strong><\/h2>\n<p>my_dict[&quot;age&quot;] = 26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5220\u9664\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u5173\u952e\u5b57\u6216 <code>pop()<\/code> \u65b9\u6cd5\u3002<code>del<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u5220\u9664\u6307\u5b9a\u7684\u952e\u503c\u5bf9\uff0c\u800c <code>pop()<\/code> \u65b9\u6cd5\u5219\u5220\u9664\u6307\u5b9a\u7684\u952e\u503c\u5bf9\u5e76\u8fd4\u56de\u5176\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u5143\u7d20<\/p>\n<p>del my_dict[&quot;city&quot;]<\/p>\n<h2><strong>\u4f7f\u7528pop()\u65b9\u6cd5\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>email = my_dict.pop(&quot;email&quot;)<\/p>\n<p>print(email)  # \u8f93\u51fa\uff1aalice@example.com<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u65b9\u6cd5\u6765\u64cd\u4f5c\u5b57\u5178\uff0c\u5982 <code>keys()<\/code>\u3001<code>values()<\/code> \u548c <code>items()<\/code> \u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u6240\u6709\u952e<\/p>\n<p>keys = my_dict.keys()<\/p>\n<p>print(keys)  # \u8f93\u51fa\uff1adict_keys([&#39;name&#39;, &#39;age&#39;])<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u503c<\/strong><\/h2>\n<p>values = my_dict.values()<\/p>\n<p>print(values)  # \u8f93\u51fa\uff1adict_values([&#39;Alice&#39;, 26])<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>items = my_dict.items()<\/p>\n<p>print(items)  # \u8f93\u51fa\uff1adict_items([(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 26)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b57\u5178\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c\uff0c\u5b57\u5178\u8fd8\u652f\u6301\u4e00\u4e9b\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u5982\u5b57\u5178\u5408\u5e76\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b57\u5178\u5408\u5e76<\/h4>\n<\/p>\n<p><p>\u5728Python 3.9\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>|<\/code> \u8fd0\u7b97\u7b26\u6765\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&quot;a&quot;: 1, &quot;b&quot;: 2}<\/p>\n<p>dict2 = {&quot;b&quot;: 3, &quot;c&quot;: 4}<\/p>\n<p>merged_dict = dict1 | dict2<\/p>\n<p>print(merged_dict)  # \u8f93\u51fa\uff1a{&#39;a&#39;: 1, &#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5feb\u901f\u751f\u6210\u5b57\u5178\u7684\u65b9\u6cd5\uff0c\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b57\u5178\u63a8\u5bfc\u5f0f<\/p>\n<p>squared_numbers = {x: x*x for x in range(1, 6)}<\/p>\n<p>print(squared_numbers)  # \u8f93\u51fa\uff1a{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5b57\u5178\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u5f88\u591a\u573a\u666f\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u914d\u7f6e\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u7a0b\u5e8f\u7684\u914d\u7f6e\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config = {<\/p>\n<p>    &quot;host&quot;: &quot;localhost&quot;,<\/p>\n<p>    &quot;port&quot;: 8080,<\/p>\n<p>    &quot;debug&quot;: True<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u805a\u5408<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u7edf\u8ba1\u6570\u636e\u9891\u6b21\u6216\u5176\u4ed6\u805a\u5408\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u9891\u6b21<\/p>\n<p>text = &quot;hello world&quot;<\/p>\n<p>frequency = {}<\/p>\n<p>for char in text:<\/p>\n<p>    if char in frequency:<\/p>\n<p>        frequency[char] += 1<\/p>\n<p>    else:<\/p>\n<p>        frequency[char] = 1<\/p>\n<p>print(frequency)  # \u8f93\u51fa\uff1a{&#39;h&#39;: 1, &#39;e&#39;: 1, &#39;l&#39;: 3, &#39;o&#39;: 2, &#39; &#39;: 1, &#39;w&#39;: 1, &#39;r&#39;: 1, &#39;d&#39;: 1}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7f13\u5b58\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u7f13\u5b58\u5df2\u7ecf\u8ba1\u7b97\u8fc7\u7684\u6570\u636e\uff0c\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u7f13\u5b58\u6570\u636e<\/p>\n<p>cache = {}<\/p>\n<p>def fibonacci(n):<\/p>\n<p>    if n in cache:<\/p>\n<p>        return cache[n]<\/p>\n<p>    if n &lt; 2:<\/p>\n<p>        return n<\/p>\n<p>    result = fibonacci(n-1) + fibonacci(n-2)<\/p>\n<p>    cache[n] = result<\/p>\n<p>    return result<\/p>\n<p>print(fibonacci(10))  # \u8f93\u51fa\uff1a55<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5b57\u5178\u7684\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u6027\u80fd\u975e\u5e38\u9ad8\uff0c\u8fd9\u662f\u56e0\u4e3a\u5b83\u7684\u952e\u503c\u5bf9\u5b58\u50a8\u65b9\u5f0f\u662f\u57fa\u4e8e\u54c8\u5e0c\u8868\u7684\u3002\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f O(1)\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\uff0c\u5982\u679c\u952e\u662f\u53ef\u53d8\u7c7b\u578b\uff08\u5982\u5217\u8868\uff09\uff0c\u4f1a\u5bfc\u81f4\u54c8\u5e0c\u51b2\u7a81\uff0c\u4ece\u800c\u964d\u4f4e\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u901a\u5e38\u4f7f\u7528\u4e0d\u53ef\u53d8\u7c7b\u578b\uff08\u5982\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\uff09\u4f5c\u4e3a\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5b57\u5178\u7684\u5c40\u9650\u6027<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5b57\u5178\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u5c40\u9650\u6027\u3002\u4f8b\u5982\uff0c\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u53ef\u54c8\u5e0c\u7684\uff0c\u8fd9\u610f\u5473\u7740\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b\u3002\u6b64\u5916\uff0c\u5b57\u5178\u662f\u65e0\u5e8f\u7684\uff08\u5728Python 3.7\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u662f\u6709\u5e8f\u7684\uff0c\u4f46\u8fd9\u53ea\u662f\u5b9e\u73b0\u7ec6\u8282\uff0c\u4e0d\u5e94\u4f9d\u8d56\uff09\u3002\u6700\u540e\uff0c\u5b57\u5178\u7684\u5185\u5b58\u5f00\u9500\u8f83\u5927\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u952e\u503c\u5bf9\u90fd\u9700\u8981\u989d\u5916\u7684\u5b58\u50a8\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5b57\u5178\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u6709\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u660e\u786e\u7684\u952e<\/h4>\n<\/p>\n<p><p>\u952e\u5e94\u5f53\u5177\u6709\u660e\u786e\u7684\u610f\u4e49\uff0c\u907f\u514d\u4f7f\u7528\u6a21\u7cca\u6216\u4e0d\u76f8\u5173\u7684\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63a8\u8350<\/p>\n<p>user_info = {<\/p>\n<p>    &quot;username&quot;: &quot;john_doe&quot;,<\/p>\n<p>    &quot;email&quot;: &quot;john@example.com&quot;<\/p>\n<p>}<\/p>\n<h2><strong>\u4e0d\u63a8\u8350<\/strong><\/h2>\n<p>user_info = {<\/p>\n<p>    &quot;u&quot;: &quot;john_doe&quot;,<\/p>\n<p>    &quot;e&quot;: &quot;john@example.com&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u907f\u514d\u5d4c\u5957\u8fc7\u6df1<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u5b57\u5178\u652f\u6301\u5d4c\u5957\uff0c\u4f46\u5d4c\u5957\u8fc7\u6df1\u4f1a\u5bfc\u81f4\u4ee3\u7801\u590d\u6742\u5ea6\u589e\u52a0\uff0c\u96be\u4ee5\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63a8\u8350<\/p>\n<p>address = {<\/p>\n<p>    &quot;city&quot;: &quot;New York&quot;,<\/p>\n<p>    &quot;state&quot;: &quot;NY&quot;<\/p>\n<p>}<\/p>\n<p>user_info = {<\/p>\n<p>    &quot;username&quot;: &quot;john_doe&quot;,<\/p>\n<p>    &quot;email&quot;: &quot;john@example.com&quot;,<\/p>\n<p>    &quot;address&quot;: address<\/p>\n<p>}<\/p>\n<h2><strong>\u4e0d\u63a8\u8350<\/strong><\/h2>\n<p>user_info = {<\/p>\n<p>    &quot;username&quot;: &quot;john_doe&quot;,<\/p>\n<p>    &quot;email&quot;: &quot;john@example.com&quot;,<\/p>\n<p>    &quot;address&quot;: {<\/p>\n<p>        &quot;city&quot;: &quot;New York&quot;,<\/p>\n<p>        &quot;state&quot;: &quot;NY&quot;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528 <code>get()<\/code> \u65b9\u6cd5\u8bbf\u95ee\u952e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>get()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u629b\u51fa\u5f02\u5e38\uff0c\u5e76\u4e14\u53ef\u4ee5\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528get()\u65b9\u6cd5\u8bbf\u95ee\u952e<\/p>\n<p>age = user_info.get(&quot;age&quot;, 0)<\/p>\n<p>print(age)  # \u8f93\u51fa\uff1a0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u66f4\u9ad8\u6548\u5730\u8bbe\u8ba1\u548c\u4f7f\u7528\u5b57\u5178\u3002\u5b57\u5178\u4e0d\u4ec5\u662fPython\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u91cd\u8981\u4e00\u73af\uff0c\u4e5f\u662f\u65e5\u5e38\u7f16\u7a0b\u4e2d\u7684\u5e38\u7528\u5de5\u5177\u3002\u65e0\u8bba\u662f\u5728\u5904\u7406\u914d\u7f6e\u53c2\u6570\u3001\u6570\u636e\u805a\u5408\u8fd8\u662f\u7f13\u5b58\u6570\u636e\u7b49\u573a\u666f\u4e2d\uff0c\u5b57\u5178\u90fd\u80fd\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002\u638c\u63e1\u5b57\u5178\u7684\u4f7f\u7528\u6280\u5de7\uff0c\u5c06\u5927\u5927\u63d0\u5347\u4f60\u7684\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\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\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u6216\u8005 <code>dict()<\/code> \u51fd\u6570\u6765\u521b\u5efa\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4f7f\u7528\u82b1\u62ec\u53f7\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\uff1a<code>my_dict = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/code>\u3002\u53e6\u4e00\u79cd\u65b9\u5f0f\u662f\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\uff1a<code>my_dict = dict(key1=&#39;value1&#39;, key2=&#39;value2&#39;)<\/code>\u3002\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u5b57\u5178\u3002<\/p>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\u5982\u4f55\u6dfb\u52a0\u6216\u4fee\u6539\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u6dfb\u52a0\u6216\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u952e\u8fdb\u884c\u8d4b\u503c\u3002\u6bd4\u5982\uff0c\u5982\u679c\u8981\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>my_dict[&#39;key3&#39;] = &#39;value3&#39;<\/code>\u3002\u5982\u679c <code>key3<\/code> \u5df2\u7ecf\u5b58\u5728\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5c06\u4f1a\u66f4\u65b0\u5176\u5bf9\u5e94\u7684\u503c\u4e3a <code>&#39;value3&#39;<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5\u6216\u8005 <code>pop()<\/code> \u65b9\u6cd5\u6765\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002\u4f7f\u7528 <code>del my_dict[&#39;key1&#39;]<\/code> \u53ef\u4ee5\u5220\u9664 <code>key1<\/code> \u53ca\u5176\u5bf9\u5e94\u7684\u503c\u3002\u901a\u8fc7 <code>my_dict.pop(&#39;key2&#39;)<\/code> \u4e5f\u53ef\u4ee5\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\uff0c\u5e76\u4e14\u8be5\u65b9\u6cd5\u8fd4\u56de\u88ab\u5220\u9664\u7684\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u8ba1\u5b57\u5178\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u5b9a\u4e49\u5b57\u5178\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u636e\u3001\u5220\u9664\u5143\u7d20\u3001\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c\u5b9a [&hellip;]","protected":false},"author":3,"featured_media":1128906,"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\/1128902"}],"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=1128902"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128902\/revisions"}],"predecessor-version":[{"id":1128907,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128902\/revisions\/1128907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1128906"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1128902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1128902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1128902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}