{"id":947876,"date":"2024-12-26T23:57:41","date_gmt":"2024-12-26T15:57:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/947876.html"},"modified":"2024-12-26T23:57:43","modified_gmt":"2024-12-26T15:57:43","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8switch","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/947876.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528switch"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083500\/d8b6eac6-c766-498f-b3d7-1df7f5c728db.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528switch\" \/><\/p>\n<p><p> <strong>Python \u4f7f\u7528 switch \u8bed\u53e5\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u5178\u6620\u5c04\u3001\u4f7f\u7528 if-elif-else \u7ed3\u6784\u3001\u4f7f\u7528 match-case \u8bed\u6cd5\uff08Python 3.10+\uff09\u3002\u5b57\u5178\u6620\u5c04\u662f\u4f7f\u7528 switch \u8bed\u53e5\u7684\u5e38\u89c1\u66ff\u4ee3\u65b9\u6cd5<\/strong>\u3002\u5b57\u5178\u6620\u5c04\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6216 lambda \u8868\u8fbe\u5f0f\u7684\u5b57\u5178\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u867d\u7136\u6ca1\u6709\u5185\u7f6e\u7684 switch \u8bed\u53e5\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u6620\u5c04\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u5b57\u5178\u6620\u5c04\u662f\u4e00\u79cd\u5c06\u952e\u503c\u5bf9\u5173\u8054\u5728\u4e00\u8d77\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7\u952e\u5feb\u901f\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c\u3002\u901a\u8fc7\u5c06\u51fd\u6570\u6216 lambda \u8868\u8fbe\u5f0f\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u8c03\u7528\u76f8\u5e94\u7684\u903b\u8f91\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u5b57\u5178\u6620\u5c04\u5b9e\u73b0 switch \u8bed\u53e5\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u5b57\u5178\u6620\u5c04<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\uff0c\u5c06\u6bcf\u4e2a\u53ef\u80fd\u7684\u8f93\u5165\u503c\uff08\u7c7b\u4f3c switch \u8bed\u53e5\u4e2d\u7684 case \u503c\uff09\u6620\u5c04\u5230\u4e00\u4e2a\u51fd\u6570\u6216 lambda \u8868\u8fbe\u5f0f\u3002\u8fd9\u4e9b\u51fd\u6570\u6216\u8868\u8fbe\u5f0f\u5b9e\u73b0\u4e86\u5bf9\u5e94\u4e8e\u6bcf\u4e2a\u8f93\u5165\u503c\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def case_one():<\/p>\n<p>    return &quot;This is case one.&quot;<\/p>\n<p>def case_two():<\/p>\n<p>    return &quot;This is case two.&quot;<\/p>\n<p>def default_case():<\/p>\n<p>    return &quot;This is the default case.&quot;<\/p>\n<p>switch_dict = {<\/p>\n<p>    1: case_one,<\/p>\n<p>    2: case_two<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>switch_dict<\/code> \u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5b83\u5c06\u6574\u6570 1 \u548c 2 \u5206\u522b\u6620\u5c04\u5230 <code>case_one<\/code> \u548c <code>case_two<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u7528\u5b57\u5178\u6620\u5c04<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u952e\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u51fd\u6570\uff0c\u5e76\u8c03\u7528\u5b83\u4eec\u3002\u53ef\u4ee5\u4f7f\u7528 <code>dict.get(key, default)<\/code> \u65b9\u6cd5\u6765\u5b9e\u73b0\u9ed8\u8ba4\u60c5\u51b5\uff08\u76f8\u5f53\u4e8e switch \u8bed\u53e5\u4e2d\u7684 default\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def switch_case(value):<\/p>\n<p>    return switch_dict.get(value, default_case)()<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>print(switch_case(1))  # \u8f93\u51fa: This is case one.<\/p>\n<p>print(switch_case(3))  # \u8f93\u51fa: This is the default case.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c<code>switch_case<\/code> \u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 <code>value<\/code>\uff0c\u901a\u8fc7 <code>switch_dict.get(value, default_case)<\/code> \u67e5\u627e\u5bf9\u5e94\u7684\u51fd\u6570\u5e76\u8c03\u7528\u3002\u5982\u679c\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u8be5\u952e\uff0c\u5219\u8c03\u7528 <code>default_case<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u7f3a\u70b9\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u6620\u5c04\u5b9e\u73b0 switch \u8bed\u53e5\u7684\u4f18\u70b9\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u53ef\u6269\u5c55\u6027\u5f3a<\/strong>\uff1a\u53ef\u4ee5\u8f7b\u677e\u6dfb\u52a0\u65b0\u7684 case \u5904\u7406\u903b\u8f91\uff0c\u53ea\u9700\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u907f\u514d\u4e86\u5197\u957f\u7684 if-elif-else \u7ed3\u6784\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4e0d\u652f\u6301\u8303\u56f4\u5224\u65ad<\/strong>\uff1a\u5b57\u5178\u6620\u5c04\u53ea\u652f\u6301\u7cbe\u786e\u5339\u914d\uff0c\u4e0d\u652f\u6301\u8303\u56f4\u5224\u65ad\uff08\u4f8b\u5982 case 1..5\uff09\u3002<\/li>\n<li><strong>\u51fd\u6570\u8c03\u7528\u5f00\u9500<\/strong>\uff1a\u5bf9\u4e8e\u6bcf\u4e2a case\uff0c\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6216 lambda \u8868\u8fbe\u5f0f\uff0c\u53ef\u80fd\u5bfc\u81f4\u989d\u5916\u7684\u51fd\u6570\u8c03\u7528\u5f00\u9500\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 if-elif-else \u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5b57\u5178\u6620\u5c04\u662f\u4e00\u79cd\u5b9e\u73b0 switch \u8bed\u53e5\u7684\u6709\u6548\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f20\u7edf\u7684 if-elif-else \u7ed3\u6784\u53ef\u80fd\u66f4\u52a0\u5408\u9002\uff0c\u7279\u522b\u662f\u5f53\u9700\u8981\u8fdb\u884c\u590d\u6742\u6761\u4ef6\u5224\u65ad\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def switch_case(value):<\/p>\n<p>    if value == 1:<\/p>\n<p>        return &quot;This is case one.&quot;<\/p>\n<p>    elif value == 2:<\/p>\n<p>        return &quot;This is case two.&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;This is the default case.&quot;<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>print(switch_case(1))  # \u8f93\u51fa: This is case one.<\/p>\n<p>print(switch_case(3))  # \u8f93\u51fa: This is the default case.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 match-case \u8bed\u6cd5\uff08Python 3.10+\uff09<\/h3>\n<\/p>\n<p><p>Python 3.10 \u5f15\u5165\u4e86\u65b0\u7684 <code>match-case<\/code> \u8bed\u6cd5\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u63a5\u8fd1\u4e8e\u4f20\u7edf switch \u8bed\u53e5\u7684\u529f\u80fd\u3002<code>match-case<\/code> \u5141\u8bb8\u5bf9\u503c\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\uff0c\u5e76\u6839\u636e\u5339\u914d\u7684\u6a21\u5f0f\u6267\u884c\u76f8\u5e94\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def switch_case(value):<\/p>\n<p>    match value:<\/p>\n<p>        case 1:<\/p>\n<p>            return &quot;This is case one.&quot;<\/p>\n<p>        case 2:<\/p>\n<p>            return &quot;This is case two.&quot;<\/p>\n<p>        case _:<\/p>\n<p>            return &quot;This is the default case.&quot;<\/p>\n<h2><strong>\u793a\u4f8b\u8c03\u7528<\/strong><\/h2>\n<p>print(switch_case(1))  # \u8f93\u51fa: This is case one.<\/p>\n<p>print(switch_case(3))  # \u8f93\u51fa: This is the default case.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>match-case<\/code> \u8bed\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u7ed3\u6784\u5316\u7684\u65b9\u5f0f\u6765\u8fdb\u884c\u503c\u5339\u914d\uff0c\u652f\u6301\u591a\u79cd\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong>\uff1a\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u4e3b\u8981\u662f\u7b80\u5355\u7684\u503c\u6620\u5c04\uff0c\u5b57\u5178\u6620\u5c04\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u5982\u679c\u9700\u8981\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\uff0cif-elif-else \u7ed3\u6784\u53ef\u80fd\u66f4\u5408\u9002\u3002\u5728 Python 3.10 \u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c<code>match-case<\/code> \u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4f18\u96c5\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u6027\u80fd<\/strong>\uff1a\u5728\u6027\u80fd\u654f\u611f\u7684\u573a\u666f\u4e0b\uff0c\u8003\u8651\u5b57\u5178\u6620\u5c04\u7684\u51fd\u6570\u8c03\u7528\u5f00\u9500\uff0c\u4ee5\u53ca if-elif-else \u7ed3\u6784\u7684\u6761\u4ef6\u68c0\u67e5\u5f00\u9500\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u6301\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong>\uff1a\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u59cb\u7ec8\u4fdd\u6301\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4f7f\u7528\u6e05\u6670\u7684\u51fd\u6570\u540d\u79f0\u548c\u6ce8\u91ca\u6765\u63cf\u8ff0\u6bcf\u4e2a case \u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728 Python \u4e2d\u5b9e\u73b0\u7c7b\u4f3c switch \u8bed\u53e5\u7684\u529f\u80fd\uff0c\u5e76\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u6700\u4f73\u5b9e\u73b0\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u662f\u5426\u6709\u5185\u7f6e\u7684switch\u8bed\u53e5\uff1f<\/strong><br \/>Python\u672c\u8eab\u6ca1\u6709\u5185\u7f6e\u7684switch\u8bed\u53e5\uff0c\u8fd9\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\uff08\u5982C\u6216Java\uff09\u4e0d\u540c\u3002Python\u66f4\u503e\u5411\u4e8e\u4f7f\u7528\u5b57\u5178\u3001if-elif-else\u8bed\u53e5\u6216match\u8bed\u53e5\uff08Python 3.10\u53ca\u4ee5\u4e0a\u7248\u672c\uff09\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4fdd\u6301\u4ee3\u7801\u7684\u6e05\u6670\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u5b9e\u73b0switch\u529f\u80fd\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u5c06\u51fd\u6570\u6216\u503c\u6620\u5c04\u5230\u5bf9\u5e94\u7684\u952e\uff0c\u4ece\u800c\u6a21\u62dfswitch\u7684\u884c\u4e3a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">def case_one():\n    return &quot;\u8fd9\u662f\u6848\u4f8b\u4e00&quot;\n\ndef case_two():\n    return &quot;\u8fd9\u662f\u6848\u4f8b\u4e8c&quot;\n\ndef case_default():\n    return &quot;\u8fd9\u662f\u9ed8\u8ba4\u6848\u4f8b&quot;\n\nswitch_dict = {\n    1: case_one,\n    2: case_two\n}\n\nresult = switch_dict.get(user_input, case_default)()  # user_input\u662f\u7528\u6237\u8f93\u5165\u7684\u503c\nprint(result)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u7684\u503c\u5c06\u88ab\u7528\u4e8e\u67e5\u627e\u5b57\u5178\u4e2d\u7684\u5bf9\u5e94\u51fd\u6570\uff0c\u5982\u679c\u672a\u627e\u5230\uff0c\u9ed8\u8ba4\u51fd\u6570\u5c06\u88ab\u8c03\u7528\u3002<\/p>\n<p><strong>Python 3.10\u4e2dmatch\u8bed\u53e5\u7684\u4f7f\u7528\u65b9\u5f0f\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python 3.10\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528match\u8bed\u53e5\u6765\u5b9e\u73b0\u7c7b\u4f3c\u4e8eswitch\u7684\u529f\u80fd\u3002\u5b83\u7684\u8bed\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u6613\u4e8e\u7406\u89e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-python\">def switch_example(value):\n    match value:\n        case 1:\n            return &quot;\u8fd9\u662f\u6848\u4f8b\u4e00&quot;\n        case 2:\n            return &quot;\u8fd9\u662f\u6848\u4f8b\u4e8c&quot;\n        case _:\n            return &quot;\u8fd9\u662f\u9ed8\u8ba4\u6848\u4f8b&quot;\n\nprint(switch_example(user_input))  # user_input\u4e3a\u7528\u6237\u8f93\u5165\u7684\u503c\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cmatch\u8bed\u53e5\u4f1a\u68c0\u67e5value\u7684\u503c\uff0c\u5e76\u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u8fd4\u56de\u4e0d\u540c\u7684\u7ed3\u679c\u3002\u4f7f\u7528case _\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u9ed8\u8ba4\u7684\u5904\u7406\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u4f7f\u7528 switch \u8bed\u53e5\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u5178\u6620\u5c04\u3001\u4f7f\u7528 if-elif-else \u7ed3\u6784\u3001\u4f7f\u7528 m [&hellip;]","protected":false},"author":3,"featured_media":947880,"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\/947876"}],"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=947876"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947876\/revisions"}],"predecessor-version":[{"id":947881,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947876\/revisions\/947881"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/947880"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=947876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=947876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=947876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}