{"id":1009214,"date":"2024-12-27T11:09:50","date_gmt":"2024-12-27T03:09:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1009214.html"},"modified":"2024-12-27T11:09:53","modified_gmt":"2024-12-27T03:09:53","slug":"python%e5%88%86%e6%ae%b5%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1009214.html","title":{"rendered":"python\u5206\u6bb5\u51fd\u6570\u5982\u4f55\u8ba1\u7b97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084231\/a99dd039-c991-4a11-b1e3-c99c0063d886.webp\" alt=\"python\u5206\u6bb5\u51fd\u6570\u5982\u4f55\u8ba1\u7b97\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u548c\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u8ba1\u7b97\u5206\u6bb5\u51fd\u6570\u3001\u5b9e\u73b0\u5206\u6bb5\u51fd\u6570\u8ba1\u7b97\u7684\u5173\u952e\u662f\u5229\u7528\u6761\u4ef6\u8bed\u53e5\uff08\u5982if\u3001elif\u3001else\uff09\u6765\u5224\u65ad\u8f93\u5165\u503c\u5c5e\u4e8e\u54ea\u4e00\u6bb5\uff0c\u7136\u540e\u6267\u884c\u76f8\u5e94\u7684\u8ba1\u7b97\u3001\u6b64\u5916\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u5f62\u5f0f\u7684\u5206\u6bb5\u51fd\u6570<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u5982\u4f55\u5b9e\u73b0\u548c\u8ba1\u7b97\u5206\u6bb5\u51fd\u6570\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b9a\u4e49\u5206\u6bb5\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5206\u6bb5\u51fd\u6570\u662f\u6570\u5b66\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u51fd\u6570\u5f62\u5f0f\uff0c\u5b83\u6839\u636e\u8f93\u5165\u7684\u4e0d\u540c\u533a\u95f4\uff0c\u5e94\u7528\u4e0d\u540c\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\u8fdb\u884c\u8ba1\u7b97\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\uff0c\u5e76\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u5206\u6bb5\u51fd\u6570\u7684\u8ba1\u7b97\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 x2 + 1<\/p>\n<p>    elif 0 &lt;= x &lt; 2:<\/p>\n<p>        return x + 2<\/p>\n<p>    else:<\/p>\n<p>        return 3*x - 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>piecewise_function<\/code>\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5206\u6bb5\u51fd\u6570\u3002\u6839\u636e\u8f93\u5165<code>x<\/code>\u7684\u503c\uff0c\u51fd\u6570\u4f1a\u9009\u62e9\u4e0d\u540c\u7684\u8ba1\u7b97\u516c\u5f0f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u5411\u91cf\u5316\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u624b\u52a8\u7f16\u5199\u5faa\u73af\u6765\u8ba1\u7b97\u6bcf\u4e2a\u8f93\u5165\u503c\u7684\u5206\u6bb5\u51fd\u6570\u53ef\u80fd\u6548\u7387\u4e0d\u9ad8\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528NumPy\u5e93\u8fdb\u884c\u5411\u91cf\u5316\u64cd\u4f5c\u3002NumPy\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u5bf9\u6570\u7ec4\u6267\u884c\u6761\u4ef6\u9009\u62e9\u548c\u8ba1\u7b97\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, (0 &lt;= x) &amp; (x &lt; 2), x &gt;= 2]<\/p>\n<p>    functions = [lambda x: x2 + 1, lambda x: x + 2, lambda x: 3*x - 1]<\/p>\n<p>    return np.piecewise(x, conditions, functions)<\/p>\n<p>x_values = np.array([-3, -1, 0, 1, 2, 3])<\/p>\n<p>result = vectorized_piecewise_function(x_values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>np.piecewise<\/code>\u51fd\u6570\u6839\u636e\u6761\u4ef6\u5217\u8868<code>conditions<\/code>\u9009\u62e9\u5408\u9002\u7684\u51fd\u6570\u6765\u5e94\u7528\u4e8e\u6570\u7ec4<code>x_values<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5e94\u7528\u573a\u666f\u4e0e\u4f18\u52bf<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u5b66\u5efa\u6a21\u4e0e\u79d1\u5b66\u8ba1\u7b97<\/strong><br \/>\u5206\u6bb5\u51fd\u6570\u5728\u6570\u5b66\u5efa\u6a21\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\u5e7f\u6cdb\u5e94\u7528\u3002\u901a\u8fc7Python\u7f16\u5199\u5206\u6bb5\u51fd\u6570\uff0c\u53ef\u4ee5\u6a21\u62df\u73b0\u5b9e\u4e16\u754c\u4e2d\u8bb8\u591a\u975e\u7ebf\u6027\u7cfb\u7edf\u7684\u884c\u4e3a\uff0c\u4f8b\u5982\u7269\u7406\u8fc7\u7a0b\u3001\u7ecf\u6d4e\u6a21\u578b\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5206\u6790\u4e0e<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/strong><br \/>\u5728\u6570\u636e\u5206\u6790\u548c\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4e0d\u540c\u533a\u95f4\u7684\u5904\u7406\u3002\u5206\u6bb5\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u590d\u6742\u7684\u7279\u5f81\u5de5\u7a0b\u548c\u6570\u636e\u9884\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7075\u6d3b\u6027\u4e0e\u53ef\u6269\u5c55\u6027<\/strong><br \/>\u4f7f\u7528Python\u7f16\u5199\u5206\u6bb5\u51fd\u6570\u5177\u6709\u9ad8\u5ea6\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8f7b\u677e\u4fee\u6539\u6761\u4ef6\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5b9e\u4f8b\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5206\u6bb5\u51fd\u6570\u7684\u5b9e\u9645\u5e94\u7528\u4f8b\u5b50\uff0c\u7528\u4e8e\u8ba1\u7b97\u4ea4\u901a\u6536\u8d39\u3002\u6839\u636e\u884c\u9a76\u91cc\u7a0b\u7684\u4e0d\u540c\u533a\u95f4\uff0c\u6536\u8d39\u6807\u51c6\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_toll(distance):<\/p>\n<p>    if distance &lt;= 5:<\/p>\n<p>        return 10<\/p>\n<p>    elif 5 &lt; distance &lt;= 20:<\/p>\n<p>        return 10 + (distance - 5) * 2<\/p>\n<p>    else:<\/p>\n<p>        return 40 + (distance - 20) * 1.5<\/p>\n<p>distances = [3, 10, 15, 25, 30]<\/p>\n<p>tolls = [calculate_toll(d) for d in distances]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>calculate_toll<\/code>\u51fd\u6570\u6839\u636e\u884c\u9a76\u8ddd\u79bb<code>distance<\/code>\u8ba1\u7b97\u5bf9\u5e94\u7684\u6536\u8d39<code>toll<\/code>\u3002\u53ef\u4ee5\u770b\u51fa\uff0c\u901a\u8fc7\u7b80\u5355\u7684\u6761\u4ef6\u5224\u65ad\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u8ba1\u8d39\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8fb9\u754c\u6761\u4ef6\u5904\u7406<\/strong><br \/>\u5728\u7f16\u5199\u5206\u6bb5\u51fd\u6570\u65f6\uff0c\u9700\u7279\u522b\u6ce8\u610f\u8fb9\u754c\u6761\u4ef6\u7684\u5904\u7406\uff0c\u786e\u4fdd\u6bcf\u4e2a\u533a\u95f4\u7684\u6761\u4ef6\u4e92\u4e0d\u91cd\u53e0\u4e14\u65e0\u9057\u6f0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong><br \/>\u867d\u7136\u5206\u6bb5\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u6761\u4ef6\u8bed\u53e5\u5b9e\u73b0\uff0c\u4f46\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\uff0c\u5efa\u8bae\u4fdd\u6301\u6bcf\u4e2a\u6761\u4ef6\u7684\u7b80\u5355\u6027\uff0c\u5e76\u5728\u5fc5\u8981\u65f6\u6dfb\u52a0\u6ce8\u91ca\u8bf4\u660e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u5316<\/strong><br \/>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0c\u5c3d\u91cf\u4f7f\u7528NumPy\u7b49\u79d1\u5b66\u8ba1\u7b97\u5e93\u8fdb\u884c\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u3001\u7075\u6d3b\u5730\u5b9e\u73b0\u548c\u8ba1\u7b97\u5206\u6bb5\u51fd\u6570\u3002\u65e0\u8bba\u662f\u5728\u6570\u5b66\u5efa\u6a21\u3001\u79d1\u5b66\u8ba1\u7b97\u8fd8\u662f\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5206\u6bb5\u51fd\u6570\u90fd\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u5904\u7406\u5404\u79cd\u590d\u6742\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5206\u6bb5\u51fd\u6570\u7684\u8ba1\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\uff08\u5982if-elif-else\uff09\u6765\u5b9e\u73b0\u5206\u6bb5\u51fd\u6570\u7684\u8ba1\u7b97\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u8f93\u5165\u503c\u7684\u4e0d\u540c\uff0c\u5b9a\u4e49\u4e0d\u540c\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u6536\u4e00\u4e2a\u6570\u5b57\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u6839\u636e\u8be5\u6570\u5b57\u7684\u8303\u56f4\u8fd4\u56de\u4e0d\u540c\u7684\u7ed3\u679c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def piecewise_function(x):\n    if x &lt; 0:\n        return &quot;\u8d1f\u6570&quot;\n    elif 0 &lt;= x &lt; 10:\n        return &quot;0\u523010\u4e4b\u95f4&quot;\n    else:\n        return &quot;\u5927\u4e8e\u7b49\u4e8e10&quot;\n<\/code><\/pre>\n<p><strong>\u5728\u5904\u7406\u5206\u6bb5\u51fd\u6570\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u5206\u6bb5\u51fd\u6570\u7684\u4ee3\u7801\u65e2\u6e05\u6670\u53c8\u6613\u4e8e\u7ef4\u62a4\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u6bcf\u4e2a\u5206\u6bb5\u7684\u903b\u8f91\u5c01\u88c5\u4e3a\u5355\u72ec\u7684\u51fd\u6570\uff0c\u5e76\u5728\u4e3b\u51fd\u6570\u4e2d\u8c03\u7528\u8fd9\u4e9b\u5b50\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u6709\u52a9\u4e8e\u51cf\u5c11\u590d\u6742\u6027\uff0c\u5e76\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def segment_one(x):\n    return &quot;\u8d1f\u6570&quot;\n\ndef segment_two(x):\n    return &quot;0\u523010\u4e4b\u95f4&quot;\n\ndef segment_three(x):\n    return &quot;\u5927\u4e8e\u7b49\u4e8e10&quot;\n\ndef piecewise_function(x):\n    if x &lt; 0:\n        return segment_one(x)\n    elif 0 &lt;= x &lt; 10:\n        return segment_two(x)\n    else:\n        return segment_three(x)\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u6709\u5e93\u53ef\u4ee5\u7b80\u5316\u5206\u6bb5\u51fd\u6570\u7684\u8ba1\u7b97\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u4e2d\u6709\u4e00\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u8f7b\u677e\u5730\u5904\u7406\u5206\u6bb5\u51fd\u6570\u3002\u4f8b\u5982\uff0cNumPy\u5e93\u4e2d\u7684<code>np.piecewise()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5b9a\u4e49\u548c\u8ba1\u7b97\u5206\u6bb5\u51fd\u6570\u3002\u4f7f\u7528\u8be5\u51fd\u6570\uff0c\u60a8\u53ea\u9700\u5b9a\u4e49\u6761\u4ef6\u548c\u76f8\u5e94\u7684\u8f93\u51fa\uff0c\u5373\u53ef\u5b9e\u73b0\u5206\u6bb5\u8ba1\u7b97\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nx = np.array([-1, 5, 10])\ny = np.piecewise(x, [x &lt; 0, (0 &lt;= x) &amp; (x &lt; 10), x &gt;= 10], [&quot;\u8d1f\u6570&quot;, &quot;0\u523010\u4e4b\u95f4&quot;, &quot;\u5927\u4e8e\u7b49\u4e8e10&quot;])\nprint(y)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4e5f\u52a0\u901f\u4e86\u8ba1\u7b97\u8fc7\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u548c\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u8ba1\u7b97\u5206\u6bb5\u51fd\u6570\u3001\u5b9e\u73b0\u5206\u6bb5\u51fd\u6570\u8ba1\u7b97\u7684\u5173\u952e\u662f\u5229\u7528\u6761\u4ef6\u8bed\u53e5\uff08\u5982if\u3001eli [&hellip;]","protected":false},"author":3,"featured_media":1009225,"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\/1009214"}],"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=1009214"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009214\/revisions"}],"predecessor-version":[{"id":1009226,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1009214\/revisions\/1009226"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1009225"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1009214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1009214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1009214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}