{"id":1139879,"date":"2025-01-08T22:14:40","date_gmt":"2025-01-08T14:14:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139879.html"},"modified":"2025-01-08T22:14:43","modified_gmt":"2025-01-08T14:14:43","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e5%85%b8%e4%b8%ad%e7%9a%84%e5%80%bc%e8%bf%94%e5%9b%9e%e9%94%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139879.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u5b57\u5178\u4e2d\u7684\u503c\u8fd4\u56de\u952e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103028\/5e9b901a-9d18-40c5-bf31-fdb62f83dd8e.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u5b57\u5178\u4e2d\u7684\u503c\u8fd4\u56de\u952e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u5178\u4e2d\u7684\u503c\u8fd4\u56de\u4e3a\u952e\u7684\u64cd\u4f5c\u4e3b\u8981\u6d89\u53ca\u5b57\u5178\u7684\u53cd\u8f6c\u3002<\/strong> \u8fd9\u79cd\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u3001<code>zip()<\/code> \u51fd\u6570\u4ee5\u53ca<code>dict<\/code>\u6784\u9020\u51fd\u6570\u7b49\u3002<strong>\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u548c<code>zip()<\/code>\u51fd\u6570<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u5b9e\u73b0\u8fd9\u4e2a\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u5b57\u5178\u7684\u53cd\u8f6c\u3002\u5b57\u5178\u63a8\u5bfc\u7684\u8bed\u6cd5\u4e0e\u5217\u8868\u63a8\u5bfc\u7c7b\u4f3c\uff0c\u4f46\u662f\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>reversed_dict = {v: k for k, v in original_dict.items()}<\/p>\n<p>print(reversed_dict)  # \u8f93\u51fa: {1: &#39;a&#39;, 2: &#39;b&#39;, 3: &#39;c&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u5176\u4e2d\u539f\u59cb\u5b57\u5178\u7684\u503c\u6210\u4e3a\u65b0\u5b57\u5178\u7684\u952e\uff0c\u800c\u539f\u59cb\u5b57\u5178\u7684\u952e\u6210\u4e3a\u65b0\u5b57\u5178\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>zip()<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>zip()<\/code> \u51fd\u6570\u53ef\u4ee5\u5c06\u4e24\u4e2a\u5217\u8868\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\u7684\u5217\u8868\uff0c\u8fd9\u5bf9\u4e8e\u53cd\u8f6c\u5b57\u5178\u4e5f\u975e\u5e38\u6709\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u5178\u7684\u952e\u548c\u503c\u5206\u522b\u63d0\u53d6\u5230\u4e24\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528 <code>zip()<\/code> \u51fd\u6570\u5c06\u5b83\u4eec\u53cd\u8f6c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>reversed_dict = dict(zip(original_dict.values(), original_dict.keys()))<\/p>\n<p>print(reversed_dict)  # \u8f93\u51fa: {1: &#39;a&#39;, 2: &#39;b&#39;, 3: &#39;c&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u63d0\u53d6\u4e86\u539f\u59cb\u5b57\u5178\u7684\u503c\u548c\u952e\uff0c\u7136\u540e\u4f7f\u7528 <code>zip()<\/code> \u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u5176\u4e2d\u503c\u548c\u952e\u7684\u4f4d\u7f6e\u88ab\u4ea4\u6362\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>dict<\/code> \u6784\u9020\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>dict<\/code> \u6784\u9020\u51fd\u6570\u6765\u5b9e\u73b0\u5b57\u5178\u7684\u53cd\u8f6c\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u5b57\u5178\u63a8\u5bfc\u7c7b\u4f3c\uff0c\u4f46\u7a0d\u5fae\u663e\u5f97\u7e41\u7410\u4e00\u4e9b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>reversed_dict = dict((v, k) for k, v in original_dict.items())<\/p>\n<p>print(reversed_dict)  # \u8f93\u51fa: {1: &#39;a&#39;, 2: &#39;b&#39;, 3: &#39;c&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u6027\u80fd\u4e0a\u548c\u5b57\u5178\u63a8\u5bfc\u5dee\u4e0d\u591a\uff0c\u4f46\u5728\u8bed\u6cd5\u4e0a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u91cd\u590d\u503c\u7684\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b57\u5178\u7684\u503c\u53ef\u80fd\u5e76\u4e0d\u552f\u4e00\u3002\u5982\u679c\u539f\u59cb\u5b57\u5178\u4e2d\u6709\u91cd\u590d\u7684\u503c\uff0c\u53cd\u8f6c\u5b57\u5178\u7684\u64cd\u4f5c\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_dict = {&#39;a&#39;: 1, &#39;b&#39;: 1, &#39;c&#39;: 2}<\/p>\n<p>reversed_dict = {}<\/p>\n<p>for k, v in original_dict.items():<\/p>\n<p>    if v in reversed_dict:<\/p>\n<p>        if isinstance(reversed_dict[v], list):<\/p>\n<p>            reversed_dict[v].append(k)<\/p>\n<p>        else:<\/p>\n<p>            reversed_dict[v] = [reversed_dict[v], k]<\/p>\n<p>    else:<\/p>\n<p>        reversed_dict[v] = k<\/p>\n<p>print(reversed_dict)  # \u8f93\u51fa: {1: [&#39;a&#39;, &#39;b&#39;], 2: &#39;c&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a <code>for<\/code> \u5faa\u73af\u6765\u904d\u5386\u539f\u59cb\u5b57\u5178\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u503c\u662f\u5426\u5df2\u7ecf\u5728\u65b0\u7684\u5b57\u5178\u4e2d\u3002\u5982\u679c\u503c\u5df2\u7ecf\u5b58\u5728\uff0c\u6211\u4eec\u5c06\u65b0\u7684\u952e\u8ffd\u52a0\u5230\u5217\u8868\u4e2d\uff1b\u5426\u5219\uff0c\u6211\u4eec\u5c06\u952e\u76f4\u63a5\u6dfb\u52a0\u5230\u65b0\u7684\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5c06\u5b57\u5178\u4e2d\u7684\u503c\u8fd4\u56de\u4e3a\u952e\u7684\u64cd\u4f5c\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u6709\u8bb8\u591a\u5e94\u7528\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u6709\u4e00\u4e2a\u503c\u9700\u8981\u5feb\u901f\u67e5\u627e\u5230\u5bf9\u5e94\u7684\u952e\u65f6\uff0c\u8fd9\u79cd\u64cd\u4f5c\u5c31\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u53cd\u8f6c<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5c06\u5b57\u5178\u6570\u636e\u8fdb\u884c\u53cd\u8f6c\uff0c\u4ee5\u4fbf\u6839\u636e\u503c\u6765\u67e5\u627e\u5bf9\u5e94\u7684\u952e\u3002\u6bd4\u5982\u5728\u5904\u7406\u5206\u7c7b\u6807\u7b7e\u65f6\uff0c\u6709\u65f6\u9700\u8981\u6839\u636e\u6807\u7b7e\u7684ID\u6765\u67e5\u627e\u6807\u7b7e\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">label_to_id = {&#39;cat&#39;: 1, &#39;dog&#39;: 2, &#39;mouse&#39;: 3}<\/p>\n<p>id_to_label = {v: k for k, v in label_to_id.items()}<\/p>\n<p>print(id_to_label)  # \u8f93\u51fa: {1: &#39;cat&#39;, 2: &#39;dog&#39;, 3: &#39;mouse&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5feb\u901f\u67e5\u627e<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u67d0\u4e2a\u503c\u5feb\u901f\u67e5\u627e\u5230\u5bf9\u5e94\u7684\u952e\u3002\u8fd9\u5728\u53cd\u5411\u67e5\u627e\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u5b66\u751f\u6210\u7ee9\u8868\u4e2d\uff0c\u5feb\u901f\u67e5\u627e\u67d0\u4e2a\u5206\u6570\u5bf9\u5e94\u7684\u5b66\u751f\u59d3\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student_scores = {&#39;Alice&#39;: 85, &#39;Bob&#39;: 90, &#39;Charlie&#39;: 85}<\/p>\n<p>score_to_student = {}<\/p>\n<p>for student, score in student_scores.items():<\/p>\n<p>    if score in score_to_student:<\/p>\n<p>        if isinstance(score_to_student[score], list):<\/p>\n<p>            score_to_student[score].append(student)<\/p>\n<p>        else:<\/p>\n<p>            score_to_student[score] = [score_to_student[score], student]<\/p>\n<p>    else:<\/p>\n<p>        score_to_student[score] = student<\/p>\n<p>print(score_to_student)  # \u8f93\u51fa: {85: [&#39;Alice&#39;, &#39;Charlie&#39;], 90: &#39;Bob&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6570\u636e\u53bb\u91cd<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u6709\u65f6\u9700\u8981\u786e\u4fdd\u6570\u636e\u7684\u552f\u4e00\u6027\u3002\u901a\u8fc7\u53cd\u8f6c\u5b57\u5178\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u68c0\u6d4b\u548c\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;item1&#39;: &#39;A&#39;, &#39;item2&#39;: &#39;B&#39;, &#39;item3&#39;: &#39;A&#39;}<\/p>\n<p>unique_data = {v: k for k, v in data.items()}<\/p>\n<p>print(unique_data)  # \u8f93\u51fa: {&#39;A&#39;: &#39;item3&#39;, &#39;B&#39;: &#39;item2&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5408\u5e76\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u4e2a\u5b57\u5178\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u5b83\u4eec\u5408\u5e76\u5e76\u53cd\u8f6c\u3002\u901a\u8fc7\u53cd\u8f6c\u5b57\u5178\uff0c\u53ef\u4ee5\u7b80\u5316\u5408\u5e76\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>dict2 = {&#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p>merged_dict = {&lt;strong&gt;dict1, &lt;\/strong&gt;dict2}<\/p>\n<p>reversed_dict = {v: k for k, v in merged_dict.items()}<\/p>\n<p>print(reversed_dict)  # \u8f93\u51fa: {1: &#39;a&#39;, 2: &#39;b&#39;, 3: &#39;c&#39;, 4: &#39;d&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6027\u80fd\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u5728\u6027\u80fd\u4e0a\u53ef\u80fd\u6709\u6240\u5dee\u5f02\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u578b\u5b57\u5178\u65f6\u3002\u4e0b\u9762\u6211\u4eec\u6765\u6bd4\u8f83\u51e0\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b57\u5178\u63a8\u5bfc<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5728\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u4e0a\u90fd\u6709\u8f83\u597d\u7684\u8868\u73b0\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u662f\u9996\u9009\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>original_dict = {i: i+1 for i in range(1000000)}<\/p>\n<p>start_time = time.time()<\/p>\n<p>reversed_dict = {v: k for k, v in original_dict.items()}<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;\u5b57\u5178\u63a8\u5bfc\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. <code>zip()<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>zip()<\/code> \u51fd\u6570\u5728\u6027\u80fd\u4e0a\u4e5f\u6709\u4e0d\u9519\u7684\u8868\u73b0\uff0c\u4f46\u5728\u53ef\u8bfb\u6027\u4e0a\u7a0d\u900a\u8272\u4e8e\u5b57\u5178\u63a8\u5bfc\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">start_time = time.time()<\/p>\n<p>reversed_dict = dict(zip(original_dict.values(), original_dict.keys()))<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;zip() \u51fd\u6570\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. <code>dict<\/code> \u6784\u9020\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>dict<\/code> \u6784\u9020\u51fd\u6570\u5728\u6027\u80fd\u4e0a\u4e0e\u5b57\u5178\u63a8\u5bfc\u76f8\u5dee\u4e0d\u5927\uff0c\u4f46\u5728\u8bed\u6cd5\u4e0a\u7a0d\u663e\u590d\u6742\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">start_time = time.time()<\/p>\n<p>reversed_dict = dict((v, k) for k, v in original_dict.items())<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;dict \u6784\u9020\u51fd\u6570\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6bd4\u8f83\u53ef\u4ee5\u770b\u51fa\uff0c\u5b57\u5178\u63a8\u5bfc\u5728\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u4e0a\u90fd\u6709\u4f18\u52bf\uff0c\u662f\u53cd\u8f6c\u5b57\u5178\u7684\u6700\u4f73\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06\u5b57\u5178\u4e2d\u7684\u503c\u8fd4\u56de\u4e3a\u952e\u5728Python\u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<strong>\u5b57\u5178\u63a8\u5bfc\u3001<code>zip()<\/code>\u51fd\u6570\u548c<code>dict<\/code>\u6784\u9020\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002<strong>\u5904\u7406\u91cd\u590d\u503c\u7684\u60c5\u51b5\u65f6<\/strong>\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<strong>\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/strong>\u5305\u62ec\u6570\u636e\u53cd\u8f6c\u3001\u5feb\u901f\u67e5\u627e\u3001\u6570\u636e\u53bb\u91cd\u548c\u5408\u5e76\u5b57\u5178\u7b49\u3002\u901a\u8fc7\u5bf9\u6bd4\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u5178\u7684\u503c\u66ff\u6362\u4e3a\u5bf9\u5e94\u7684\u952e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u904d\u5386\u5b57\u5178\u7684\u9879\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u5176\u4e2d\u539f\u5b57\u5178\u7684\u503c\u6210\u4e3a\u65b0\u5b57\u5178\u7684\u952e\uff0c\u539f\u5b57\u5178\u7684\u952e\u6210\u4e3a\u65b0\u5b57\u5178\u7684\u503c\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">original_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nreversed_dict = {v: k for k, v in original_dict.items()}\nprint(reversed_dict)  # \u8f93\u51fa: {1: &#39;a&#39;, 2: &#39;b&#39;, 3: &#39;c&#39;}\n<\/code><\/pre>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\uff0c\u503c\u662f\u5426\u53ef\u4ee5\u91cd\u590d\uff1f\u5982\u679c\u53ef\u4ee5\uff0c\u5982\u4f55\u5904\u7406\u91cd\u590d\u503c\u7684\u60c5\u51b5\uff1f<\/strong><br \/>Python\u4e2d\u7684\u5b57\u5178\u5141\u8bb8\u503c\u91cd\u590d\uff0c\u4f46\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002\u5982\u679c\u5b57\u5178\u4e2d\u5b58\u5728\u591a\u4e2a\u76f8\u540c\u7684\u503c\uff0c\u53cd\u8f6c\u5b57\u5178\u65f6\u53ea\u80fd\u4fdd\u7559\u5176\u4e2d\u4e00\u4e2a\u952e\u3002\u5904\u7406\u91cd\u590d\u503c\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u503c\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u7559\u6240\u6709\u5bf9\u5e94\u7684\u952e\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">original_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 1}\nreversed_dict = {}\nfor k, v in original_dict.items():\n    reversed_dict.setdefault(v, []).append(k)\nprint(reversed_dict)  # \u8f93\u51fa: {1: [&#39;a&#39;, &#39;c&#39;], 2: [&#39;b&#39;]}\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Python\u7684collections\u6a21\u5757\u662f\u5426\u6709\u66f4\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u503c\u5230\u952e\u7684\u8f6c\u6362\uff1f<\/strong><br \/>\u786e\u5b9e\uff0cPython\u7684collections\u6a21\u5757\u4e2d\u7684<code>defaultdict<\/code>\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e00\u8fc7\u7a0b\u3002\u901a\u8fc7\u4f7f\u7528<code>defaultdict(list)<\/code>\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u591a\u4e2a\u952e\u4e0e\u76f8\u540c\u7684\u503c\u5173\u8054\u5728\u4e00\u8d77\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import defaultdict\n\noriginal_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 1}\nreversed_dict = defaultdict(list)\nfor k, v in original_dict.items():\n    reversed_dict[v].append(k)\nprint(dict(reversed_dict))  # \u8f93\u51fa: {1: [&#39;a&#39;, &#39;c&#39;], 2: [&#39;b&#39;]}\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u7075\u6d3b\u5730\u6839\u636e\u5b57\u5178\u7684\u5185\u5bb9\u8fdb\u884c\u503c\u4e0e\u952e\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u5b57\u5178\u4e2d\u7684\u503c\u8fd4\u56de\u4e3a\u952e\u7684\u64cd\u4f5c\u4e3b\u8981\u6d89\u53ca\u5b57\u5178\u7684\u53cd\u8f6c\u3002 \u8fd9\u79cd\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1139889,"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\/1139879"}],"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=1139879"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139879\/revisions"}],"predecessor-version":[{"id":1139891,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139879\/revisions\/1139891"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139889"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}