{"id":1085134,"date":"2025-01-08T13:13:19","date_gmt":"2025-01-08T05:13:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1085134.html"},"modified":"2025-01-08T13:13:21","modified_gmt":"2025-01-08T05:13:21","slug":"python%e5%a6%82%e4%bd%95%e5%b9%b3%e5%9d%87%e5%88%86%e9%85%8d%e7%ba%a2%e5%8c%85%e4%bb%a3%e7%a0%81-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1085134.html","title":{"rendered":"python\u5982\u4f55\u5e73\u5747\u5206\u914d\u7ea2\u5305\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195147\/999c68f7-2d44-4768-9b30-9c6a5b2dd476.webp\" alt=\"python\u5982\u4f55\u5e73\u5747\u5206\u914d\u7ea2\u5305\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5e73\u5747\u5206\u914d\u7ea2\u5305\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u7528\u7684\u5305\u62ec\uff1a\u7b49\u989d\u5206\u914d\u3001\u968f\u673a\u5206\u914d\u548c\u6309\u6bd4\u4f8b\u5206\u914d\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7b49\u989d\u5206\u914d<\/h3>\n<\/p>\n<p><p>\u7b49\u989d\u5206\u914d\u662f\u6700\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5c06\u7ea2\u5305\u603b\u91d1\u989d\u5747\u5300\u5730\u5206\u914d\u7ed9\u6bcf\u4e00\u4e2a\u4eba\u3002\u5982\u679c\u4e0d\u80fd\u5b8c\u5168\u5747\u5206\uff0c\u5219\u5c06\u5269\u4f59\u7684\u90e8\u5206\u5206\u914d\u7ed9\u524d\u51e0\u4e2a\u4eba\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def distribute_evenly(total_amount, num_people):<\/p>\n<p>    if num_people &lt;= 0:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Number of people must be greater than zero&quot;)<\/p>\n<p>    base_amount = total_amount \/\/ num_people<\/p>\n<p>    remainder = total_amount % num_people<\/p>\n<p>    distribution = [base_amount] * num_people<\/p>\n<p>    for i in range(remainder):<\/p>\n<p>        distribution[i] += 1<\/p>\n<p>    return distribution<\/p>\n<h2><strong>\u6d4b\u8bd5\u7528\u4f8b<\/strong><\/h2>\n<p>total_amount = 100<\/p>\n<p>num_people = 6<\/p>\n<p>print(distribute_evenly(total_amount, num_people))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u968f\u673a\u5206\u914d<\/h3>\n<\/p>\n<p><p>\u968f\u673a\u5206\u914d\u662f\u4e00\u79cd\u8f83\u4e3a\u516c\u5e73\u7684\u5206\u914d\u65b9\u5f0f\uff0c\u6bcf\u4e2a\u4eba\u83b7\u5f97\u7684\u91d1\u989d\u4e0d\u56fa\u5b9a\uff0c\u4f46\u603b\u91d1\u989d\u548c\u4eba\u6570\u662f\u4e00\u5b9a\u7684\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u201c\u4e8c\u500d\u5747\u503c\u6cd5\u201d\u548c\u201c\u7ebf\u6bb5\u5207\u5272\u6cd5\u201d\u3002<\/p>\n<\/p>\n<p><h4>1. \u4e8c\u500d\u5747\u503c\u6cd5<\/h4>\n<\/p>\n<p><p>\u6bcf\u4e2a\u4eba\u83b7\u5f97\u7684\u91d1\u989d\u57280\u5230\u5269\u4f59\u91d1\u989d\u7684\u5747\u503c\u7684\u4e24\u500d\u4e4b\u95f4\u968f\u673a\u5206\u914d\uff0c\u4fdd\u8bc1\u6bcf\u4e2a\u4eba\u90fd\u6709\u673a\u4f1a\u83b7\u5f97\u8f83\u5927\u7684\u91d1\u989d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def distribute_random(total_amount, num_people):<\/p>\n<p>    if num_people &lt;= 0:<\/p>\n<p>        raise ValueError(&quot;Number of people must be greater than zero&quot;)<\/p>\n<p>    distribution = []<\/p>\n<p>    remaining_amount = total_amount<\/p>\n<p>    for i in range(num_people - 1):<\/p>\n<p>        max_amount = (remaining_amount \/ (num_people - i)) * 2<\/p>\n<p>        amount = random.uniform(0.01, max_amount)<\/p>\n<p>        amount = round(amount, 2)<\/p>\n<p>        distribution.append(amount)<\/p>\n<p>        remaining_amount -= amount<\/p>\n<p>    distribution.append(round(remaining_amount, 2))<\/p>\n<p>    return distribution<\/p>\n<h2><strong>\u6d4b\u8bd5\u7528\u4f8b<\/strong><\/h2>\n<p>total_amount = 100<\/p>\n<p>num_people = 6<\/p>\n<p>print(distribute_random(total_amount, num_people))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7ebf\u6bb5\u5207\u5272\u6cd5<\/h4>\n<\/p>\n<p><p>\u5c06\u603b\u91d1\u989d\u89c6\u4e3a\u4e00\u6761\u7ebf\u6bb5\uff0c\u968f\u673a\u751f\u6210num_people &#8211; 1\u4e2a\u5207\u5272\u70b9\uff0c\u5c06\u7ebf\u6bb5\u5207\u5272\u6210num_people\u6bb5\uff0c\u6bcf\u6bb5\u7684\u957f\u5ea6\u5373\u4e3a\u6bcf\u4e2a\u4eba\u83b7\u5f97\u7684\u91d1\u989d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def distribute_cutting(total_amount, num_people):<\/p>\n<p>    if num_people &lt;= 0:<\/p>\n<p>        raise ValueError(&quot;Number of people must be greater than zero&quot;)<\/p>\n<p>    cuts = sorted([random.uniform(0, total_amount) for _ in range(num_people - 1)])<\/p>\n<p>    cuts = [0] + cuts + [total_amount]<\/p>\n<p>    distribution = [round(cuts[i+1] - cuts[i], 2) for i in range(num_people)]<\/p>\n<p>    return distribution<\/p>\n<h2><strong>\u6d4b\u8bd5\u7528\u4f8b<\/strong><\/h2>\n<p>total_amount = 100<\/p>\n<p>num_people = 6<\/p>\n<p>print(distribute_cutting(total_amount, num_people))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6309\u6bd4\u4f8b\u5206\u914d<\/h3>\n<\/p>\n<p><p>\u6309\u6bd4\u4f8b\u5206\u914d\u662f\u6839\u636e\u6bcf\u4e2a\u4eba\u7684\u6743\u91cd\uff08\u5982\u8d21\u732e\u5ea6\u3001\u6210\u7ee9\u7b49\uff09\u5206\u914d\u7ea2\u5305\uff0c\u603b\u91d1\u989d\u6309\u6bd4\u4f8b\u5206\u914d\u7ed9\u6bcf\u4e2a\u4eba\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def distribute_by_ratio(total_amount, ratios):<\/p>\n<p>    if not ratios or sum(ratios) == 0:<\/p>\n<p>        raise ValueError(&quot;Ratios must be non-empty and their sum must be greater than zero&quot;)<\/p>\n<p>    total_ratio = sum(ratios)<\/p>\n<p>    distribution = [round(total_amount * ratio \/ total_ratio, 2) for ratio in ratios]<\/p>\n<p>    # \u8c03\u6574\u8bef\u5dee\uff0c\u4f7f\u603b\u91d1\u989d\u7cbe\u786e<\/p>\n<p>    discrepancy = round(total_amount - sum(distribution), 2)<\/p>\n<p>    distribution[0] += discrepancy<\/p>\n<p>    return distribution<\/p>\n<h2><strong>\u6d4b\u8bd5\u7528\u4f8b<\/strong><\/h2>\n<p>total_amount = 100<\/p>\n<p>ratios = [1, 2, 3, 4, 5, 6]<\/p>\n<p>print(distribute_by_ratio(total_amount, ratios))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u51e0\u79cd\u4e0d\u540c\u7684\u7ea2\u5305\u5206\u914d\u65b9\u6cd5\uff0c\u5305\u62ec\u7b49\u989d\u5206\u914d\u3001\u968f\u673a\u5206\u914d\u548c\u6309\u6bd4\u4f8b\u5206\u914d\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u5206\u914d\u66f4\u52a0\u516c\u5e73\u5408\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u7b49\u989d\u5206\u914d<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u7b80\u5355\u3001\u76f4\u63a5\u3001\u516c\u5e73\u3002<br \/><strong>\u7f3a\u70b9\uff1a<\/strong> \u7f3a\u4e4f\u968f\u673a\u6027\uff0c\u53ef\u80fd\u4e0d\u591f\u6fc0\u52a8\u4eba\u5fc3\u3002<\/p>\n<\/p>\n<p><h4>\u968f\u673a\u5206\u914d<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u589e\u52a0\u4e86\u968f\u673a\u6027\u548c\u8da3\u5473\u6027\uff0c\u6bcf\u4e2a\u4eba\u90fd\u6709\u673a\u4f1a\u83b7\u5f97\u8f83\u5927\u7684\u91d1\u989d\u3002<br \/><strong>\u7f3a\u70b9\uff1a<\/strong> \u53ef\u80fd\u4f1a\u51fa\u73b0\u6781\u7aef\u60c5\u51b5\uff0c\u6709\u4eba\u83b7\u5f97\u592a\u5c11\u6216\u592a\u591a\u3002<\/p>\n<\/p>\n<p><h4>\u6309\u6bd4\u4f8b\u5206\u914d<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u6839\u636e\u6743\u91cd\u8fdb\u884c\u5206\u914d\uff0c\u66f4\u52a0\u5408\u7406\u548c\u516c\u5e73\u3002<br \/><strong>\u7f3a\u70b9\uff1a<\/strong> \u9700\u8981\u4e8b\u5148\u8bbe\u5b9a\u6743\u91cd\uff0c\u53ef\u80fd\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5206\u914d\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u5206\u914d\uff0c\u4ee5\u8fbe\u5230\u66f4\u597d\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u4ee3\u7801\u6765\u5e73\u5747\u5206\u914d\u7ea2\u5305\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u6570\u5b66\u8fd0\u7b97\u548c\u5217\u8868\u64cd\u4f5c\u6765\u5b9e\u73b0\u7ea2\u5305\u7684\u5e73\u5747\u5206\u914d\u3002\u57fa\u672c\u601d\u8def\u662f\u5c06\u603b\u91d1\u989d\u9664\u4ee5\u7ea2\u5305\u7684\u6570\u91cf\uff0c\u5f97\u5230\u6bcf\u4e2a\u7ea2\u5305\u7684\u91d1\u989d\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">def distribute_red_envelope(total_amount, num_red_envelopes):\n    if num_red_envelopes &lt;= 0:\n        return []\n    average_amount = total_amount \/ num_red_envelopes\n    return [average_amount] * num_red_envelopes\n\n# \u793a\u4f8b\ntotal_amount = 100  # \u603b\u91d1\u989d\nnum_red_envelopes = 5  # \u7ea2\u5305\u6570\u91cf\nred_envelopes = distribute_red_envelope(total_amount, num_red_envelopes)\nprint(red_envelopes)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06100\u5143\u5e73\u5747\u5206\u914d\u52305\u4e2a\u7ea2\u5305\u4e2d\uff0c\u8f93\u51fa\u6bcf\u4e2a\u7ea2\u5305\u7684\u91d1\u989d\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u8981\u968f\u673a\u5206\u914d\u7ea2\u5305\uff0cPython\u4ee3\u7801\u8be5\u5982\u4f55\u5b9e\u73b0\uff1f<\/strong><br \/>\u968f\u673a\u5206\u914d\u7ea2\u5305\u7684\u903b\u8f91\u76f8\u5bf9\u590d\u6742\uff0c\u9700\u8981\u786e\u4fdd\u603b\u91d1\u989d\u4e0d\u88ab\u8d85\u51fa\uff0c\u540c\u65f6\u4fdd\u8bc1\u6bcf\u4e2a\u7ea2\u5305\u90fd\u6709\u4e00\u5b9a\u7684\u91d1\u989d\u3002\u53ef\u4ee5\u4f7f\u7528\u968f\u673a\u6570\u751f\u6210\u5668\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import random\n\ndef random_distribute_red_envelope(total_amount, num_red_envelopes):\n    if num_red_envelopes &lt;= 0 or total_amount &lt;= 0:\n        return []\n    red_envelopes = []\n    for _ in range(num_red_envelopes - 1):\n        amount = random.randint(1, total_amount \/\/ (num_red_envelopes - len(red_envelopes)))\n        red_envelopes.append(amount)\n        total_amount -= amount\n    red_envelopes.append(total_amount)  # \u628a\u5269\u4f59\u7684\u91d1\u989d\u5206\u914d\u7ed9\u6700\u540e\u4e00\u4e2a\u7ea2\u5305\n    random.shuffle(red_envelopes)  # \u6253\u4e71\u987a\u5e8f\n    return red_envelopes\n\n# \u793a\u4f8b\ntotal_amount = 100  # \u603b\u91d1\u989d\nnum_red_envelopes = 5  # \u7ea2\u5305\u6570\u91cf\nred_envelopes = random_distribute_red_envelope(total_amount, num_red_envelopes)\nprint(red_envelopes)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5b9e\u73b0\u4e86100\u5143\u57285\u4e2a\u7ea2\u5305\u4e2d\u968f\u673a\u5206\u914d\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u5728\u5206\u914d\u7ea2\u5305\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u6bcf\u4e2a\u7ea2\u5305\u7684\u91d1\u989d\u4e0d\u4e3a\u96f6\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u6bcf\u4e2a\u7ea2\u5305\u7684\u91d1\u989d\u90fd\u5927\u4e8e\u96f6\uff0c\u9700\u5728\u5206\u914d\u65f6\u8fdb\u884c\u4e00\u4e9b\u989d\u5916\u7684\u68c0\u67e5\u3002\u53ef\u4ee5\u5728\u8ba1\u7b97\u91d1\u989d\u65f6\uff0c\u786e\u4fdd\u6bcf\u4e2a\u7ea2\u5305\u81f3\u5c11\u5206\u914d1\u5143\uff0c\u7136\u540e\u518d\u5bf9\u5269\u4f59\u7684\u91d1\u989d\u8fdb\u884c\u968f\u673a\u5206\u914d\u3002\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">def secure_random_distribute_red_envelope(total_amount, num_red_envelopes):\n    if num_red_envelopes &lt;= 0 or total_amount &lt; num_red_envelopes:\n        return []\n    red_envelopes = [1] * num_red_envelopes  # \u6bcf\u4e2a\u7ea2\u5305\u81f3\u5c111\u5143\n    total_amount -= num_red_envelopes  # \u6263\u9664\u6bcf\u4e2a\u7ea2\u5305\u76841\u5143\n    for i in range(num_red_envelopes):\n        if total_amount &gt; 0:\n            add_amount = random.randint(0, total_amount)\n            red_envelopes[i] += add_amount\n            total_amount -= add_amount\n    return red_envelopes\n\n# \u793a\u4f8b\ntotal_amount = 100  # \u603b\u91d1\u989d\nnum_red_envelopes = 5  # \u7ea2\u5305\u6570\u91cf\nred_envelopes = secure_random_distribute_red_envelope(total_amount, num_red_envelopes)\nprint(red_envelopes)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u786e\u4fdd\u4e86\u6bcf\u4e2a\u7ea2\u5305\u7684\u91d1\u989d\u4e0d\u4e3a\u96f6\uff0c\u4e14\u603b\u91d1\u989d\u4ecd\u7136\u5f97\u5230\u5408\u7406\u5206\u914d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5e73\u5747\u5206\u914d\u7ea2\u5305\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5e38\u7528\u7684\u5305\u62ec\uff1a\u7b49\u989d\u5206\u914d\u3001\u968f\u673a\u5206\u914d\u548c\u6309\u6bd4\u4f8b\u5206\u914d\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1085142,"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\/1085134"}],"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=1085134"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085134\/revisions"}],"predecessor-version":[{"id":1085143,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085134\/revisions\/1085143"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1085142"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1085134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1085134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1085134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}