{"id":1086149,"date":"2025-01-08T13:22:57","date_gmt":"2025-01-08T05:22:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1086149.html"},"modified":"2025-01-08T13:22:59","modified_gmt":"2025-01-08T05:22:59","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82a%e5%92%8cb%e7%9a%84%e5%95%86-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1086149.html","title":{"rendered":"python\u5982\u4f55\u6c42a\u548cb\u7684\u5546"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195713\/44662d3f-559f-44e9-adde-590e431bed87.webp\" alt=\"python\u5982\u4f55\u6c42a\u548cb\u7684\u5546\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u6c42a\u548cb\u7684\u5546\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u9664\u6cd5\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u6574\u9664\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570divmod()\u3001\u8003\u8651\u5f02\u5e38\u5904\u7406\u3002<\/strong> \u4eca\u5929\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u548c\u4f7f\u7528\u573a\u666f\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u9664\u6cd5\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6c42\u4e24\u4e2a\u6570\u7684\u5546\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u9664\u6cd5\u8fd0\u7b97\u7b26<code>\/<\/code>\u3002\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6d6e\u70b9\u6570\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 3<\/p>\n<p>result = a \/ b<\/p>\n<p>print(result)  # \u8f93\u51fa: 3.3333333333333335<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9664\u6cd5\u8fd0\u7b97\u7b26\u9002\u7528\u4e8e\u9700\u8981\u7cbe\u786e\u5c0f\u6570\u7ed3\u679c\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u79d1\u5b66\u8ba1\u7b97\u6216\u91d1\u878d\u8ba1\u7b97\u7b49\u3002\u8bf7\u6ce8\u610f\uff0c\u5982\u679c<code>b<\/code>\u4e3a\u96f6\uff0c\u5219\u4f1a\u5f15\u53d1<code>ZeroDivisionError<\/code>\u5f02\u5e38\uff0c\u56e0\u6b64\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u9700\u8981\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6574\u9664\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u53ea\u9700\u8981\u5546\u7684\u6574\u6570\u90e8\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528\u6574\u9664\u8fd0\u7b97\u7b26<code>\/\/<\/code>\u3002\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 3<\/p>\n<p>result = a \/\/ b<\/p>\n<p>print(result)  # \u8f93\u51fa: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6574\u9664\u8fd0\u7b97\u7b26\u9002\u7528\u4e8e\u9700\u8981\u820d\u5f03\u5c0f\u6570\u90e8\u5206\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u8ba1\u7b97\u9875\u9762\u5206\u9875\u3001\u5206\u914d\u4efb\u52a1\u7b49\u3002\u4e0e\u9664\u6cd5\u8fd0\u7b97\u7b26\u4e00\u6837\uff0c<code>b<\/code>\u4e3a\u96f6\u65f6\u4f1a\u5f15\u53d1<code>ZeroDivisionError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570divmod()<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u51fd\u6570<code>divmod()<\/code>\u53ef\u4ee5\u540c\u65f6\u8fd4\u56de\u5546\u548c\u4f59\u6570\u3002\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5546\u548c\u4f59\u6570\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 3<\/p>\n<p>quotient, rem<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nder = divmod(a, b)<\/p>\n<p>print(f&quot;\u5546: {quotient}, \u4f59\u6570: {remainder}&quot;)  # \u8f93\u51fa: \u5546: 3, \u4f59\u6570: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>divmod()<\/code>\u51fd\u6570\u975e\u5e38\u9002\u5408\u9700\u8981\u540c\u65f6\u83b7\u53d6\u5546\u548c\u4f59\u6570\u7684\u60c5\u51b5\uff0c\u5982\u65f6\u95f4\u6362\u7b97\u3001\u5750\u6807\u8f6c\u6362\u7b49\u3002\u540c\u6837\u5730\uff0c<code>b<\/code>\u4e3a\u96f6\u65f6\u4f1a\u5f15\u53d1<code>ZeroDivisionError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8003\u8651\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9664\u6570<code>b<\/code>\u53ef\u80fd\u4e3a\u96f6\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 0<\/p>\n<p>try:<\/p>\n<p>    result = a \/ b<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    print(&quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406<code>ZeroDivisionError<\/code>\u5f02\u5e38\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u6240\u6709\u6d89\u53ca\u9664\u6cd5\u8fd0\u7b97\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u5904\u7406\u7684\u662f\u6570\u7ec4\u6216\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u548c\u66f4\u9ad8\u6548\u7684\u6570\u7ec4\u8ba1\u7b97\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>a = np.array([10, 20, 30])<\/p>\n<p>b = np.array([2, 4, 5])<\/p>\n<p>result = np.divide(a, b)<\/p>\n<p>print(result)  # \u8f93\u51fa: [ 5.  5.  6.]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u5e93\u4e2d\u7684<code>np.divide()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u9010\u4e2a\u9664\u6cd5\u8fd0\u7b97\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002\u5b83\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u548c\u79d1\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u8868\u683c\u65f6\uff0cPandas\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5305\u62ec\u9664\u6cd5\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;a&#39;: [10, 20, 30], &#39;b&#39;: [2, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&#39;result&#39;] = df[&#39;a&#39;] \/ df[&#39;b&#39;]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7Pandas\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6570\u636e\u8868\u683c\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u65b0\u7684\u5217\u4e2d\u3002Pandas\u5e93\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u4e86Python\u4e2d\u6c42a\u548cb\u7684\u5546\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u9664\u6cd5\u8fd0\u7b97\u7b26\u3001\u6574\u9664\u8fd0\u7b97\u7b26\u3001\u5185\u7f6e\u51fd\u6570<code>divmod()<\/code>\u3001\u5f02\u5e38\u5904\u7406\u3001NumPy\u5e93\u548cPandas\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u7279\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u8fdb\u884c\u7b80\u5355\u7684\u6570\u5b66\u8ba1\u7b97\u8fd8\u662f\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u5206\u6790\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u5e2e\u52a9\u60a8\u8f7b\u677e\u89e3\u51b3\u9664\u6cd5\u8fd0\u7b97\u7684\u95ee\u9898\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u60a8\u5728Python\u7f16\u7a0b\u7684\u9053\u8def\u4e0a\u4e0d\u65ad\u8fdb\u6b65\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u7684\u5546\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u9664\u6cd5\u8fd0\u7b97\u7b26 <code>\/<\/code> \u6765\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u7684\u5546\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e24\u4e2a\u53d8\u91cf <code>a<\/code> \u548c <code>b<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>a \/ b<\/code> \u6765\u83b7\u53d6\u5b83\u4eec\u7684\u5546\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53 <code>b<\/code> \u4e3a\u96f6\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\uff0c\u56e0\u6b64\u5728\u6267\u884c\u9664\u6cd5\u4e4b\u524d\uff0c\u786e\u4fdd <code>b<\/code> \u4e0d\u4e3a\u96f6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u9664\u6cd5\u8fd0\u7b97\u4e2d\u7684\u5c0f\u6570\uff1f<\/strong><br \/>Python\u4f1a\u81ea\u52a8\u5904\u7406\u5c0f\u6570\u8fd0\u7b97\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5f97\u5230\u6d6e\u70b9\u6570\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>\/<\/code> \u8fd0\u7b97\u7b26\u3002\u5982\u679c\u4f60\u5e0c\u671b\u5f97\u5230\u6574\u6570\u5546\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>\/\/<\/code> \u8fd0\u7b97\u7b26\uff0c\u8fd9\u6837Python\u4f1a\u8fd4\u56de\u5546\u7684\u6574\u6570\u90e8\u5206\u3002\u4f8b\u5982\uff0c<code>a \/\/ b<\/code> \u4f1a\u8fd4\u56de <code>a<\/code> \u9664\u4ee5 <code>b<\/code> \u7684\u6574\u6570\u90e8\u5206\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u8981\u53d6\u4f59\u6570\uff0c\u8be5\u5982\u4f55\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>%<\/code> \u8fd0\u7b97\u7b26\u6765\u83b7\u53d6\u4e24\u4e2a\u6570\u5b57\u76f8\u9664\u540e\u7684\u4f59\u6570\u3002\u4f8b\u5982\uff0c<code>remainder = a % b<\/code> \u5c06\u4f1a\u8fd4\u56de <code>a<\/code> \u9664\u4ee5 <code>b<\/code> \u7684\u4f59\u6570\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u540c\u65f6\u8ba1\u7b97\u5546\u548c\u4f59\u6570\u7684\u573a\u666f\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u5728\u5904\u7406\u5206\u7ec4\u3001\u5faa\u73af\u6216\u5206\u9875\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u6c42a\u548cb\u7684\u5546\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u9664\u6cd5\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u6574\u9664\u8fd0\u7b97\u7b26\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570divmod()\u3001\u8003\u8651\u5f02\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1086157,"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\/1086149"}],"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=1086149"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086149\/revisions"}],"predecessor-version":[{"id":1086160,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1086149\/revisions\/1086160"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1086157"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1086149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1086149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1086149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}