{"id":977609,"date":"2024-12-27T06:31:00","date_gmt":"2024-12-26T22:31:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/977609.html"},"modified":"2024-12-27T06:31:02","modified_gmt":"2024-12-26T22:31:02","slug":"python-%e5%a6%82%e4%bd%95%e5%88%92%e5%88%86%e5%87%ba%e5%ad%a3%e8%8a%82","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/977609.html","title":{"rendered":"python \u5982\u4f55\u5212\u5206\u51fa\u5b63\u8282"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202910\/4e00a8c3-82b0-470b-9c17-fcfb08f78be3.webp\" alt=\"python \u5982\u4f55\u5212\u5206\u51fa\u5b63\u8282\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5212\u5206\u51fa\u5b63\u8282\u53ef\u4ee5\u901a\u8fc7\u65e5\u671f\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u5e76\u8fdb\u884c\u5224\u65ad\u3001\u4f7f\u7528\u5b57\u5178\u6216\u5217\u8868\u6765\u5b9a\u4e49\u6bcf\u4e2a\u5b63\u8282\u7684\u65e5\u671f\u8303\u56f4\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u7b80\u5316\u5b63\u8282\u5212\u5206\u7684\u8fc7\u7a0b<\/strong>\u3002\u5176\u4e2d\uff0c\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u6765\u5224\u65ad\u65e5\u671f\u5c5e\u4e8e\u54ea\u4e2a\u5b63\u8282\u662f\u4e00\u79cd\u8f83\u4e3a\u7b80\u4fbf\u548c\u76f4\u89c2\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u6708\u4efd\u6765\u5224\u65ad\u5c5e\u4e8e\u54ea\u4e00\u4e2a\u5b63\u8282\uff0c\u6bd4\u5982\uff1a3\u6708\u81f35\u6708\u4e3a\u6625\u5b63\uff0c6\u6708\u81f38\u6708\u4e3a\u590f\u5b63\uff0c9\u6708\u81f311\u6708\u4e3a\u79cb\u5b63\uff0c12\u6708\u81f32\u6708\u4e3a\u51ac\u5b63\u3002\u8fd9\u4e2a\u5212\u5206\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u7684\u65e5\u671f\u8fd4\u56de\u76f8\u5e94\u7684\u5b63\u8282\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u65e5\u671f\u6a21\u5757\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u6a21\u5757datetime\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u53ca\u5176\u76f8\u5173\u4fe1\u606f\u3002\u901a\u8fc7datetime\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u65e5\u671f\u4e2d\u63d0\u53d6\u51fa\u6708\u4efd\uff0c\u7136\u540e\u6839\u636e\u6708\u4efd\u6765\u5224\u65ad\u5f53\u524d\u65e5\u671f\u6240\u5c5e\u7684\u5b63\u8282\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>def get_season(date):<\/p>\n<p>    month = date.month<\/p>\n<p>    if month in [3, 4, 5]:<\/p>\n<p>        return &quot;\u6625\u5b63&quot;<\/p>\n<p>    elif month in [6, 7, 8]:<\/p>\n<p>        return &quot;\u590f\u5b63&quot;<\/p>\n<p>    elif month in [9, 10, 11]:<\/p>\n<p>        return &quot;\u79cb\u5b63&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;\u51ac\u5b63&quot;<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>today = datetime.now()<\/p>\n<p>season = get_season(today)<\/p>\n<p>print(f&quot;\u4eca\u5929\u662f{today.month}\u6708\uff0c\u5c5e\u4e8e{season}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165datetime\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3aget_season\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u65e5\u671f\u5bf9\u8c61\u5e76\u6839\u636e\u6708\u4efd\u8fd4\u56de\u5bf9\u5e94\u7684\u5b63\u8282\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u6216\u5217\u8868\u5b9a\u4e49\u5b63\u8282\u8303\u56f4<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u53ef\u7ef4\u62a4\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5b57\u5178\u6216\u5217\u8868\u6765\u5b9a\u4e49\u5b63\u8282\u7684\u65e5\u671f\u8303\u56f4\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u5b63\u8282\u7684\u5212\u5206\u89c4\u5219\u66f4\u52a0\u6e05\u6670\uff0c\u5e76\u4e14\u6613\u4e8e\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_season_with_dict(date):<\/p>\n<p>    month_to_season = {<\/p>\n<p>        1: &quot;\u51ac\u5b63&quot;, 2: &quot;\u51ac\u5b63&quot;, 3: &quot;\u6625\u5b63&quot;,<\/p>\n<p>        4: &quot;\u6625\u5b63&quot;, 5: &quot;\u6625\u5b63&quot;, 6: &quot;\u590f\u5b63&quot;,<\/p>\n<p>        7: &quot;\u590f\u5b63&quot;, 8: &quot;\u590f\u5b63&quot;, 9: &quot;\u79cb\u5b63&quot;,<\/p>\n<p>        10: &quot;\u79cb\u5b63&quot;, 11: &quot;\u79cb\u5b63&quot;, 12: &quot;\u51ac\u5b63&quot;<\/p>\n<p>    }<\/p>\n<p>    return month_to_season[date.month]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>today = datetime.now()<\/p>\n<p>season = get_season_with_dict(today)<\/p>\n<p>print(f&quot;\u4eca\u5929\u662f{today.month}\u6708\uff0c\u5c5e\u4e8e{season}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178month_to_season\uff0c\u6211\u4eec\u53ef\u4ee5\u6e05\u6670\u5730\u770b\u5230\u6bcf\u4e2a\u6708\u5bf9\u5e94\u7684\u5b63\u8282\uff0c\u8fd9\u6837\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u66f4\u52a0\u76f4\u89c2\uff0c\u800c\u4e14\u5728\u9700\u8981\u4fee\u6539\u5b63\u8282\u5212\u5206\u89c4\u5219\u65f6\uff0c\u4e5f\u53ea\u9700\u8981\u4fee\u6539\u5b57\u5178\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5c01\u88c5\u4e3a\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b63\u8282\u5212\u5206\u7684\u903b\u8f91\u5c01\u88c5\u5230\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u4e2d\uff0c\u8fd9\u6837\u5728\u9700\u8981\u5224\u65ad\u65e5\u671f\u6240\u5c5e\u5b63\u8282\u65f6\uff0c\u53ea\u9700\u8c03\u7528\u8be5\u51fd\u6570\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def determine_season(month):<\/p>\n<p>    if month in [3, 4, 5]:<\/p>\n<p>        return &quot;\u6625\u5b63&quot;<\/p>\n<p>    elif month in [6, 7, 8]:<\/p>\n<p>        return &quot;\u590f\u5b63&quot;<\/p>\n<p>    elif month in [9, 10, 11]:<\/p>\n<p>        return &quot;\u79cb\u5b63&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;\u51ac\u5b63&quot;<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>month = 5<\/p>\n<p>season = determine_season(month)<\/p>\n<p>print(f&quot;{month}\u6708\u5c5e\u4e8e{season}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u4f7f\u5f97\u5b63\u8282\u5224\u65ad\u7684\u903b\u8f91\u66f4\u52a0\u6a21\u5757\u5316\u548c\u6613\u4e8e\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u7279\u6b8a\u65e5\u671f<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5b63\u8282\u7684\u5212\u5206\u53ef\u80fd\u9700\u8981\u8003\u8651\u5230\u66f4\u4e3a\u7279\u6b8a\u7684\u65e5\u671f\u6216\u8282\u6c14\uff0c\u6bd4\u5982\u4e2d\u56fd\u7684\u4e8c\u5341\u56db\u8282\u6c14\u3002\u8fd9\u65f6\u5019\uff0c\u7b80\u5355\u7684\u6708\u4efd\u5212\u5206\u53ef\u80fd\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\uff0c\u9700\u8981\u5f15\u5165\u66f4\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\u6216\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_season_advanced(date):<\/p>\n<p>    day = date.day<\/p>\n<p>    month = date.month<\/p>\n<p>    if (month == 3 and day &gt;= 20) or month in [4, 5] or (month == 6 and day &lt; 21):<\/p>\n<p>        return &quot;\u6625\u5b63&quot;<\/p>\n<p>    elif (month == 6 and day &gt;= 21) or month in [7, 8] or (month == 9 and day &lt; 23):<\/p>\n<p>        return &quot;\u590f\u5b63&quot;<\/p>\n<p>    elif (month == 9 and day &gt;= 23) or month in [10, 11] or (month == 12 and day &lt; 21):<\/p>\n<p>        return &quot;\u79cb\u5b63&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;\u51ac\u5b63&quot;<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>today = datetime.now()<\/p>\n<p>season = get_season_advanced(today)<\/p>\n<p>print(f&quot;\u4eca\u5929\u662f{today.month}\u6708{today.day}\u65e5\uff0c\u5c5e\u4e8e{season}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5f15\u5165\u4e86\u66f4\u4e3a\u7cbe\u7ec6\u7684\u65e5\u671f\u5224\u65ad\u903b\u8f91\uff0c\u901a\u8fc7\u5224\u65ad\u5177\u4f53\u7684\u65e5\u671f\u6765\u786e\u5b9a\u5b63\u8282\u7684\u53d8\u5316\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u9700\u8981\u7cbe\u786e\u5230\u5929\u7684\u5b63\u8282\u5212\u5206\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7ed3\u5408\u5916\u90e8\u5e93<\/p>\n<\/p>\n<p><p>Python\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u4e8e\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\uff0c\u6bd4\u5982pandas\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u65e5\u671f\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u5b63\u8282\u5212\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def get_season_with_pandas(date):<\/p>\n<p>    month = date.month<\/p>\n<p>    if month in [3, 4, 5]:<\/p>\n<p>        return &quot;\u6625\u5b63&quot;<\/p>\n<p>    elif month in [6, 7, 8]:<\/p>\n<p>        return &quot;\u590f\u5b63&quot;<\/p>\n<p>    elif month in [9, 10, 11]:<\/p>\n<p>        return &quot;\u79cb\u5b63&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;\u51ac\u5b63&quot;<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>date_series = pd.to_datetime([&#39;2023-03-21&#39;, &#39;2023-06-15&#39;, &#39;2023-09-30&#39;, &#39;2023-12-01&#39;])<\/p>\n<p>seasons = date_series.map(get_season_with_pandas)<\/p>\n<p>print(seasons)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528pandas\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u4e00\u7cfb\u5217\u65e5\u671f\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u5b63\u8282\u5212\u5206\u3002\u8fd9\u5728\u5904\u7406\u5927\u91cf\u65e5\u671f\u6570\u636e\u65f6\u5c24\u5176\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5212\u5206\u5b63\u8282\uff0c\u4ece\u7b80\u5355\u7684\u6708\u4efd\u5224\u65ad\u5230\u7ed3\u5408\u5916\u90e8\u5e93\u8fdb\u884c\u590d\u6742\u7684\u65e5\u671f\u5904\u7406\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b9e\u73b0\u5b63\u8282\u5212\u5206\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6839\u636e\u65e5\u671f\u5212\u5206\u5b63\u8282\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u83b7\u53d6\u65e5\u671f\u7684\u6708\u4efd\u6765\u5224\u65ad\u5c5e\u4e8e\u54ea\u4e2a\u5b63\u8282\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6625\u5b63\u4e3a3\u6708\u81f35\u6708\uff0c\u590f\u5b63\u4e3a6\u6708\u81f38\u6708\uff0c\u79cb\u5b63\u4e3a9\u6708\u81f311\u6708\uff0c\u51ac\u5b63\u4e3a12\u6708\u81f32\u6708\u3002\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u4e00\u4e2a\u65e5\u671f\uff0c\u5229\u7528\u6761\u4ef6\u8bed\u53e5\u5224\u65ad\u5176\u6240\u5c5e\u7684\u5b63\u8282\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8de8\u5e74\u65e5\u671f\u7684\u5b63\u8282\u5212\u5206\uff1f<\/strong><br \/>\u5728\u5904\u7406\u8de8\u5e74\u65e5\u671f\u65f6\uff0c\u4f9d\u65e7\u53ef\u4ee5\u4f7f\u7528\u6708\u4efd\u6765\u5224\u65ad\u5b63\u8282\u3002\u4f8b\u5982\uff0c1\u6708\u548c2\u6708\u5c5e\u4e8e\u51ac\u5b63\uff0c3\u6708\u5219\u662f\u6625\u5b63\u7684\u5f00\u59cb\u3002\u65e0\u8bba\u8f93\u5165\u7684\u65e5\u671f\u662f\u5728\u65b0\u7684\u4e00\u5e74\u8fd8\u662f\u65e7\u7684\u4e00\u5e74\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u76f8\u540c\u7684\u903b\u8f91\u8fdb\u884c\u5b63\u8282\u7684\u5212\u5206\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u5728Python\u4e2d\u5904\u7406\u5b63\u8282\u76f8\u5173\u7684\u6570\u636e\uff1f<\/strong><br \/>\u9664\u4e86\u6807\u51c6\u7684<code>datetime<\/code>\u5e93\uff0cPython\u4e2d\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u5982<code>pandas<\/code>\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u65f6\u95f4\u5e8f\u5217\u5904\u7406\u529f\u80fd\u3002\u4f7f\u7528<code>pandas<\/code>\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u65e5\u671f\u6570\u636e\u8f6c\u6362\u4e3a\u5b63\u8282\uff0c\u751a\u81f3\u53ef\u4ee5\u5bf9\u5b63\u8282\u8fdb\u884c\u5206\u7ec4\u5206\u6790\uff0c\u5e2e\u52a9\u66f4\u597d\u5730\u5904\u7406\u4e0e\u5b63\u8282\u76f8\u5173\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5212\u5206\u51fa\u5b63\u8282\u53ef\u4ee5\u901a\u8fc7\u65e5\u671f\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u5e76\u8fdb\u884c\u5224\u65ad\u3001\u4f7f\u7528\u5b57\u5178\u6216\u5217\u8868\u6765\u5b9a\u4e49\u6bcf\u4e2a\u5b63\u8282\u7684\u65e5\u671f\u8303\u56f4\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":977615,"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\/977609"}],"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=977609"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977609\/revisions"}],"predecessor-version":[{"id":977618,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/977609\/revisions\/977618"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/977615"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=977609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=977609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=977609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}