{"id":1087496,"date":"2025-01-08T13:35:36","date_gmt":"2025-01-08T05:35:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1087496.html"},"modified":"2025-01-08T13:35:38","modified_gmt":"2025-01-08T05:35:38","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%95%b0%e5%a4%9a%e4%b8%aa%e8%8c%83%e5%9b%b4%e5%b9%b6%e9%9b%86-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1087496.html","title":{"rendered":"\u5982\u4f55\u7528Python\u6570\u591a\u4e2a\u8303\u56f4\u5e76\u96c6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200410\/ad67e307-58c5-4a94-bceb-f80ced31fcc3.webp\" alt=\"\u5982\u4f55\u7528Python\u6570\u591a\u4e2a\u8303\u56f4\u5e76\u96c6\" \/><\/p>\n<p><p> <strong>\u7528Python\u6570\u591a\u4e2a\u8303\u56f4\u5e76\u96c6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5c06\u533a\u95f4\u5408\u5e76\u3001\u8ba1\u7b97\u5408\u5e76\u540e\u7684\u533a\u95f4\u603b\u957f\u5ea6\u3001\u4f7f\u7528\u6392\u5e8f\u548c\u904d\u5386\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u533a\u95f4\u5e76\u96c6\u8ba1\u7b97\u95ee\u9898\u3002<\/strong> \u5176\u4e2d\uff0c\u5408\u5e76\u533a\u95f4\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u5177\u4f53\u64cd\u4f5c\u5305\u62ec\u6392\u5e8f\u533a\u95f4\u3001\u5408\u5e76\u91cd\u53e0\u533a\u95f4\u3001\u8ba1\u7b97\u5e76\u96c6\u957f\u5ea6\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5177\u4f53\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5c06\u533a\u95f4\u5408\u5e76<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u4e2a\u533a\u95f4\u7684\u5e76\u96c6\u65f6\uff0c\u9996\u5148\u9700\u8981\u5c06\u8fd9\u4e9b\u533a\u95f4\u8fdb\u884c\u5408\u5e76\u3002\u5408\u5e76\u533a\u95f4\u7684\u8fc7\u7a0b\u4e3b\u8981\u5305\u62ec\u4e24\u4e2a\u6b65\u9aa4\uff1a\u6392\u5e8f\u548c\u5408\u5e76\u91cd\u53e0\u533a\u95f4\u3002<\/p>\n<\/p>\n<p><h4>1. \u6392\u5e8f\u533a\u95f4<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5bf9\u8f93\u5165\u7684\u533a\u95f4\u8fdb\u884c\u6392\u5e8f\uff0c\u901a\u5e38\u662f\u6309\u7167\u6bcf\u4e2a\u533a\u95f4\u7684\u8d77\u70b9\u8fdb\u884c\u6392\u5e8f\u3002\u5982\u679c\u8d77\u70b9\u76f8\u540c\uff0c\u5219\u6309\u7167\u7ec8\u70b9\u6392\u5e8f\u3002Python \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6392\u5e8f\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>sorted<\/code> \u51fd\u6570\u5bf9\u533a\u95f4\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">intervals = [(1, 3), (2, 6), (8, 10), (15, 18)]<\/p>\n<p>intervals.sort(key=lambda x: (x[0], x[1]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5408\u5e76\u91cd\u53e0\u533a\u95f4<\/h4>\n<\/p>\n<p><p>\u5728\u6392\u5e8f\u540e\u7684\u533a\u95f4\u5217\u8868\u4e2d\uff0c\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e2a\u533a\u95f4\uff0c\u5408\u5e76\u6240\u6709\u91cd\u53e0\u7684\u533a\u95f4\u3002\u5408\u5e76\u7684\u65b9\u6cd5\u662f\uff0c\u5982\u679c\u5f53\u524d\u533a\u95f4\u7684\u8d77\u70b9\u5c0f\u4e8e\u6216\u7b49\u4e8e\u524d\u4e00\u4e2a\u533a\u95f4\u7684\u7ec8\u70b9\uff0c\u5219\u5c06\u5b83\u4eec\u5408\u5e76\u6210\u4e00\u4e2a\u533a\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">merged_intervals = []<\/p>\n<p>for interval in intervals:<\/p>\n<p>    if not merged_intervals or merged_intervals[-1][1] &lt; interval[0]:<\/p>\n<p>        merged_intervals.append(interval)<\/p>\n<p>    else:<\/p>\n<p>        merged_intervals[-1] = (merged_intervals[-1][0], max(merged_intervals[-1][1], interval[1]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8ba1\u7b97\u5408\u5e76\u540e\u7684\u533a\u95f4\u603b\u957f\u5ea6<\/h3>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u533a\u95f4\u5408\u5e76\u4e4b\u540e\uff0c\u8ba1\u7b97\u6240\u6709\u5408\u5e76\u533a\u95f4\u7684\u603b\u957f\u5ea6\u3002\u5408\u5e76\u540e\u7684\u533a\u95f4\u5217\u8868\u4e2d\uff0c\u6bcf\u4e2a\u533a\u95f4\u7684\u957f\u5ea6\u4e3a\u5176\u7ec8\u70b9\u51cf\u53bb\u8d77\u70b9\u7684\u503c\uff0c\u6240\u6709\u533a\u95f4\u957f\u5ea6\u7684\u548c\u5373\u4e3a\u5e76\u96c6\u7684\u603b\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total_length = sum(end - start for start, end in merged_intervals)<\/p>\n<p>print(f&quot;Total length of merged intervals: {total_length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u8ba1\u7b97\u591a\u4e2a\u533a\u95f4\u7684\u5e76\u96c6\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_intervals(intervals):<\/p>\n<p>    # \u6392\u5e8f\u533a\u95f4<\/p>\n<p>    intervals.sort(key=lambda x: (x[0], x[1]))<\/p>\n<p>    merged_intervals = []<\/p>\n<p>    # \u5408\u5e76\u533a\u95f4<\/p>\n<p>    for interval in intervals:<\/p>\n<p>        if not merged_intervals or merged_intervals[-1][1] &lt; interval[0]:<\/p>\n<p>            merged_intervals.append(interval)<\/p>\n<p>        else:<\/p>\n<p>            merged_intervals[-1] = (merged_intervals[-1][0], max(merged_intervals[-1][1], interval[1]))<\/p>\n<p>    return merged_intervals<\/p>\n<p>def total_length_of_intervals(intervals):<\/p>\n<p>    merged_intervals = merge_intervals(intervals)<\/p>\n<p>    total_length = sum(end - start for start, end in merged_intervals)<\/p>\n<p>    return total_length<\/p>\n<h2><strong>\u793a\u4f8b\u533a\u95f4\u5217\u8868<\/strong><\/h2>\n<p>intervals = [(1, 3), (2, 6), (8, 10), (15, 18)]<\/p>\n<p>print(f&quot;Original intervals: {intervals}&quot;)<\/p>\n<p>merged_intervals = merge_intervals(intervals)<\/p>\n<p>print(f&quot;Merged intervals: {merged_intervals}&quot;)<\/p>\n<p>total_length = total_length_of_intervals(intervals)<\/p>\n<p>print(f&quot;Total length of merged intervals: {total_length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1. \u8d44\u6e90\u8c03\u5ea6<\/h4>\n<\/p>\n<p><p>\u5728\u8d44\u6e90\u8c03\u5ea6\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5904\u7406\u65f6\u95f4\u533a\u95f4\u7684\u5e76\u96c6\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728\u8ba1\u7b97\u673a\u96c6\u7fa4\u4e2d\uff0c\u4efb\u52a1\u8c03\u5ea6\u5668\u9700\u8981\u786e\u5b9a\u67d0\u4e2a\u65f6\u95f4\u6bb5\u5185\u6709\u591a\u5c11\u8d44\u6e90\u88ab\u5360\u7528\uff0c\u8ba1\u7b97\u591a\u4e2a\u4efb\u52a1\u7684\u65f6\u95f4\u533a\u95f4\u5e76\u96c6\u53ef\u4ee5\u5e2e\u52a9\u8c03\u5ea6\u5668\u4f18\u5316\u8d44\u6e90\u5206\u914d\u3002<\/p>\n<\/p>\n<p><h4>2. \u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u5904\u7406\u7ecf\u5e38\u9700\u8981\u8ba1\u7b97\u65f6\u95f4\u533a\u95f4\u7684\u5e76\u96c6\u3002\u4f8b\u5982\uff0c\u5728\u7528\u6237\u884c\u4e3a\u5206\u6790\u4e2d\uff0c\u53ef\u4ee5\u8ba1\u7b97\u7528\u6237\u8bbf\u95ee\u67d0\u4e2a\u9875\u9762\u7684\u603b\u65f6\u95f4\uff0c\u5408\u5e76\u91cd\u53e0\u7684\u8bbf\u95ee\u65f6\u95f4\u533a\u95f4\u53ef\u4ee5\u5f97\u5230\u66f4\u51c6\u786e\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>3. \u65e5\u7a0b\u5b89\u6392<\/h4>\n<\/p>\n<p><p>\u5728\u65e5\u7a0b\u5b89\u6392\u4e2d\uff0c\u591a\u4e2a\u6d3b\u52a8\u7684\u65f6\u95f4\u533a\u95f4\u53ef\u80fd\u4f1a\u91cd\u53e0\uff0c\u8ba1\u7b97\u8fd9\u4e9b\u6d3b\u52a8\u7684\u5e76\u96c6\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7ba1\u7406\u65f6\u95f4\uff0c\u907f\u514d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u533a\u95f4\u6570\u636e\u65f6\uff0c\u6392\u5e8f\u548c\u5408\u5e76\u64cd\u4f5c\u7684\u6027\u80fd\u53ef\u80fd\u6210\u4e3a\u74f6\u9888\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6027\u80fd\u4f18\u5316\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<p><h4>1. \u9ad8\u6548\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u9ad8\u6548\u7684\u6392\u5e8f\u7b97\u6cd5\uff08\u5982\u5feb\u901f\u6392\u5e8f\uff09\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002Python \u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570 <code>sorted<\/code> \u4f7f\u7528 Timsort \u7b97\u6cd5\uff0c\u5177\u6709\u8f83\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>2. \u5e76\u884c\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u6280\u672f\uff0c\u5c06\u533a\u95f4\u6570\u636e\u5212\u5206\u4e3a\u591a\u4e2a\u5b50\u96c6\uff0c\u5206\u522b\u8fdb\u884c\u6392\u5e8f\u548c\u5408\u5e76\u64cd\u4f5c\uff0c\u6700\u540e\u5408\u5e76\u6240\u6709\u5b50\u96c6\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>3. \u6570\u636e\u7ed3\u6784\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\u7ebf\u6bb5\u6811\u3001\u5e73\u8861\u6811\uff09\u53ef\u4ee5\u63d0\u9ad8\u5408\u5e76\u533a\u95f4\u7684\u6548\u7387\u3002\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u5feb\u901f\u67e5\u627e\u548c\u5408\u5e76\u91cd\u53e0\u533a\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u8ba1\u7b97\u591a\u4e2a\u533a\u95f4\u7684\u5e76\u96c6\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4f7f\u7528 Python \u5b9e\u73b0\u8be5\u529f\u80fd\u7684\u65b9\u6cd5\u3002\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u6392\u5e8f\u533a\u95f4\u3001\u5408\u5e76\u91cd\u53e0\u533a\u95f4\u3001\u8ba1\u7b97\u5408\u5e76\u540e\u7684\u533a\u95f4\u603b\u957f\u5ea6\u3002\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u4e86\u5177\u4f53\u5b9e\u73b0\u8fc7\u7a0b\uff0c\u5e76\u8ba8\u8bba\u4e86\u5e94\u7528\u573a\u666f\u548c\u6027\u80fd\u4f18\u5316\u5efa\u8bae\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u533a\u95f4\u5e76\u96c6\u7684\u8ba1\u7b97\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u591a\u4e2a\u8303\u56f4\u7684\u5e76\u96c6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u5904\u7406\u8303\u56f4\u7684\u5e76\u96c6\u3002\u901a\u8fc7\u5c06\u6bcf\u4e2a\u8303\u56f4\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u4f7f\u7528\u96c6\u5408\u7684\u5e76\u96c6\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5f97\u5230\u591a\u4e2a\u8303\u56f4\u7684\u5e76\u96c6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>range()<\/code>\u51fd\u6570\u751f\u6210\u8303\u56f4\uff0c\u5e76\u901a\u8fc7\u96c6\u5408\u7684<code>union()<\/code>\u65b9\u6cd5\u6765\u8ba1\u7b97\u5e76\u96c6\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5904\u7406\u91cd\u53e0\u8303\u56f4\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5904\u7406\u91cd\u53e0\u8303\u56f4\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u6392\u5e8f\u548c\u5408\u5e76\u7684\u7b56\u7565\u3002\u53ef\u4ee5\u5c06\u6240\u6709\u8303\u56f4\u6309\u8d77\u59cb\u503c\u6392\u5e8f\uff0c\u7136\u540e\u904d\u5386\u8fd9\u4e9b\u8303\u56f4\uff0c\u5408\u5e76\u91cd\u53e0\u7684\u90e8\u5206\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u5e76\u786e\u4fdd\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8303\u56f4\u7684\u8f93\u5165\u548c\u8f93\u51fa\uff1f<\/strong><br \/>\u5bf9\u4e8e\u8303\u56f4\u7684\u8f93\u5165\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u6216\u5143\u7ec4\u7684\u5f62\u5f0f\u6765\u8868\u793a\u3002\u5728\u8f93\u51fa\u65b9\u9762\uff0c\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c55\u793a\u7ed3\u679c\u3002\u5982\u679c\u9700\u8981\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u5c06\u7ed3\u679c\u5199\u5165\u6587\u672c\u6587\u4ef6\u6216CSV\u683c\u5f0f\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u4f7f\u7528\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u6570\u591a\u4e2a\u8303\u56f4\u5e76\u96c6\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5c06\u533a\u95f4\u5408\u5e76\u3001\u8ba1\u7b97\u5408\u5e76\u540e\u7684\u533a\u95f4\u603b\u957f\u5ea6\u3001\u4f7f\u7528\u6392\u5e8f\u548c\u904d\u5386\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6709 [&hellip;]","protected":false},"author":3,"featured_media":1087502,"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\/1087496"}],"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=1087496"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087496\/revisions"}],"predecessor-version":[{"id":1087504,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087496\/revisions\/1087504"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1087502"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1087496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1087496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1087496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}