{"id":1017336,"date":"2024-12-27T12:26:06","date_gmt":"2024-12-27T04:26:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1017336.html"},"modified":"2024-12-27T12:26:14","modified_gmt":"2024-12-27T04:26:14","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%88%86%e6%ae%b5%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1017336.html","title":{"rendered":"python\u5982\u4f55\u6253\u5206\u6bb5\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25160234\/c3b4cd7a-843a-4f10-81a8-293766dea37b.webp\" alt=\"python\u5982\u4f55\u6253\u5206\u6bb5\u51fd\u6570\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u6253\u5206\u6bb5\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7<strong>\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u3001\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528numpy\u7684select\u51fd\u6570<\/strong>\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<strong>\u6761\u4ef6\u8bed\u53e5<\/strong>\u53ef\u4ee5\u901a\u8fc7if-elif-else\u6765\u68c0\u67e5\u8f93\u5165\u503c\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u8f93\u51fa\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u5206\u6bb5\u51fd\u6570\u3002<strong>\u5b9a\u4e49\u51fd\u6570<\/strong>\u7684\u65b9\u6cd5\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u6761\u4ef6\u8bed\u53e5\u5c01\u88c5\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u91cd\u7528\u6027\u3002<strong>\u4f7f\u7528numpy\u7684select\u51fd\u6570<\/strong>\u5219\u9002\u5408\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u60c5\u51b5\u4e0b\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5229\u7528\u5411\u91cf\u5316\u64cd\u4f5c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u51e0\u79cd\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\uff1a\u4f7f\u7528numpy\u7684select\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528numpy\u7684select\u51fd\u6570\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u7684\u6548\u7387\u3002\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u6761\u4ef6\u5217\u8868\u548c\u4e00\u4e2a\u8f93\u51fa\u503c\u5217\u8868\uff0c\u7136\u540e\u4f20\u9012\u7ed9numpy.select\u51fd\u6570\u3002numpy\u4f1a\u6839\u636e\u6761\u4ef6\u5217\u8868\u4e2d\u7684\u6761\u4ef6\u9010\u4e2a\u68c0\u67e5\u8f93\u5165\u503c\uff0c\u5e76\u5c06\u7b26\u5408\u6761\u4ef6\u7684\u8f93\u5165\u503c\u6620\u5c04\u4e3a\u5bf9\u5e94\u8f93\u51fa\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\u8fd8\u80fd\u5145\u5206\u5229\u7528numpy\u7684\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6761\u4ef6\u8bed\u53e5\u5b9e\u73b0\u5206\u6bb5\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u6761\u4ef6\u8bed\u53e5\u662f\u5b9e\u73b0\u5206\u6bb5\u51fd\u6570\u6700\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u4e00\u7cfb\u5217\u7684if-elif-else\u8bed\u53e5\u6765\u5b9e\u73b0\u4e0d\u540c\u533a\u95f4\u7684\u5224\u65ad\u548c\u8f93\u51fa\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u5206\u6bb5\u51fd\u6570\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5355\u4e2a\u8f93\u5165\u503c\u65f6\u663e\u5f97\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528if-elif-else\u8bed\u53e5<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7if-elif-else\u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u8f93\u5165\u503c\u7684\u4e0d\u540c\u533a\u95f4\u6765\u8f93\u51fa\u4e0d\u540c\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u5206\u6bb5\u51fd\u6570f(x)\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5f53x &lt; 0\u65f6\uff0cf(x) = -1<\/li>\n<li>\u5f530 &lt;= x &lt;= 10\u65f6\uff0cf(x) = 2x<\/li>\n<li>\u5f53x &gt; 10\u65f6\uff0cf(x) = 3x + 1<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">def piecewise_function(x):<\/p>\n<p>    if x &lt; 0:<\/p>\n<p>        return -1<\/p>\n<p>    elif 0 &lt;= x &lt;= 10:<\/p>\n<p>        return 2 * x<\/p>\n<p>    else:<\/p>\n<p>        return 3 * x + 1<\/p>\n<h2><strong>\u6d4b\u8bd5\u51fd\u6570<\/strong><\/h2>\n<p>print(piecewise_function(-5))  # \u8f93\u51fa\uff1a-1<\/p>\n<p>print(piecewise_function(5))   # \u8f93\u51fa\uff1a10<\/p>\n<p>print(piecewise_function(15))  # \u8f93\u51fa\uff1a46<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5904\u7406\u590d\u6742\u7684\u5206\u6bb5\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u8f83\u4e3a\u590d\u6742\u7684\u5206\u6bb5\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u6bcf\u4e2a\u6761\u4ef6\u8bed\u53e5\u4e2d\u5d4c\u5957\u66f4\u591a\u7684\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u5206\u6bb5\u51fd\u6570\uff0c\u9700\u8981\u5728\u6bcf\u4e2a\u533a\u95f4\u5185\u6267\u884c\u989d\u5916\u7684\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def complex_piecewise_function(x):<\/p>\n<p>    if x &lt; 0:<\/p>\n<p>        result = -1 * (x  2)<\/p>\n<p>    elif 0 &lt;= x &lt;= 10:<\/p>\n<p>        result = 2 * x + (x  0.5)<\/p>\n<p>    else:<\/p>\n<p>        result = 3 * x + 1 - (x \/ 2)<\/p>\n<p>    return result<\/p>\n<h2><strong>\u6d4b\u8bd5\u51fd\u6570<\/strong><\/h2>\n<p>print(complex_piecewise_function(-5))  # \u8f93\u51fa\uff1a-25<\/p>\n<p>print(complex_piecewise_function(5))   # \u8f93\u51fa\uff1a12.23606797749979<\/p>\n<p>print(complex_piecewise_function(15))  # \u8f93\u51fa\uff1a38.5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u5206\u6bb5\u903b\u8f91<\/p>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5c01\u88c5\u5206\u6bb5\u903b\u8f91\u662f\u4e00\u4e2a\u826f\u597d\u7684\u7f16\u7a0b\u5b9e\u8df5\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u591a\u6b21\u8c03\u7528\u76f8\u540c\u7684\u5206\u6bb5\u51fd\u6570\u65f6\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4e5f\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b9a\u4e49\u5206\u6bb5\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Python\u7684\u51fd\u6570\u5b9a\u4e49\u8bed\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5206\u6bb5\u903b\u8f91\u5c01\u88c5\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u4e0d\u540c\u90e8\u5206\u91cd\u590d\u4f7f\u7528\u76f8\u540c\u7684\u5206\u6bb5\u903b\u8f91\uff0c\u800c\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u91cd\u65b0\u7f16\u5199\u6761\u4ef6\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def piecewise_function(x):<\/p>\n<p>    if x &lt; 0:<\/p>\n<p>        return -1<\/p>\n<p>    elif 0 &lt;= x &lt;= 10:<\/p>\n<p>        return 2 * x<\/p>\n<p>    else:<\/p>\n<p>        return 3 * x + 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u5c01\u88c5\u51fd\u6570\u5904\u7406\u6570\u636e\u96c6<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u96c6\u65f6\uff0c\u5c01\u88c5\u7684\u5206\u6bb5\u51fd\u6570\u53ef\u4ee5\u88ab\u7528\u6765\u5bf9\u6bcf\u4e2a\u6570\u636e\u70b9\u8fdb\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u6709\u4e00\u7ec4\u6570\u636e\uff0c\u9700\u8981\u5bf9\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e0a\u8ff0\u5206\u6bb5\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [-3, 0, 5, 10, 15]<\/p>\n<p>results = [piecewise_function(x) for x in data]<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(results)  # \u8f93\u51fa\uff1a[-1, 0, 10, 20, 46]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u53c2\u6570\u5316\u5206\u6bb5\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u51fd\u6570\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u53c2\u6570\u6765\u5bf9\u5206\u6bb5\u51fd\u6570\u8fdb\u884c\u53c2\u6570\u5316\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u5e0c\u671b\u5206\u6bb5\u51fd\u6570\u7684\u533a\u95f4\u548c\u8f93\u51fa\u7cfb\u6570\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u6307\u5b9a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def parametric_piecewise_function(x, thresholds, coefficients):<\/p>\n<p>    if x &lt; thresholds[0]:<\/p>\n<p>        return coefficients[0] * x<\/p>\n<p>    elif thresholds[0] &lt;= x &lt;= thresholds[1]:<\/p>\n<p>        return coefficients[1] * x<\/p>\n<p>    else:<\/p>\n<p>        return coefficients[2] * x + coefficients[3]<\/p>\n<h2><strong>\u5b9a\u4e49\u9608\u503c\u548c\u7cfb\u6570<\/strong><\/h2>\n<p>thresholds = [0, 10]<\/p>\n<p>coefficients = [-1, 2, 3, 1]<\/p>\n<h2><strong>\u5e94\u7528\u53c2\u6570\u5316\u5206\u6bb5\u51fd\u6570<\/strong><\/h2>\n<p>result = parametric_piecewise_function(5, thresholds, coefficients)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528numpy\u7684select\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0cnumpy\u5e93\u63d0\u4f9b\u7684select\u51fd\u6570\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u9009\u62e9\u3002\u5b83\u53ef\u4ee5\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u9009\u62e9\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u9700\u8981\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5206\u6bb5\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>numpy.select\u51fd\u6570\u7b80\u4ecb<\/li>\n<\/ol>\n<p><p>numpy.select(conditionlist, choicelist)\u51fd\u6570\u80fd\u591f\u6839\u636econditionlist\u4e2d\u7684\u6761\u4ef6\uff0c\u5c06\u8f93\u5165\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6620\u5c04\u5230choicelist\u4e2d\u7684\u5bf9\u5e94\u503c\u3002\u6761\u4ef6\u548c\u9009\u62e9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4e00\u4e00\u5bf9\u5e94\uff0c\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u5143\u7d20\u5c06\u88ab\u66ff\u6362\u4e3a\u76f8\u5e94\u7684\u9009\u62e9\u5217\u8868\u4e2d\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u8f93\u5165\u6570\u636e<\/strong><\/h2>\n<p>x = np.array([-5, 0, 5, 10, 15])<\/p>\n<h2><strong>\u5b9a\u4e49\u6761\u4ef6\u5217\u8868<\/strong><\/h2>\n<p>conditions = [x &lt; 0, (x &gt;= 0) &amp; (x &lt;= 10), x &gt; 10]<\/p>\n<h2><strong>\u5b9a\u4e49\u9009\u62e9\u5217\u8868<\/strong><\/h2>\n<p>choices = [-1, 2 * x, 3 * x + 1]<\/p>\n<h2><strong>\u4f7f\u7528numpy.select\u8fdb\u884c\u5206\u6bb5\u5904\u7406<\/strong><\/h2>\n<p>result = np.select(conditions, choices)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(result)  # \u8f93\u51fa\uff1a[-1  0 10 20 46]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u9ad8\u6548\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e<\/li>\n<\/ol>\n<p><p>numpy\u7684select\u51fd\u6570\u901a\u8fc7\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002\u8fd9\u5728\u9700\u8981\u5bf9\u5927\u91cf\u6570\u636e\u8fdb\u884c\u5206\u6bb5\u5904\u7406\u65f6\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u5927\u89c4\u6a21\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 20, 1000000)<\/p>\n<h2><strong>\u5b9a\u4e49\u6761\u4ef6\u5217\u8868<\/strong><\/h2>\n<p>conditions = [x &lt; 0, (x &gt;= 0) &amp; (x &lt;= 10), x &gt; 10]<\/p>\n<h2><strong>\u5b9a\u4e49\u9009\u62e9\u5217\u8868<\/strong><\/h2>\n<p>choices = [-1, 2 * x, 3 * x + 1]<\/p>\n<h2><strong>\u4f7f\u7528numpy.select\u8fdb\u884c\u5206\u6bb5\u5904\u7406<\/strong><\/h2>\n<p>result = np.select(conditions, choices)<\/p>\n<h2><strong>\u8f93\u51fa\u524d\u5341\u4e2a\u7ed3\u679c<\/strong><\/h2>\n<p>print(result[:10])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528numpy\u7684where\u51fd\u6570\u8fdb\u884c\u5206\u6bb5\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u9664\u4e86select\u51fd\u6570\uff0cnumpy\u8fd8\u63d0\u4f9b\u4e86where\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u7b80\u5355\u7684\u5206\u6bb5\u5904\u7406\u3002where\u51fd\u6570\u7684\u4f7f\u7528\u66f4\u52a0\u7075\u6d3b\uff0c\u9002\u5408\u5904\u7406\u4e24\u4e2a\u6761\u4ef6\u7684\u7b80\u5355\u5206\u6bb5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u8f93\u5165\u6570\u636e<\/strong><\/h2>\n<p>x = np.array([-5, 0, 5, 10, 15])<\/p>\n<h2><strong>\u4f7f\u7528numpy.where\u8fdb\u884c\u5206\u6bb5\u5904\u7406<\/strong><\/h2>\n<p>result = np.where(x &lt; 0, -1, np.where(x &lt;= 10, 2 * x, 3 * x + 1))<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(result)  # \u8f93\u51fa\uff1a[-1  0 10 20 46]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5e94\u7528\u5b9e\u4f8b\uff1a\u5206\u6bb5\u51fd\u6570\u5728\u5b9e\u9645\u95ee\u9898\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5206\u6bb5\u51fd\u6570\u5728\u5b9e\u9645\u95ee\u9898\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5c24\u5176\u662f\u5728\u7ecf\u6d4e\u5b66\u3001\u5de5\u7a0b\u5b66\u548c\u6570\u636e\u5206\u6790\u9886\u57df\u3002\u4ee5\u4e0b\u6211\u4eec\u5c06\u63a2\u8ba8\u51e0\u4e2a\u5177\u4f53\u7684\u5e94\u7528\u5b9e\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u8fd9\u4e9b\u5206\u6bb5\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\u7a0e\u7387\u8ba1\u7b97<\/li>\n<\/ol>\n<p><p>\u5728\u7a0e\u52a1\u7cfb\u7edf\u4e2d\uff0c\u7a0e\u7387\u901a\u5e38\u662f\u5206\u6bb5\u8ba1\u7b97\u7684\u3002\u4e0d\u540c\u6536\u5165\u533a\u95f4\u5bf9\u5e94\u4e0d\u540c\u7684\u7a0e\u7387\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5206\u6bb5\u51fd\u6570\u8fdb\u884c\u6a21\u62df\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u7a0e\u7387\u89c4\u5219\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6536\u5165\u5c0f\u4e8e5000\u7684\u90e8\u5206\uff0c\u7a0e\u7387\u4e3a5%<\/li>\n<li>\u6536\u5165\u57285000\u523010000\u4e4b\u95f4\u7684\u90e8\u5206\uff0c\u7a0e\u7387\u4e3a10%<\/li>\n<li>\u6536\u5165\u8d85\u8fc710000\u7684\u90e8\u5206\uff0c\u7a0e\u7387\u4e3a15%<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">def tax_calculation(income):<\/p>\n<p>    if income &lt;= 5000:<\/p>\n<p>        return income * 0.05<\/p>\n<p>    elif 5000 &lt; income &lt;= 10000:<\/p>\n<p>        return 5000 * 0.05 + (income - 5000) * 0.10<\/p>\n<p>    else:<\/p>\n<p>        return 5000 * 0.05 + 5000 * 0.10 + (income - 10000) * 0.15<\/p>\n<h2><strong>\u6d4b\u8bd5\u7a0e\u7387\u8ba1\u7b97<\/strong><\/h2>\n<p>print(tax_calculation(3000))   # \u8f93\u51fa\uff1a150.0<\/p>\n<p>print(tax_calculation(7500))   # \u8f93\u51fa\uff1a500.0<\/p>\n<p>print(tax_calculation(12000))  # \u8f93\u51fa\uff1a1150.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8fd0\u8f93\u8d39\u7528\u8ba1\u7b97<\/li>\n<\/ol>\n<p><p>\u5728\u7269\u6d41\u884c\u4e1a\uff0c\u8fd0\u8f93\u8d39\u7528\u7ecf\u5e38\u6839\u636e\u8fd0\u8f93\u8ddd\u79bb\u8fdb\u884c\u5206\u6bb5\u8ba1\u7b97\u3002\u5047\u8bbe\u6211\u4eec\u6709\u5982\u4e0b\u8fd0\u8f93\u8d39\u7528\u89c4\u5219\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u8ddd\u79bb\u5c0f\u4e8e100\u516c\u91cc\u7684\u90e8\u5206\uff0c\u6bcf\u516c\u91cc\u8d39\u7528\u4e3a1\u5143<\/li>\n<li>\u8ddd\u79bb\u5728100\u5230500\u516c\u91cc\u4e4b\u95f4\u7684\u90e8\u5206\uff0c\u6bcf\u516c\u91cc\u8d39\u7528\u4e3a0.8\u5143<\/li>\n<li>\u8ddd\u79bb\u8d85\u8fc7500\u516c\u91cc\u7684\u90e8\u5206\uff0c\u6bcf\u516c\u91cc\u8d39\u7528\u4e3a0.5\u5143<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">def shipping_cost(distance):<\/p>\n<p>    if distance &lt;= 100:<\/p>\n<p>        return distance * 1.0<\/p>\n<p>    elif 100 &lt; distance &lt;= 500:<\/p>\n<p>        return 100 * 1.0 + (distance - 100) * 0.8<\/p>\n<p>    else:<\/p>\n<p>        return 100 * 1.0 + 400 * 0.8 + (distance - 500) * 0.5<\/p>\n<h2><strong>\u6d4b\u8bd5\u8fd0\u8f93\u8d39\u7528\u8ba1\u7b97<\/strong><\/h2>\n<p>print(shipping_cost(50))    # \u8f93\u51fa\uff1a50.0<\/p>\n<p>print(shipping_cost(150))   # \u8f93\u51fa\uff1a140.0<\/p>\n<p>print(shipping_cost(600))   # \u8f93\u51fa\uff1a420.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u9636\u68af\u6c34\u8d39\u8ba1\u7b97<\/li>\n<\/ol>\n<p><p>\u5728\u6c34\u8d39\u8ba1\u7b97\u4e2d\uff0c\u7ecf\u5e38\u91c7\u7528\u9636\u68af\u5f0f\u7684\u8ba1\u8d39\u65b9\u5f0f\u3002\u5047\u8bbe\u6211\u4eec\u6709\u5982\u4e0b\u6c34\u8d39\u89c4\u5219\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7528\u6c34\u91cf\u5c0f\u4e8e\u7b49\u4e8e15\u7acb\u65b9\u7c73\u7684\u90e8\u5206\uff0c\u6bcf\u7acb\u65b9\u7c73\u8d39\u7528\u4e3a2\u5143<\/li>\n<li>\u7528\u6c34\u91cf\u572815\u523030\u7acb\u65b9\u7c73\u4e4b\u95f4\u7684\u90e8\u5206\uff0c\u6bcf\u7acb\u65b9\u7c73\u8d39\u7528\u4e3a3\u5143<\/li>\n<li>\u7528\u6c34\u91cf\u8d85\u8fc730\u7acb\u65b9\u7c73\u7684\u90e8\u5206\uff0c\u6bcf\u7acb\u65b9\u7c73\u8d39\u7528\u4e3a5\u5143<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">def water_bill(volume):<\/p>\n<p>    if volume &lt;= 15:<\/p>\n<p>        return volume * 2<\/p>\n<p>    elif 15 &lt; volume &lt;= 30:<\/p>\n<p>        return 15 * 2 + (volume - 15) * 3<\/p>\n<p>    else:<\/p>\n<p>        return 15 * 2 + 15 * 3 + (volume - 30) * 5<\/p>\n<h2><strong>\u6d4b\u8bd5\u6c34\u8d39\u8ba1\u7b97<\/strong><\/h2>\n<p>print(water_bill(10))   # \u8f93\u51fa\uff1a20<\/p>\n<p>print(water_bill(20))   # \u8f93\u51fa\uff1a45<\/p>\n<p>print(water_bill(40))   # \u8f93\u51fa\uff1a125<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f18\u5316\u4e0e\u6269\u5c55\uff1a\u63d0\u9ad8\u5206\u6bb5\u51fd\u6570\u7684\u6027\u80fd\u548c\u7075\u6d3b\u6027<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u4e00\u6b65\u4f18\u5316\u5206\u6bb5\u51fd\u6570\u7684\u6027\u80fd\uff0c\u6216\u8005\u589e\u52a0\u5176\u7075\u6d3b\u6027\u4ee5\u9002\u5e94\u66f4\u591a\u7684\u5e94\u7528\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f18\u5316\u548c\u6269\u5c55\u7b56\u7565\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u5b57\u5178\u6620\u5c04<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u5177\u6709\u56fa\u5b9a\u533a\u95f4\u548c\u5bf9\u5e94\u503c\u7684\u5206\u6bb5\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u8fdb\u884c\u6620\u5c04\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u67e5\u627e\u901f\u5ea6\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u79bb\u6563\u8f93\u5165\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def dictionary_mapping(x):<\/p>\n<p>    mapping = {<\/p>\n<p>        (-float(&#39;inf&#39;), 0): -1,<\/p>\n<p>        (0, 10): lambda x: 2 * x,<\/p>\n<p>        (10, float(&#39;inf&#39;)): lambda x: 3 * x + 1<\/p>\n<p>    }<\/p>\n<p>    for (low, high), func in mapping.items():<\/p>\n<p>        if low &lt; x &lt;= high:<\/p>\n<p>            return func(x) if callable(func) else func<\/p>\n<h2><strong>\u6d4b\u8bd5\u5b57\u5178\u6620\u5c04<\/strong><\/h2>\n<p>print(dictionary_mapping(-5))  # \u8f93\u51fa\uff1a-1<\/p>\n<p>print(dictionary_mapping(5))   # \u8f93\u51fa\uff1a10<\/p>\n<p>print(dictionary_mapping(15))  # \u8f93\u51fa\uff1a46<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5411\u91cf\u5316\u64cd\u4f5c<\/li>\n<\/ol>\n<p><p>\u5728\u9700\u8981\u5bf9\u5927\u6570\u636e\u96c6\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u5411\u91cf\u5316\u64cd\u4f5c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002numpy\u7684select\u548cwhere\u51fd\u6570\u5c31\u662f\u5178\u578b\u7684\u5411\u91cf\u5316\u64cd\u4f5c\u3002\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5faa\u73af\uff0c\u800c\u662f\u4f7f\u7528numpy\u7684\u5411\u91cf\u5316\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def vectorized_piecewise_function(x):<\/p>\n<p>    conditions = [x &lt; 0, (x &gt;= 0) &amp; (x &lt;= 10), x &gt; 10]<\/p>\n<p>    choices = [-1, 2 * x, 3 * x + 1]<\/p>\n<p>    return np.select(conditions, choices)<\/p>\n<h2><strong>\u751f\u6210\u5927\u89c4\u6a21\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 20, 1000000)<\/p>\n<h2><strong>\u4f7f\u7528\u5411\u91cf\u5316\u64cd\u4f5c<\/strong><\/h2>\n<p>result = vectorized_piecewise_function(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528pandas\u8fdb\u884c\u6570\u636e\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u8868\u683c\u7684\u5904\u7406\uff0cpandas\u5e93\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\u3002\u53ef\u4ee5\u5229\u7528pandas\u7684apply\u65b9\u6cd5\u5bf9\u6570\u636e\u96c6\u7684\u6bcf\u4e00\u884c\u6216\u6bcf\u4e00\u5217\u5e94\u7528\u5206\u6bb5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;value&#39;: [-5, 0, 5, 10, 15]})<\/p>\n<h2><strong>\u5b9a\u4e49\u5206\u6bb5\u51fd\u6570<\/strong><\/h2>\n<p>def piecewise_function(row):<\/p>\n<p>    x = row[&#39;value&#39;]<\/p>\n<p>    if x &lt; 0:<\/p>\n<p>        return -1<\/p>\n<p>    elif 0 &lt;= x &lt;= 10:<\/p>\n<p>        return 2 * x<\/p>\n<p>    else:<\/p>\n<p>        return 3 * x + 1<\/p>\n<h2><strong>\u4f7f\u7528apply\u65b9\u6cd5<\/strong><\/h2>\n<p>df[&#39;result&#39;] = df.apply(piecewise_function, axis=1)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\u4f7f\u7528scipy\u5e93\u5b9e\u73b0\u590d\u6742\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5206\u6bb5\u51fd\u6570\uff0cscipy\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u548c\u4f18\u5316\u8fd9\u4e9b\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528scipy\u7684interp1d\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u63d2\u503c\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5206\u6bb5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.interpolate import interp1d<\/p>\n<h2><strong>\u5b9a\u4e49\u6570\u636e\u70b9<\/strong><\/h2>\n<p>x_points = [-5, 0, 10, 15]<\/p>\n<p>y_points = [-1, 0, 20, 46]<\/p>\n<h2><strong>\u521b\u5efa\u63d2\u503c\u51fd\u6570<\/strong><\/h2>\n<p>f_interp = interp1d(x_points, y_points, kind=&#39;linear&#39;, fill_value=&quot;extrapolate&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u63d2\u503c\u51fd\u6570<\/strong><\/h2>\n<p>print(f_interp(-5))  # \u8f93\u51fa\uff1a-1<\/p>\n<p>print(f_interp(5))   # \u8f93\u51fa\uff1a10<\/p>\n<p>print(f_interp(15))  # \u8f93\u51fa\uff1a46<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5206\u6bb5\u51fd\u6570\u7684\u6027\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u4ece\u800c\u66f4\u597d\u5730\u6ee1\u8db3\u590d\u6742\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u5206\u6bb5\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u7684\u6761\u4ef6\u8bed\u53e5\u6765\u5b9a\u4e49\u5206\u6bb5\u51fd\u6570\u3002\u901a\u8fc7\u4f7f\u7528<code>if<\/code>\u3001<code>elif<\/code>\u548c<code>else<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u6839\u636e\u8f93\u5165\u7684\u4e0d\u540c\u503c\u8fd4\u56de\u4e0d\u540c\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def piecewise_function(x):\n    if x &lt; 0:\n        return &quot;Negative&quot;\n    elif 0 &lt;= x &lt; 10:\n        return &quot;Between 0 and 10&quot;\n    else:\n        return &quot;10 or greater&quot;\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u7ed8\u5236\u5206\u6bb5\u51fd\u6570\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u8981\u7ed8\u5236\u5206\u6bb5\u51fd\u6570\u7684\u56fe\u5f62\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u3002\u9996\u5148\uff0c\u9700\u8981\u5b9a\u4e49\u597d\u5206\u6bb5\u51fd\u6570\uff0c\u7136\u540e\u4f7f\u7528<code>numpy<\/code>\u751f\u6210\u6570\u636e\u70b9\uff0c\u6700\u540e\u7ed8\u5236\u8fd9\u4e9b\u70b9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport matplotlib.pyplot as plt\n\ndef piecewise_function(x):\n    if x &lt; 0:\n        return x**2\n    elif 0 &lt;= x &lt; 10:\n        return x\n    else:\n        return 10\n\nx = np.linspace(-5, 15, 100)\ny = [piecewise_function(i) for i in x]\n\nplt.plot(x, y)\nplt.title(&#39;Piecewise Function&#39;)\nplt.xlabel(&#39;x&#39;)\nplt.ylabel(&#39;f(x)&#39;)\nplt.grid()\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5206\u6bb5\u51fd\u6570\u7684\u8f93\u5165\u503c\uff1f<\/strong><br \/>\u5904\u7406\u5206\u6bb5\u51fd\u6570\u7684\u8f93\u5165\u503c\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u786e\u4fdd\u8f93\u5165\u503c\u7684\u6709\u6548\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u53ef\u80fd\u7684\u9519\u8bef\u5e76\u7ed9\u4e88\u7528\u6237\u53cd\u9988\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_piecewise_function(x):\n    try:\n        x = float(x)\n        if x &lt; 0:\n            return &quot;Negative&quot;\n        elif 0 &lt;= x &lt; 10:\n            return &quot;Between 0 and 10&quot;\n        else:\n            return &quot;10 or greater&quot;\n    except ValueError:\n        return &quot;Invalid input, please enter a number.&quot;\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\u4e0e\u7b54\u6848\uff0c\u7528\u6237\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u548c\u4f7f\u7528\u5206\u6bb5\u51fd\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6253\u5206\u6bb5\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u3001\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528numpy\u7684select\u51fd\u6570\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u6761\u4ef6\u8bed [&hellip;]","protected":false},"author":3,"featured_media":1017347,"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\/1017336"}],"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=1017336"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1017336\/revisions"}],"predecessor-version":[{"id":1017351,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1017336\/revisions\/1017351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1017347"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1017336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1017336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1017336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}