{"id":987010,"date":"2024-12-27T07:49:48","date_gmt":"2024-12-26T23:49:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/987010.html"},"modified":"2024-12-27T07:49:50","modified_gmt":"2024-12-26T23:49:50","slug":"python-%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e5%8f%96%e9%94%ae%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/987010.html","title":{"rendered":"python \u5b57\u5178\u5982\u4f55\u53d6\u952e\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063322\/976db742-ef49-4b77-b1a0-a2bb0371c16e.webp\" alt=\"python \u5b57\u5178\u5982\u4f55\u53d6\u952e\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5b57\u5178\u7684\u952e\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u83b7\u53d6\u3001\u904d\u5386\u3001\u5220\u9664\u548c\u66f4\u65b0<\/strong>\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5\u83b7\u53d6\u952e\u3001<code>values()<\/code>\u65b9\u6cd5\u83b7\u53d6\u503c\u3001<code>items()<\/code>\u65b9\u6cd5\u83b7\u53d6\u952e\u503c\u5bf9\u3001\u4f7f\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u6216<code>get()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u7279\u5b9a\u952e\u7684\u503c\u3002<strong>\u4f7f\u7528<code>keys()<\/code>\u548c<code>values()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5206\u522b\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e\u548c\u6240\u6709\u503c<\/strong>\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7<code>dict.keys()<\/code>\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e\uff0c\u901a\u8fc7<code>dict.values()<\/code>\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u503c\u3002\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u4e86\u89e3\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\u662f\u5f88\u6709\u5fc5\u8981\u7684\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PYTHON\u5b57\u5178\u7684\u57fa\u7840\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>Python\u5b57\u5178\u662f\u4e00\u79cd\u5b58\u50a8\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u57fa\u672c\u64cd\u4f5c\u5305\u62ec\u521b\u5efa\u5b57\u5178\u3001\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3001\u66f4\u65b0\u5b57\u5178\u7b49\u3002\u521b\u5efa\u5b57\u5178\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7<code>{}<\/code>\u6765\u5305\u542b\u952e\u503c\u5bf9\uff0c\u6bcf\u4e2a\u952e\u503c\u5bf9\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\uff0c<code>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/code>\u3002\u5b57\u5178\u4e2d\u7684\u952e\u5fc5\u987b\u662f\u552f\u4e00\u4e14\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982\u5b57\u7b26\u4e32\u3001\u6574\u6570\u6216\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u8981\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u6765\u6307\u5b9a\u952e\uff0c\u4f8b\u5982<code>my_dict[&#39;name&#39;]<\/code>\u5c06\u8fd4\u56de<code>&#39;Alice&#39;<\/code>\u3002\u5982\u679c\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\uff0c\u5c06\u4f1a\u629b\u51fa<code>KeyError<\/code>\u5f02\u5e38\uff0c\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>get()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5141\u8bb8\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u4f8b\u5982<code>my_dict.get(&#39;name&#39;, &#39;Not Found&#39;)<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\uff0c\u4f8b\u5982<code>my_dict[&#39;age&#39;] = 26<\/code>\u5c06\u66f4\u65b0<code>age<\/code>\u7684\u503c\u4e3a<code>26<\/code>\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u8d4b\u503c\u8bed\u53e5\u5c06\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u904d\u5386\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u904d\u5386\u5b57\u5178\u662fPython\u5b57\u5178\u64cd\u4f5c\u4e2d\u975e\u5e38\u5e38\u89c1\u7684\u4efb\u52a1\uff0c\u901a\u5e38\u6709\u4e09\u79cd\u65b9\u5f0f\uff1a\u904d\u5386\u952e\u3001\u904d\u5386\u503c\u548c\u904d\u5386\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u904d\u5386\u952e<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e\uff0c\u7136\u540e\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u8fdb\u884c\u904d\u5386\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">for key in my_dict.keys():<\/p>\n<p>    print(key)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u904d\u5386\u503c<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>values()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u503c\uff0c\u7136\u540e\u8fdb\u884c\u904d\u5386\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">for value in my_dict.values():<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u904d\u5386\u952e\u503c\u5bf9<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>items()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e\u503c\u5bf9\uff0c<code>items()<\/code>\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u5143\u7ec4\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">for key, value in my_dict.items():<\/p>\n<p>    print(f&quot;Key: {key}, Value: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5b57\u5178\u7684\u5e38\u7528\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u5b57\u5178\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u5b57\u5178\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong><code>clear()<\/code><\/strong>\uff1a\u6e05\u7a7a\u5b57\u5178\u3002\u4f8b\u5982\uff0c<code>my_dict.clear()<\/code>\u5c06\u6e05\u7a7a\u5b57\u5178\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>copy()<\/code><\/strong>\uff1a\u8fd4\u56de\u5b57\u5178\u7684\u6d45\u590d\u5236\u3002\u4f8b\u5982\uff0c<code>new_dict = my_dict.copy()<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>fromkeys()<\/code><\/strong>\uff1a\u4f7f\u7528\u6307\u5b9a\u7684\u952e\u548c\u53ef\u9009\u7684\u9ed8\u8ba4\u503c\u521b\u5efa\u4e00\u4e2a\u65b0\u5b57\u5178\u3002\u4f8b\u5982\uff0c<code>new_dict = dict.fromkeys([&#39;name&#39;, &#39;age&#39;], &#39;unknown&#39;)<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>pop()<\/code><\/strong>\uff1a\u5220\u9664\u6307\u5b9a\u952e\u7684\u503c\u5e76\u8fd4\u56de\u8be5\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u4f1a\u629b\u51fa<code>KeyError<\/code>\u3002\u4f8b\u5982\uff0c<code>value = my_dict.pop(&#39;name&#39;)<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>popitem()<\/code><\/strong>\uff1a\u5220\u9664\u5e76\u8fd4\u56de\u5b57\u5178\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u5b57\u5178\u662f\u65e0\u5e8f\u7684\uff0c\u5220\u9664\u7684\u662f\u4efb\u610f\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff0c<code>key, value = my_dict.popitem()<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>setdefault()<\/code><\/strong>\uff1a\u8fd4\u56de\u6307\u5b9a\u952e\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\u5219\u63d2\u5165\u952e\u5e76\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff0c<code>value = my_dict.setdefault(&#39;name&#39;, &#39;unknown&#39;)<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>update()<\/code><\/strong>\uff1a\u5c06\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u66f4\u65b0\u5230\u5f53\u524d\u5b57\u5178\u4e2d\u3002\u4f8b\u5982\uff0c<code>my_dict.update({&#39;age&#39;: 30})<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5b57\u5178\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>Python\u5b57\u5178\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u5e38\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b58\u50a8\u548c\u67e5\u8be2\u914d\u7f6e<\/strong>\uff1a\u5b57\u5178\u662f\u5b58\u50a8\u548c\u67e5\u8be2\u914d\u7f6e\u4fe1\u606f\u7684\u7406\u60f3\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u901a\u8fc7\u952e\u5feb\u901f\u8bbf\u95ee\u76f8\u5173\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8ba1\u6570\u5668<\/strong>\uff1a\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u7b80\u5355\u7684\u8ba1\u6570\u529f\u80fd\uff0c\u6bd4\u5982\u7edf\u8ba1\u5b57\u7b26\u6216\u5355\u8bcd\u51fa\u73b0\u7684\u6b21\u6570\u3002\u53ef\u4ee5\u901a\u8fc7<code>collections<\/code>\u6a21\u5757\u4e2d\u7684<code>Counter<\/code>\u7c7b\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u8ba1\u6570\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6620\u5c04\u5173\u7cfb<\/strong>\uff1a\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u4e0d\u540c\u6570\u636e\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u6bd4\u5982\u5c06\u7528\u6237ID\u6620\u5c04\u5230\u7528\u6237\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f13\u5b58\u673a\u5236<\/strong>\uff1a\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u7b80\u5355\u7684\u7f13\u5b58\u673a\u5236\uff0c\u901a\u8fc7\u7f13\u5b58\u5df2\u7ecf\u8ba1\u7b97\u8fc7\u7684\u503c\uff0c\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5b57\u5178\u7684\u6027\u80fd\u548c\u4f18\u5316<\/p>\n<\/p>\n<p><p>Python\u5b57\u5178\u662f\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\u7684\uff0c\u5176\u67e5\u627e\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u901a\u5e38\u4e3aO(1)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u7136\u800c\uff0c\u5b57\u5178\u7684\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u4ee5\u4e0b\u56e0\u7d20\u7684\u5f71\u54cd\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u54c8\u5e0c\u51b2\u7a81<\/strong>\uff1a\u5f53\u591a\u4e2a\u952e\u7684\u54c8\u5e0c\u503c\u76f8\u540c\u65f6\uff0c\u4f1a\u5bfc\u81f4\u54c8\u5e0c\u51b2\u7a81\uff0c\u4ece\u800c\u5f71\u54cd\u67e5\u627e\u6548\u7387\u3002\u4e3a\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\uff0c\u5e94\u5c3d\u91cf\u9009\u62e9\u5408\u9002\u7684\u54c8\u5e0c\u51fd\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u5178\u7684\u5927\u5c0f<\/strong>\uff1a\u5b57\u5178\u4f1a\u5728\u5185\u5b58\u4e2d\u5360\u7528\u4e00\u5b9a\u7a7a\u95f4\uff0c\u5f53\u5b57\u5178\u8fc7\u5927\u65f6\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u5e94\u6ce8\u610f\u63a7\u5236\u5b57\u5178\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u5178\u7684\u52a8\u6001\u6269\u5c55<\/strong>\uff1a\u5b57\u5178\u5728\u52a8\u6001\u6269\u5c55\u65f6\uff0c\u4f1a\u91cd\u65b0\u5206\u914d\u5185\u5b58\u5e76\u91cd\u65b0\u54c8\u5e0c\u6240\u6709\u5143\u7d20\u3002\u8fd9\u4e00\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u9891\u7e41\u7684\u5b57\u5178\u52a8\u6001\u6269\u5c55\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e3a\u4f18\u5316\u5b57\u5178\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u7b56\u7565\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff08\u5982\u5217\u8868\u6216\u96c6\u5408\uff09\u6765\u66ff\u4ee3\u5b57\u5178\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u751f\u6210\u5668<\/strong>\uff1a\u5728\u9700\u8981\u5927\u91cf\u8ba1\u7b97\u6216\u6570\u636e\u5904\u7406\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\u548c\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u64cd\u4f5c<\/strong>\uff1a\u5728\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u63d2\u5165\u3001\u5220\u9664\u548c\u66f4\u65b0\u64cd\u4f5c\uff0c\u4ece\u800c\u51cf\u5c11\u6027\u80fd\u5f00\u9500\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u516d\u3001\u5b57\u5178\u7684\u6269\u5c55\u548c\u8fdb\u9636<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u952e\u503c\u5bf9\u5b58\u50a8\u548c\u67e5\u627e\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u9ad8\u7ea7\u6280\u5de7\u548c\u5e93\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5feb\u901f\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\uff0c\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u8ba1\u6570\u5b57\u5178\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">text = &quot;hello&quot;<\/p>\n<p>char_count = {char: text.count(char) for char in set(text)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong><code>defaultdict<\/code><\/strong>\uff1a<code>defaultdict<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u5141\u8bb8\u4e3a\u5b57\u5178\u4e2d\u7684\u952e\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u4ece\u800c\u907f\u514d<code>KeyError<\/code>\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<p>my_dict = defaultdict(int)<\/p>\n<p>my_dict[&#39;age&#39;] += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\n<p><strong><code>OrderedDict<\/code><\/strong>\uff1a<code>OrderedDict<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u4fdd\u7559\u4e86\u5b57\u5178\u4e2d\u952e\u503c\u5bf9\u7684\u63d2\u5165\u987a\u5e8f\u3002\u5728Python 3.7\u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\uff0c\u6807\u51c6\u5b57\u5178\u4e5f\u4fdd\u7559\u4e86\u63d2\u5165\u987a\u5e8f\uff0c\u4f46<code>OrderedDict<\/code>\u4ecd\u7136\u63d0\u4f9b\u4e86\u4e00\u4e9b\u989d\u5916\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>Ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nMap<\/code><\/strong>\uff1a<code>ChainMap<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u63d0\u4f9b\u4e86\u5c06\u591a\u4e2a\u5b57\u5178\u5408\u5e76\u4e3a\u4e00\u4e2a\u89c6\u56fe\u7684\u65b9\u6cd5\u3002<code>ChainMap<\/code>\u4e0d\u4f1a\u5b9e\u9645\u5408\u5e76\u5b57\u5178\uff0c\u800c\u662f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5408\u5e76\u89c6\u56fe\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>Counter<\/code><\/strong>\uff1a<code>Counter<\/code>\u662f<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5b57\u5178\u5b50\u7c7b\uff0c\u7528\u4e8e\u8ba1\u6570\u53ef\u54c8\u5e0c\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Counter<\/code>\u5feb\u901f\u7edf\u8ba1\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>text = &quot;hello&quot;<\/p>\n<p>char_count = Counter(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9Python\u5b57\u5178\u7684\u6df1\u5165\u4e86\u89e3\u548c\u5e94\u7528\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528\u5176\u5f3a\u5927\u7684\u529f\u80fd\u6765\u89e3\u51b3\u5404\u79cd\u5b9e\u9645\u95ee\u9898\u3002\u65e0\u8bba\u662f\u5728\u6570\u636e\u5b58\u50a8\u3001\u67e5\u627e\u8fd8\u662f\u8ba1\u6570\u7b49\u4efb\u52a1\u4e2d\uff0c\u5b57\u5178\u90fd\u80fd\u63d0\u4f9b\u9ad8\u6548\u548c\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u5b57\u5178\uff0c\u63d0\u9ad8\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\u5b57\u5178\u4e2d\u83b7\u53d6\u7279\u5b9a\u952e\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u65b9\u62ec\u53f7\u6216<code>get()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u5b57\u5178\u4e2d\u7279\u5b9a\u952e\u7684\u503c\u3002\u4f7f\u7528\u65b9\u62ec\u53f7\u7684\u8bed\u6cd5\u662f<code>dict[key]<\/code>\uff0c\u5982\u679c\u952e\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u76f8\u5e94\u7684\u503c\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1<code>KeyError<\/code>\u3002\u800c\u4f7f\u7528<code>get()<\/code>\u65b9\u6cd5\u5219\u66f4\u4e3a\u5b89\u5168\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>dict.get(key, default)<\/code>\u6765\u83b7\u53d6\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de<code>default<\/code>\u503c\uff0c\u907f\u514d\u4e86\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u4f55\u904d\u5386Python\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u548c\u503c\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>items()<\/code>\u65b9\u6cd5\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u548c\u503c\u3002\u901a\u8fc7<code>for key, value in dict.items():<\/code>\u7684\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u540c\u65f6\u8bbf\u95ee\u6bcf\u4e00\u4e2a\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u9700\u8981\u540c\u65f6\u5904\u7406\u952e\u548c\u503c\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u952e\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>if key in dict:<\/code>\u6765\u9a8c\u8bc1\u952e\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u3002\u5982\u679c\u5b58\u5728\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5904\u7406\u76f8\u5173\u7684\u503c\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u5982\u6dfb\u52a0\u8be5\u952e\u6216\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b57\u5178\u7684\u952e\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u83b7\u53d6\u3001\u904d\u5386\u3001\u5220\u9664\u548c\u66f4\u65b0\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528keys()\u65b9\u6cd5\u83b7\u53d6 [&hellip;]","protected":false},"author":3,"featured_media":987013,"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\/987010"}],"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=987010"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/987010\/revisions"}],"predecessor-version":[{"id":987016,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/987010\/revisions\/987016"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/987013"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=987010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=987010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=987010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}