{"id":1080334,"date":"2025-01-08T12:29:08","date_gmt":"2025-01-08T04:29:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080334.html"},"modified":"2025-01-08T12:29:10","modified_gmt":"2025-01-08T04:29:10","slug":"python%e4%b8%ad%e7%9a%84%e5%a6%82%e4%bd%95%e6%89%be%e5%88%b0%e6%9c%80%e5%b0%8f%e6%97%b6%e9%97%b4-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1080334.html","title":{"rendered":"python\u4e2d\u7684\u5982\u4f55\u627e\u5230\u6700\u5c0f\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182930\/659ba395-2eb6-4234-86f4-c31ab1f7bf27.webp\" alt=\"python\u4e2d\u7684\u5982\u4f55\u627e\u5230\u6700\u5c0f\u65f6\u95f4\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d: <\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982<strong>datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u5e93\u3001min\u51fd\u6570<\/strong>\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528datetime\u6a21\u5757\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65f6\u95f4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u6570\u636e\u3002\u901a\u8fc7\u5c06\u65f6\u95f4\u6570\u636e\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528min\u51fd\u6570\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5728Python\u4e2d\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528DATETIME\u6a21\u5757<\/p>\n<\/p>\n<p><p>datetime\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u6700\u5e38\u7528\u7684\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86datetime\u7c7b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u65f6\u95f4\u6bd4\u8f83\u548c\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u521b\u5efadatetime\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5c06\u65f6\u95f4\u6570\u636e\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528datetime.datetime.strptime()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>time_strings = [&quot;2023-03-25 14:30:00&quot;, &quot;2023-03-25 12:45:00&quot;, &quot;2023-03-25 16:00:00&quot;]<\/p>\n<p>time_objects = [datetime.strptime(time, &quot;%Y-%m-%d %H:%M:%S&quot;) for time in time_strings]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u627e\u5230\u6700\u5c0f\u65f6\u95f4<\/p>\n<\/p>\n<p><p>\u5c06\u65f6\u95f4\u6570\u636e\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528min()\u51fd\u6570\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">min_time = min(time_objects)<\/p>\n<p>print(&quot;\u6700\u5c0f\u65f6\u95f4\u662f\uff1a&quot;, min_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528TIME\u6a21\u5757<\/p>\n<\/p>\n<p><p>time\u6a21\u5757\u662fPython\u4e2d\u53e6\u4e00\u4e2a\u5904\u7406\u65f6\u95f4\u7684\u6a21\u5757\u3002\u867d\u7136\u5b83\u529f\u80fd\u4e0d\u5982datetime\u6a21\u5757\u5f3a\u5927\uff0c\u4f46\u5728\u67d0\u4e9b\u7b80\u5355\u573a\u666f\u4e0b\u4ecd\u7136\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u521b\u5efa\u65f6\u95f4\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528time.strptime()\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>time_strings = [&quot;14:30:00&quot;, &quot;12:45:00&quot;, &quot;16:00:00&quot;]<\/p>\n<p>time_objects = [time.strptime(time, &quot;%H:%M:%S&quot;) for time in time_strings]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u627e\u5230\u6700\u5c0f\u65f6\u95f4<\/p>\n<\/p>\n<p><p>\u5c06\u65f6\u95f4\u6570\u636e\u8f6c\u6362\u4e3a\u65f6\u95f4\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528min()\u51fd\u6570\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">min_time = min(time_objects)<\/p>\n<p>print(&quot;\u6700\u5c0f\u65f6\u95f4\u662f\uff1a&quot;, time.strftime(&quot;%H:%M:%S&quot;, min_time))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PANDAS\u5e93<\/p>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u4f7f\u7528pandas\u53ef\u4ee5\u65b9\u4fbf\u5730\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u521b\u5efa\u65f6\u95f4\u6570\u636e<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5c06\u65f6\u95f4\u6570\u636e\u8f6c\u6362\u4e3apandas\u7684DatetimeIndex\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>time_strings = [&quot;2023-03-25 14:30:00&quot;, &quot;2023-03-25 12:45:00&quot;, &quot;2023-03-25 16:00:00&quot;]<\/p>\n<p>time_series = pd.to_datetime(time_strings)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u627e\u5230\u6700\u5c0f\u65f6\u95f4<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pandas\u7684min()\u65b9\u6cd5\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">min_time = time_series.min()<\/p>\n<p>print(&quot;\u6700\u5c0f\u65f6\u95f4\u662f\uff1a&quot;, min_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528MIN\u51fd\u6570<\/p>\n<\/p>\n<p><p>min\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u627e\u5230\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6700\u5c0f\u503c\u3002\u5728\u5904\u7406\u65f6\u95f4\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u5c06\u65f6\u95f4\u6570\u636e\u8f6c\u6362\u4e3a\u5408\u9002\u7684\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528min\u51fd\u6570\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u7684\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5982\u679c\u65f6\u95f4\u6570\u636e\u662f\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528min\u51fd\u6570\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">time_strings = [&quot;14:30:00&quot;, &quot;12:45:00&quot;, &quot;16:00:00&quot;]<\/p>\n<p>min_time = min(time_strings)<\/p>\n<p>print(&quot;\u6700\u5c0f\u65f6\u95f4\u662f\uff1a&quot;, min_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u7684\u6570\u503c<\/p>\n<\/p>\n<p><p>\u5982\u679c\u65f6\u95f4\u6570\u636e\u662f\u6570\u503c\uff08\u5982\u65f6\u95f4\u6233\uff09\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528min\u51fd\u6570\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">time_stamps = [1616686200, 1616678700, 1616690400]<\/p>\n<p>min_time = min(time_stamps)<\/p>\n<p>print(&quot;\u6700\u5c0f\u65f6\u95f4\u662f\uff1a&quot;, min_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u7efc\u5408\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u5bf9\u4e0d\u540c\u65b9\u6cd5\u7684\u7efc\u5408\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<p><p>1\u3001datetime\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u652f\u6301\u591a\u79cd\u65f6\u95f4\u64cd\u4f5c\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u65f6\u95f4\u5904\u7406\u573a\u666f\u3002<\/p>\n<p>\u7f3a\u70b9\uff1a\u9700\u8981\u624b\u52a8\u8f6c\u6362\u65f6\u95f4\u683c\u5f0f\uff0c\u4ee3\u7801\u8f83\u4e3a\u5197\u957f\u3002<\/p>\n<\/p>\n<p><p>2\u3001time\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u65f6\u95f4\u5904\u7406\u573a\u666f\u3002<\/p>\n<p>\u7f3a\u70b9\uff1a\u529f\u80fd\u6709\u9650\uff0c\u4e0d\u652f\u6301\u590d\u6742\u7684\u65f6\u95f4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>3\u3001pandas\u5e93<\/p>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u529f\u80fd\u4e30\u5bcc\uff0c\u4ee3\u7801\u7b80\u6d01\u3002<\/p>\n<p>\u7f3a\u70b9\uff1a\u9700\u8981\u5b89\u88c5pandas\u5e93\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>4\u3001min\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u65f6\u95f4\u6bd4\u8f83\u573a\u666f\u3002<\/p>\n<p>\u7f3a\u70b9\uff1a\u529f\u80fd\u6709\u9650\uff0c\u4e0d\u9002\u7528\u4e8e\u590d\u6742\u7684\u65f6\u95f4\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u5728Python\u4e2d\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u683c\u5f0f\uff0c\u53ef\u4ee5\u9009\u62e9datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u5e93\u6216min\u51fd\u6570\u6765\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6bd4\u8f83\u591a\u4e2a\u65f6\u95f4\u5e76\u627e\u5230\u6700\u5c0f\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u5904\u7406\u65f6\u95f4\u3002\u901a\u8fc7\u5c06\u591a\u4e2a\u65f6\u95f4\u5bf9\u8c61\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528<code>min()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u65f6\u95f4\u7684\u5217\u8868\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9<code>min()<\/code>\u51fd\u6570\uff0c\u4ece\u800c\u83b7\u5f97\u6700\u65e9\u7684\u65f6\u95f4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u65f6\u95f4\u4ee5\u627e\u5230\u6700\u5c0f\u65f6\u95f4\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u7684\u65f6\u95f4\u4ee5\u5b57\u7b26\u4e32\u683c\u5f0f\u5b58\u50a8\uff0c\u4f8b\u5982&quot;2023-10-01 14:30:00&quot;\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strptime()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u3002\u5b8c\u6210\u8f6c\u6362\u540e\uff0c\u4f9d\u7136\u53ef\u4ee5\u5229\u7528<code>min()<\/code>\u51fd\u6570\u6765\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u3002\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u786e\u4fdd\u4f60\u7684\u65f6\u95f4\u6bd4\u8f83\u662f\u51c6\u786e\u7684\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49\u65f6\u95f4\u6bd4\u8f83\u7684\u903b\u8f91\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u5982\u679c\u9700\u8981\u6839\u636e\u7279\u5b9a\u7684\u9700\u6c42\u81ea\u5b9a\u4e49\u65f6\u95f4\u6bd4\u8f83\u903b\u8f91\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u7ed3\u5408\u81ea\u5b9a\u4e49\u7684\u952e\u51fd\u6570\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8bf4\u660e\u5982\u4f55\u6bd4\u8f83\u65f6\u95f4\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u6216\u7c7b\u578b\u7684\u65f6\u95f4\u6570\u636e\uff0c\u627e\u5230\u6240\u9700\u7684\u6700\u5c0f\u65f6\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d: \u5728Python\u4e2d\u627e\u5230\u6700\u5c0f\u65f6\u95f4\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1080343,"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\/1080334"}],"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=1080334"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080334\/revisions"}],"predecessor-version":[{"id":1080344,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080334\/revisions\/1080344"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080343"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}