{"id":1172207,"date":"2025-01-15T16:46:04","date_gmt":"2025-01-15T08:46:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172207.html"},"modified":"2025-01-15T16:46:06","modified_gmt":"2025-01-15T08:46:06","slug":"python%e5%a6%82%e4%bd%95%e6%89%be%e5%88%b0%e5%ad%97%e5%85%b8%e9%87%8c%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1172207.html","title":{"rendered":"python\u5982\u4f55\u627e\u5230\u5b57\u5178\u91cc\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074336\/6bc6d7ba-b3b7-406c-96a5-a777d41334e5.webp\" alt=\"python\u5982\u4f55\u627e\u5230\u5b57\u5178\u91cc\u5143\u7d20\" \/><\/p>\n<p><p> <strong>Python\u627e\u5230\u5b57\u5178\u91cc\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u901a\u8fc7\u952e\u67e5\u627e\u3001\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u3001\u904d\u5386\u5b57\u5178\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b49\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u901a\u8fc7\u952e\u67e5\u627e\u548c\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u3002<\/strong> \u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u901a\u8fc7\u952e\u67e5\u627e\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u952e\u67e5\u627e\u662f\u6307\u76f4\u63a5\u4f7f\u7528\u952e\u6765\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u5178 <code>my_dict<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>my_dict[key]<\/code> \u6765\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u4f1a\u629b\u51fa <code>KeyError<\/code> \u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>get<\/code> \u65b9\u6cd5\uff0c\u5982 <code>my_dict.get(key, default)<\/code>\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de <code>default<\/code> \u503c\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u901a\u8fc7\u952e\u67e5\u627e<\/strong><\/h2>\n<p><p>Python\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u5feb\u901f\u67e5\u627e\u5bf9\u5e94\u7684\u503c\u3002\u8fd9\u79cd\u67e5\u627e\u65b9\u5f0f\u6548\u7387\u5f88\u9ad8\uff0c\u56e0\u4e3a\u5b57\u5178\u662f\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\u7684\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u76f4\u63a5\u901a\u8fc7\u952e\u67e5\u627e<\/h3>\n<\/p>\n<p><p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u952e\u6765\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u5178 <code>my_dict<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>my_dict[key]<\/code> \u6765\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>value = my_dict[&#39;name&#39;]<\/p>\n<p>print(value)  # \u8f93\u51fa: Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u800c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u4f1a\u629b\u51fa <code>KeyError<\/code> \u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u786e\u4fdd\u952e\u786e\u5b9e\u5b58\u5728\u6216\u8005\u4f7f\u7528\u66f4\u5b89\u5168\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u4f7f\u7528 get \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d <code>KeyError<\/code> \u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>get<\/code> \u65b9\u6cd5\u3002<code>get<\/code> \u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>value = my_dict.get(&#39;name&#39;, &#39;Unknown&#39;)<\/p>\n<p>print(value)  # \u8f93\u51fa: Alice<\/p>\n<p>value = my_dict.get(&#39;gender&#39;, &#39;Unknown&#39;)<\/p>\n<p>print(value)  # \u8f93\u51fa: Unknown<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>get<\/code> \u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u907f\u514d\u5f02\u5e38\uff0c\u8fd8\u80fd\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5<\/strong><\/h2>\n<p><p>\u9664\u4e86\u76f4\u63a5\u901a\u8fc7\u952e\u67e5\u627e\uff0cPython\u5b57\u5178\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u8f85\u52a9\u67e5\u627e\u548c\u64cd\u4f5c\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>2.1 keys \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>keys<\/code> \u65b9\u6cd5\u8fd4\u56de\u5b57\u5178\u4e2d\u6240\u6709\u952e\u7684\u89c6\u56fe\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6765\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>keys = my_dict.keys()<\/p>\n<p>print(keys)  # \u8f93\u51fa: dict_keys([&#39;name&#39;, &#39;age&#39;, &#39;city&#39;])<\/p>\n<p>if &#39;name&#39; in keys:<\/p>\n<p>    print(&quot;Key &#39;name&#39; exists in the dictionary&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 values \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>values<\/code> \u65b9\u6cd5\u8fd4\u56de\u5b57\u5178\u4e2d\u6240\u6709\u503c\u7684\u89c6\u56fe\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6765\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>values = my_dict.values()<\/p>\n<p>print(values)  # \u8f93\u51fa: dict_values([&#39;Alice&#39;, 25, &#39;New York&#39;])<\/p>\n<p>for value in values:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.3 items \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>items<\/code> \u65b9\u6cd5\u8fd4\u56de\u5b57\u5178\u4e2d\u6240\u6709\u952e\u503c\u5bf9\u7684\u89c6\u56fe\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6765\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>items = my_dict.items()<\/p>\n<p>print(items)  # \u8f93\u51fa: dict_items([(&#39;name&#39;, &#39;Alice&#39;), (&#39;age&#39;, 25), (&#39;city&#39;, &#39;New York&#39;)])<\/p>\n<p>for key, value in items:<\/p>\n<p>    print(f&#39;Key: {key}, Value: {value}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u904d\u5386\u5b57\u5178<\/strong><\/h2>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u904d\u5386\u6574\u4e2a\u5b57\u5178\u6765\u67e5\u627e\u67d0\u4e2a\u5143\u7d20\uff0c\u6216\u8005\u5bf9\u5b57\u5178\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u904d\u5386\u952e<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\uff0c\u7136\u540e\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>for key in my_dict:<\/p>\n<p>    value = my_dict[key]<\/p>\n<p>    print(f&#39;Key: {key}, Value: {value}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u904d\u5386\u952e\u503c\u5bf9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>items<\/code> \u65b9\u6cd5\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u952e\u548c\u503c\uff0c\u65b9\u4fbf\u6211\u4eec\u904d\u5386\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>for key, value in my_dict.items():<\/p>\n<p>    print(f&#39;Key: {key}, Value: {value}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u5b57\u5178\u3002\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8fc7\u6ee4\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u6216\u8005\u5bf9\u5b57\u5178\u4e2d\u7684\u503c\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u8fc7\u6ee4\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u5178\uff0c\u60f3\u8981\u8fc7\u6ee4\u6389\u67d0\u4e9b\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>filtered_dict = {key: value for key, value in my_dict.items() if key != &#39;age&#39;}<\/p>\n<p>print(filtered_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u4fee\u6539\u5b57\u5178\u503c<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6765\u5bf9\u5b57\u5178\u4e2d\u7684\u503c\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>modified_dict = {key: (value.upper() if isinstance(value, str) else value) for key, value in my_dict.items()}<\/p>\n<p>print(modified_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;ALICE&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;NEW YORK&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u5b57\u5178\u7684\u5e38\u89c1\u64cd\u4f5c<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9664\u4e86\u67e5\u627e\u5b57\u5178\u4e2d\u7684\u5143\u7d20\uff0c\u6211\u4eec\u8fd8\u7ecf\u5e38\u9700\u8981\u5bf9\u5b57\u5178\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u6dfb\u52a0\u3001\u66f4\u65b0\u3001\u5220\u9664\u5143\u7d20\u7b49\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u6dfb\u52a0\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u5143\u7d20\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u6307\u5b9a\u65b0\u7684\u952e\u548c\u503c\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>my_dict[&#39;city&#39;] = &#39;New York&#39;<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2 \u66f4\u65b0\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u952e\u6765\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/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}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.3 \u5220\u9664\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u5173\u952e\u5b57\u6216\u8005 <code>pop<\/code> \u65b9\u6cd5\u6765\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/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;: 25}<\/p>\n<p>age = my_dict.pop(&#39;age&#39;)<\/p>\n<p>print(age)  # \u8f93\u51fa: 25<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.4 \u6e05\u7a7a\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>clear<\/code> \u65b9\u6cd5\u53ef\u4ee5\u6e05\u7a7a\u5b57\u5178\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>my_dict.clear()<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u5b57\u5178\u7684\u9ad8\u7ea7\u7528\u6cd5<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u5b57\u5178\u53ef\u4ee5\u4e0e\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u7ed3\u5408\u4f7f\u7528\uff0c\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>6.1 \u5b57\u5178\u5d4c\u5957<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u5d4c\u5957\uff0c\u5373\u5b57\u5178\u7684\u503c\u53ef\u4ee5\u662f\u53e6\u4e00\u4e2a\u5b57\u5178\u3002\u8fd9\u5728\u5904\u7406\u591a\u5c42\u6b21\u7684\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_dict = {<\/p>\n<p>    &#39;person1&#39;: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25},<\/p>\n<p>    &#39;person2&#39;: {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30}<\/p>\n<p>}<\/p>\n<p>print(nested_dict[&#39;person1&#39;][&#39;name&#39;])  # \u8f93\u51fa: Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.2 \u4f7f\u7528 <code>defaultdict<\/code><\/h3>\n<\/p>\n<p><p><code>collections<\/code> \u6a21\u5757\u4e2d\u7684 <code>defaultdict<\/code> \u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u503c\u673a\u5236\uff0c\u53ef\u4ee5\u7b80\u5316\u67d0\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>my_dict = defaultdict(int)<\/p>\n<p>my_dict[&#39;count&#39;] += 1<\/p>\n<p>print(my_dict[&#39;count&#39;])  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.3 \u4f7f\u7528 <code>Counter<\/code><\/h3>\n<\/p>\n<p><p><code>Counter<\/code> \u662f <code>collections<\/code> \u6a21\u5757\u4e2d\u7684\u53e6\u4e00\u4e2a\u6709\u7528\u7684\u7c7b\uff0c\u7528\u4e8e\u8ba1\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;]<\/p>\n<p>counter = Counter(my_list)<\/p>\n<p>print(counter)  # \u8f93\u51fa: Counter({&#39;apple&#39;: 2, &#39;banana&#39;: 1, &#39;orange&#39;: 1})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u5b57\u5178\u7684\u6027\u80fd<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u5b57\u5178\u7684\u6027\u80fd\u975e\u5e38\u91cd\u8981\u3002Python\u5b57\u5178\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u901a\u5e38\u5177\u6709\u4f18\u79c0\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>7.1 \u67e5\u627e\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e\u5b57\u5178\u4f7f\u7528\u54c8\u5e0c\u8868\uff0c\u67e5\u627e\u952e\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(1)\u3002\u8fd9\u610f\u5473\u7740\u67e5\u627e\u64cd\u4f5c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u975e\u5e38\u5feb\u3002<\/p>\n<\/p>\n<p><h3>7.2 \u63d2\u5165\u548c\u5220\u9664\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u662f O(1)\u3002\u7136\u800c\uff0c\u5f53\u54c8\u5e0c\u8868\u9700\u8981\u6269\u5c55\u65f6\uff0c\u63d2\u5165\u64cd\u4f5c\u7684\u6027\u80fd\u53ef\u80fd\u4f1a\u6709\u6240\u4e0b\u964d\u3002<\/p>\n<\/p>\n<p><h3>7.3 \u5185\u5b58\u6d88\u8017<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u5185\u5b58\u6d88\u8017\u76f8\u5bf9\u8f83\u5927\uff0c\u56e0\u4e3a\u9700\u8981\u5b58\u50a8\u54c8\u5e0c\u8868\u7684\u7ed3\u6784\u548c\u6240\u6709\u952e\u503c\u5bf9\u3002\u5728\u5904\u7406\u7279\u522b\u5927\u7684\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u5185\u5b58\u6d88\u8017\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u5b57\u5178\u7684\u5b9e\u9645\u5e94\u7528<\/strong><\/h2>\n<p><p>\u5b57\u5178\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u5e7f\u6cdb\uff0c\u51e0\u4e4e\u6bcf\u4e00\u4e2aPython\u9879\u76ee\u90fd\u4f1a\u4f7f\u7528\u5230\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>8.1 \u8ba1\u6570<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5e38\u7528\u4e8e\u8ba1\u6570\u64cd\u4f5c\uff0c\u4f8b\u5982\u7edf\u8ba1\u6587\u672c\u4e2d\u5355\u8bcd\u7684\u9891\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;hello world hello&quot;<\/p>\n<p>word_count = {}<\/p>\n<p>for word in text.split():<\/p>\n<p>    if word in word_count:<\/p>\n<p>        word_count[word] += 1<\/p>\n<p>    else:<\/p>\n<p>        word_count[word] = 1<\/p>\n<p>print(word_count)  # \u8f93\u51fa: {&#39;hello&#39;: 2, &#39;world&#39;: 1}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8.2 \u914d\u7f6e\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5e38\u7528\u4e8e\u5b58\u50a8\u914d\u7f6e\u53c2\u6570\uff0c\u4f8b\u5982\u5e94\u7528\u7a0b\u5e8f\u7684\u8bbe\u7f6e\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>print(config[&#39;host&#39;])  # \u8f93\u51fa: localhost<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8.3 \u6570\u636e\u7ed3\u6784\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u8f6c\u6362\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">names = [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;]<\/p>\n<p>ages = [25, 30, 35]<\/p>\n<p>people = {name: age for name, age in zip(names, ages)}<\/p>\n<p>print(people)  # \u8f93\u51fa: {&#39;Alice&#39;: 25, &#39;Bob&#39;: 30, &#39;Charlie&#39;: 35}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e5d\u3001\u5b57\u5178\u7684\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/strong><\/h2>\n<p><p>\u5728\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h3>9.1 KeyError \u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5f53\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c\u4f1a\u629b\u51fa <code>KeyError<\/code> \u5f02\u5e38\u3002\u53ef\u4ee5\u4f7f\u7528 <code>get<\/code> \u65b9\u6cd5\u6765\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;}<\/p>\n<p>value = my_dict.get(&#39;age&#39;, &#39;Unknown&#39;)<\/p>\n<p>print(value)  # \u8f93\u51fa: Unknown<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>9.2 \u5b57\u5178\u7684\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u65e0\u5e8f\u7684\uff0c\u5982\u679c\u9700\u8981\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>sorted<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;b&#39;: 2, &#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<p>sorted_dict = dict(sorted(my_dict.items()))<\/p>\n<p>print(sorted_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>9.3 \u5408\u5e76\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>update<\/code> \u65b9\u6cd5\u6765\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;name&#39;: &#39;Alice&#39;}<\/p>\n<p>dict2 = {&#39;age&#39;: 25}<\/p>\n<p>dict1.update(dict2)<\/p>\n<p>print(dict1)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>9.4 \u6df1\u62f7\u8d1d\u548c\u6d45\u62f7\u8d1d<\/h3>\n<\/p>\n<p><p>\u5728\u590d\u5236\u5b57\u5178\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6df1\u62f7\u8d1d\u548c\u6d45\u62f7\u8d1d\u7684\u533a\u522b\u3002\u6d45\u62f7\u8d1d\u53ea\u590d\u5236\u5b57\u5178\u7684\u5f15\u7528\uff0c\u800c\u6df1\u62f7\u8d1d\u4f1a\u590d\u5236\u6240\u6709\u5d4c\u5957\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>original = {&#39;name&#39;: &#39;Alice&#39;, &#39;det<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls&#39;: {&#39;age&#39;: 25}}<\/p>\n<p>shallow_copy = original.copy()<\/p>\n<p>deep_copy = copy.deepcopy(original)<\/p>\n<p>original[&#39;details&#39;][&#39;age&#39;] = 30<\/p>\n<p>print(shallow_copy[&#39;details&#39;][&#39;age&#39;])  # \u8f93\u51fa: 30<\/p>\n<p>print(deep_copy[&#39;details&#39;][&#39;age&#39;])     # \u8f93\u51fa: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>Python\u5b57\u5178\u662f\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u7f16\u7a0b\u4efb\u52a1\u3002\u4ece\u57fa\u672c\u7684\u67e5\u627e\u64cd\u4f5c\u5230\u9ad8\u7ea7\u7684\u5e94\u7528\u573a\u666f\uff0c\u5b57\u5178\u90fd\u80fd\u63d0\u4f9b\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5728\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u6027\u80fd\u3002\u901a\u8fc7\u638c\u63e1\u5b57\u5178\u7684\u5404\u79cd\u7528\u6cd5\u548c\u6280\u5de7\uff0c\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u64cd\u4f5c\u590d\u6742\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\u5b57\u5178\u4e2d\u67e5\u627e\u7279\u5b9a\u7684\u952e\u6216\u503c\uff1f<\/strong><br \/>\u5728Python\u5b57\u5178\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u67e5\u627e\u7279\u5b9a\u7684\u952e\u6216\u503c\u3002\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u53ef\u4ee5\u68c0\u67e5\u67d0\u4e2a\u952e\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u3002\u4f8b\u5982\uff0c<code>if &#39;key&#39; in my_dict:<\/code>\u53ef\u4ee5\u5224\u65ad\u952e&#39;key&#39;\u662f\u5426\u5728<code>my_dict<\/code>\u4e2d\u3002\u82e5\u8981\u67e5\u627e\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216<code>values()<\/code>\u65b9\u6cd5\uff0c\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5982\u679c\u5b57\u5178\u4e2d\u6ca1\u6709\u627e\u5230\u6240\u9700\u7684\u5143\u7d20\uff0c\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5f53\u5728\u5b57\u5178\u4e2d\u67e5\u627e\u5143\u7d20\u672a\u679c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u6765\u907f\u514d\u7a0b\u5e8f\u51fa\u9519\u3002\u4f7f\u7528<code>dict.get(key, default)<\/code>\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de\u7684\u5c06\u662f\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\uff0c\u800c\u4e0d\u662f\u629b\u51fa\u5f02\u5e38\u3002\u4f8b\u5982\uff0c<code>my_dict.get(&#39;key&#39;, &#39;default_value&#39;)<\/code>\u5c06\u8fd4\u56de&#39;default_value&#39;\uff0c\u5982\u679c&#39;key&#39;\u5728\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u3002<\/p>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\u5982\u4f55\u9ad8\u6548\u67e5\u627e\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u82e5\u9700\u8981\u540c\u65f6\u67e5\u627e\u591a\u4e2a\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>in<\/code>\u8fd0\u7b97\u7b26\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5feb\u901f\u68c0\u67e5\u591a\u4e2a\u952e\u7684\u5b58\u5728\u6027\uff0c\u4f8b\u5982\uff1a<code>[key for key in keys_list if key in my_dict]<\/code>\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u96c6\u5408\u64cd\u4f5c\u80fd\u591f\u63d0\u5347\u67e5\u627e\u6548\u7387\uff0c\u5c24\u5176\u5728\u5904\u7406\u5927\u578b\u5b57\u5178\u65f6\uff0c\u80fd\u663e\u8457\u51cf\u5c11\u67e5\u627e\u65f6\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u627e\u5230\u5b57\u5178\u91cc\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u901a\u8fc7\u952e\u67e5\u627e\u3001\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u3001\u904d\u5386\u5b57\u5178\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b49\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u901a\u8fc7 [&hellip;]","protected":false},"author":3,"featured_media":1172212,"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\/1172207"}],"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=1172207"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172207\/revisions"}],"predecessor-version":[{"id":1172214,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172207\/revisions\/1172214"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172212"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}