{"id":988963,"date":"2024-12-27T08:07:09","date_gmt":"2024-12-27T00:07:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/988963.html"},"modified":"2024-12-27T08:07:13","modified_gmt":"2024-12-27T00:07:13","slug":"python%e4%b8%ad%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/988963.html","title":{"rendered":"python\u4e2d\u5b57\u5178\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064350\/3a682f7b-dc59-4435-a9c6-50c7ab4045fc.webp\" alt=\"python\u4e2d\u5b57\u5178\u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\u3002\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u53ef\u4ee5\u662f\u4efb\u4f55\u4e0d\u53ef\u53d8\u7c7b\u578b\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\u3002\u5b57\u5178\u7684\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\u3002\u4f7f\u7528\u5b57\u5178\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u521b\u5efa\u5b57\u5178\u3001\u8bbf\u95ee\u548c\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3001\u5220\u9664\u5143\u7d20\u4ee5\u53ca\u904d\u5386\u5b57\u5178\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u4f7f\u7528\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efa\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521b\u5efa\u5b57\u5178\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u6765\u5b9a\u4e49\u5b57\u5178\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5927\u62ec\u53f7\u521b\u5efa\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u4f7f\u7528 dict() \u51fd\u6570\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p>my_dict2 = dict(name=&#39;Bob&#39;, age=30, city=&#39;Los Angeles&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e24\u79cd\u65b9\u6cd5\u4e2d\uff0c\u5b57\u5178\u90fd\u662f\u7531\u952e\u503c\u5bf9\u7ec4\u6210\u7684\uff0c\u952e\u548c\u503c\u4e4b\u95f4\u7528\u5192\u53f7 <code>:<\/code> \u5206\u9694\uff0c\u591a\u4e2a\u952e\u503c\u5bf9\u4e4b\u95f4\u7528\u9017\u53f7 <code>,<\/code> \u5206\u9694\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u5b9e\u73b0\u3002\u5982\u679c\u952e\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1 <code>KeyError<\/code> \u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>get()<\/code> \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u8fd4\u56de <code>None<\/code> \u6216\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u901a\u8fc7\u952e\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/p>\n<p>print(my_dict[&#39;name&#39;])  # \u8f93\u51fa: Alice<\/p>\n<h2><strong>\u4f7f\u7528 get() \u65b9\u6cd5\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/strong><\/h2>\n<p>print(my_dict.get(&#39;age&#39;))  # \u8f93\u51fa: 25<\/p>\n<p>print(my_dict.get(&#39;gender&#39;, &#39;Not Specified&#39;))  # \u8f93\u51fa: Not Specified<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5bf9\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u4fee\u6539\u3002\u4fee\u6539\u65f6\uff0c\u53ea\u9700\u901a\u8fc7\u952e\u6307\u5b9a\u65b0\u7684\u503c\u5373\u53ef\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fee\u6539\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/p>\n<p>my_dict[&#39;age&#39;] = 26<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 26, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;gender&#39;] = &#39;Female&#39;<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 26, &#39;city&#39;: &#39;New York&#39;, &#39;gender&#39;: &#39;Female&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5\u6216 <code>pop()<\/code> \u65b9\u6cd5\u4ece\u5b57\u5178\u4e2d\u5220\u9664\u5143\u7d20\u3002<code>del<\/code> \u8bed\u53e5\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u6307\u5b9a\u952e\u7684\u5143\u7d20\uff0c\u800c <code>pop()<\/code> \u65b9\u6cd5\u9664\u4e86\u5220\u9664\u5143\u7d20\u5916\uff0c\u8fd8\u53ef\u4ee5\u8fd4\u56de\u88ab\u5220\u9664\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 del \u8bed\u53e5\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/p>\n<p>del my_dict[&#39;city&#39;]<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 26, &#39;gender&#39;: &#39;Female&#39;}<\/p>\n<h2><strong>\u4f7f\u7528 pop() \u65b9\u6cd5\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20<\/strong><\/h2>\n<p>age = my_dict.pop(&#39;age&#39;)<\/p>\n<p>print(age)  # \u8f93\u51fa: 26<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;gender&#39;: &#39;Female&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u904d\u5386\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002<code>keys()<\/code> \u65b9\u6cd5\u8fd4\u56de\u5b57\u5178\u4e2d\u6240\u6709\u7684\u952e\uff0c<code>values()<\/code> \u65b9\u6cd5\u8fd4\u56de\u5b57\u5178\u4e2d\u6240\u6709\u7684\u503c\uff0c<code>items()<\/code> \u65b9\u6cd5\u8fd4\u56de\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u5b57\u5178\u7684\u952e<\/p>\n<p>for key in my_dict.keys():<\/p>\n<p>    print(key)<\/p>\n<h2><strong>\u904d\u5386\u5b57\u5178\u7684\u503c<\/strong><\/h2>\n<p>for value in my_dict.values():<\/p>\n<p>    print(value)<\/p>\n<h2><strong>\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>for key, value in my_dict.items():<\/p>\n<p>    print(key, value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5b57\u5178\u7684\u5176\u4ed6\u5e38\u7528\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u57fa\u672c\u64cd\u4f5c\uff0c\u5b57\u5178\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982 <code>update()<\/code>\u3001<code>clear()<\/code> \u548c <code>copy()<\/code>\u3002<\/p>\n<\/p>\n<ul>\n<li><code>update()<\/code>: \u7528\u4e8e\u5c06\u4e00\u4e2a\u5b57\u5178\u7684\u5185\u5bb9\u66f4\u65b0\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002\u5982\u679c\u6709\u91cd\u590d\u7684\u952e\uff0c\u5219\u66f4\u65b0\u4e3a\u65b0\u7684\u503c\u3002<\/li>\n<li><code>clear()<\/code>: \u5220\u9664\u5b57\u5178\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/li>\n<li><code>copy()<\/code>: \u8fd4\u56de\u5b57\u5178\u7684\u4e00\u4e2a\u6d45\u62f7\u8d1d\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 update() \u65b9\u6cd5\u66f4\u65b0\u5b57\u5178<\/p>\n<p>my_dict.update({&#39;age&#39;: 27, &#39;city&#39;: &#39;San Francisco&#39;})<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;gender&#39;: &#39;Female&#39;, &#39;age&#39;: 27, &#39;city&#39;: &#39;San Francisco&#39;}<\/p>\n<h2><strong>\u4f7f\u7528 clear() \u65b9\u6cd5\u6e05\u7a7a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict.clear()<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {}<\/p>\n<h2><strong>\u4f7f\u7528 copy() \u65b9\u6cd5\u62f7\u8d1d\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>new_dict = my_dict.copy()<\/p>\n<p>print(new_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u5b57\u5178\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u5728Python\u4e2d\u6709\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff0c\u5305\u62ec\u5b58\u50a8\u548c\u5feb\u901f\u67e5\u627e\u6570\u636e\u3001\u5b9e\u73b0\u7b80\u5355\u7684\u6570\u636e\u5e93\u3001\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u3001\u5904\u7406JSON\u6570\u636e\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5b58\u50a8\u548c\u5feb\u901f\u67e5\u627e\u6570\u636e\uff1a\u5b57\u5178\u7684\u952e\u503c\u5bf9\u7ed3\u6784\u4f7f\u5176\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5b58\u50a8\u548c\u5feb\u901f\u67e5\u627e\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u5b58\u50a8\u7528\u6237\u4fe1\u606f\uff0c\u5e76\u901a\u8fc7\u7528\u6237\u540d\u5feb\u901f\u67e5\u627e\u7528\u6237\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u73b0\u7b80\u5355\u7684\u6570\u636e\u5e93\uff1a\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u6a21\u62df\u7b80\u5355\u7684\u6570\u636e\u5e93\u7ed3\u6784\uff0c\u901a\u8fc7\u5d4c\u5957\u5b57\u5178\u6765\u8868\u793a\u8868\u683c\u548c\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4f5c\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\uff1a\u5b57\u5178\u53ef\u4ee5\u7528\u4f5c\u51fd\u6570\u7684\u53ef\u53d8\u53c2\u6570\uff0c\u901a\u8fc7 <code>kwargs<\/code> \u8bed\u6cd5\u5c06\u5b57\u5178\u4f20\u9012\u7ed9\u51fd\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5904\u7406JSON\u6570\u636e\uff1aJSON\u683c\u5f0f\u7684\u6570\u636e\u53ef\u4ee5\u76f4\u63a5\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u65b9\u4fbf\u8fdb\u884c\u6570\u636e\u89e3\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516b\u3001\u5b57\u5178\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u5b57\u5178\u7684\u6027\u80fd\u4f18\u5316\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u5b57\u5178\u6027\u80fd\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff1a\u5982\u679c\u952e\u7684\u7c7b\u578b\u662f\u53ef\u54c8\u5e0c\u7684\uff0c\u4f7f\u7528\u5b57\u5178\u662f\u975e\u5e38\u9ad8\u6548\u7684\u3002\u5426\u5219\uff0c\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5217\u8868\u6216\u96c6\u5408\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u590d\u5236\uff1a\u5728\u9700\u8981\u590d\u5236\u5b57\u5178\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u6d45\u62f7\u8d1d\u800c\u4e0d\u662f\u6df1\u62f7\u8d1d\uff0c\u4ee5\u51cf\u5c11\u5185\u5b58\u5f00\u9500\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528 <code>defaultdict<\/code>\uff1a<code>defaultdict<\/code> \u662f <code>collections<\/code> \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u5b83\u4e3a\u5b57\u5178\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c\u5de5\u5382\u51fd\u6570\uff0c\u53ef\u4ee5\u907f\u514d\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4f18\u5316\u952e\u7684\u9009\u62e9\uff1a\u9009\u62e9\u5408\u9002\u7684\u952e\u7c7b\u578b\u53ef\u4ee5\u63d0\u9ad8\u5b57\u5178\u7684\u67e5\u627e\u901f\u5ea6\u3002\u901a\u5e38\uff0c\u4f7f\u7528\u6574\u6570\u6216\u77ed\u5b57\u7b26\u4e32\u4f5c\u4e3a\u952e\u4f1a\u66f4\u5feb\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5b57\u5178\u53ca\u5176\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002\u638c\u63e1\u5b57\u5178\u7684\u4f7f\u7528\u6280\u5de7\u548c\u4f18\u5316\u7b56\u7565\uff0c\u5bf9\u4e8ePython\u5f00\u53d1\u8005\u800c\u8a00\u662f\u975e\u5e38\u91cd\u8981\u7684\u6280\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5b57\u5178\u5728Python\u4e2d\u6709\u4ec0\u4e48\u57fa\u672c\u7279\u6027\uff1f<\/strong><br \/>\u5b57\u5178\u662fPython\u4e2d\u7684\u4e00\u79cd\u5185\u7f6e\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\uff08key-value p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rs\uff09\u3002\u5b83\u7684\u7279\u70b9\u5305\u62ec\u65e0\u5e8f\u6027\u3001\u53ef\u53d8\u6027\u548c\u7d22\u5f15\u6027\u3002\u6bcf\u4e2a\u952e\u90fd\u662f\u552f\u4e00\u7684\uff0c\u4e14\u4e0d\u80fd\u91cd\u590d\u3002\u5b57\u5178\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u6765\u5b9a\u4e49\uff0c\u952e\u548c\u503c\u4e4b\u95f4\u7528\u5192\u53f7 <code>:<\/code> \u5206\u9694\uff0c\u591a\u4e2a\u952e\u503c\u5bf9\u4e4b\u95f4\u7528\u9017\u53f7 <code>,<\/code> \u5206\u9694\u3002\u4f8b\u5982\uff1a<code>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u6dfb\u52a0\u548c\u4fee\u6539\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u6216\u4fee\u6539\u5143\u7d20\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u8d4b\u503c\u4f1a\u6dfb\u52a0\u65b0\u952e\u503c\u5bf9\uff1b\u5982\u679c\u952e\u5df2\u5b58\u5728\uff0c\u5219\u4f1a\u66f4\u65b0\u5176\u5bf9\u5e94\u7684\u503c\u3002\u4f8b\u5982\uff1a<code>my_dict[&#39;age&#39;] = 30<\/code> \u4f1a\u66f4\u65b0 <code>age<\/code> \u7684\u503c\u4e3a30\u3002\u5982\u679c\u8981\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>my_dict[&#39;gender&#39;] = &#39;female&#39;<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u5220\u9664\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u5220\u9664\u5b57\u5178\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5\u6216\u8005 <code>pop()<\/code> \u65b9\u6cd5\u3002\u4f7f\u7528 <code>del my_dict[&#39;name&#39;]<\/code> \u53ef\u4ee5\u5220\u9664\u952e\u4e3a <code>name<\/code> \u7684\u5143\u7d20\uff0c\u800c <code>popped_value = my_dict.pop(&#39;age&#39;)<\/code> \u5219\u4f1a\u5220\u9664 <code>age<\/code> \u952e\uff0c\u5e76\u8fd4\u56de\u5176\u5bf9\u5e94\u7684\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5c1d\u8bd5\u5220\u9664\u4e0d\u5b58\u5728\u7684\u952e\u4f1a\u5f15\u53d1 <code>KeyError<\/code> \u5f02\u5e38\u3002<\/p>\n<p><strong>\u5982\u4f55\u904d\u5386Python\u5b57\u5178\u4e2d\u7684\u952e\u548c\u503c\uff1f<\/strong><br \/>\u904d\u5386\u5b57\u5178\u7684\u952e\u53ef\u4ee5\u4f7f\u7528 <code>for key in my_dict<\/code>\uff0c\u800c\u904d\u5386\u503c\u53ef\u4ee5\u4f7f\u7528 <code>for value in my_dict.values()<\/code>\u3002\u5982\u679c\u9700\u8981\u540c\u65f6\u904d\u5386\u952e\u548c\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>for key, value in my_dict.items()<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u952e\u503c\u5bf9\u7684\u5143\u7ec4\u3002\u5728\u5faa\u73af\u4e2d\u53ef\u4ee5\u5bf9\u6bcf\u4e2a\u952e\u6216\u503c\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\u3002\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u53ef\u4ee5\u662f\u4efb\u4f55\u4e0d\u53ef\u53d8\u7c7b\u578b\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001 [&hellip;]","protected":false},"author":3,"featured_media":988981,"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\/988963"}],"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=988963"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988963\/revisions"}],"predecessor-version":[{"id":988984,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988963\/revisions\/988984"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/988981"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=988963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=988963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=988963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}