{"id":104,"date":"2026-08-06T00:30:07","date_gmt":"2026-08-05T16:30:07","guid":{"rendered":"https:\/\/ltrainyard.icu\/?p=104"},"modified":"2026-08-06T00:44:52","modified_gmt":"2026-08-05T16:44:52","slug":"day-19-%e5%b8%b8%e7%94%a8%e6%a0%87%e5%87%86%e5%ba%93%ef%bc%9adatetime-collections-itertools-re","status":"publish","type":"post","link":"https:\/\/ltrainyard.icu\/index.php\/2026\/08\/06\/day-19-%e5%b8%b8%e7%94%a8%e6%a0%87%e5%87%86%e5%ba%93%ef%bc%9adatetime-collections-itertools-re\/","title":{"rendered":"| Day 19 | \u5e38\u7528\u6807\u51c6\u5e93\uff1adatetime \/ collections \/ itertools \/ re |"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1 datetime \u2014\u2014 \u65f6\u95f4\u5bf9\u8c61\u662f\u600e\u4e48\u300c\u8fd0\u8f6c\u300d\u7684<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>datetime \u6a21\u5757<\/strong>\u662f Python \u5904\u7406\u65e5\u671f\u65f6\u95f4\u7684\u5b98\u65b9\u6807\u51c6\u5e93\uff0c\u6838\u5fc3\u6709\u4e09\u4e2a\u7c7b\uff1a<code>datetime<\/code>\uff08\u65e5\u671f + \u65f6\u95f4\uff09\u3001<code>date<\/code>\uff08\u53ea\u8981\u65e5\u671f\uff09\u3001<code>timedelta<\/code>\uff08\u65f6\u95f4\u5dee\uff09\u3002\u65e5\u5fd7\u65f6\u95f4\u6233\u3001\u751f\u65e5\u8ba1\u7b97\u3001\u8ba2\u5355\u8d85\u65f6\u5224\u65ad\u3001\u62a5\u8868\u6309\u5929\u7edf\u8ba1\u2026\u2026\u51e1\u662f\u548c\u65f6\u95f4\u6709\u5173\u7684\u6d3b\u513f\u90fd\u7ed5\u4e0d\u5f00\u5b83\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u65f6\u95f4\u5728 Python \u91cc\u662f<strong>\u5bf9\u8c61<\/strong>\uff0c\u4e0d\u662f\u5b57\u7b26\u4e32\u3002<code>datetime<\/code> \u5bf9\u8c61\u5185\u90e8\u5b58\u7740\u5e74\u3001\u6708\u3001\u65e5\u3001\u65f6\u3001\u5206\u3001\u79d2\u3001\u5fae\u79d2\u5b57\u6bb5\uff0c<code>datetime(2026, 8, 4, 9, 30, 45)<\/code> \u6309\u4f4d\u7f6e\u4f20\u5165\u5373\u53ef\u6784\u9020\uff1b<code>date<\/code> \u662f\u5b83\u7684\u300c\u7cbe\u7b80\u7248\u300d\uff08\u6ca1\u6709\u65f6\u5206\u79d2\uff09\u3002**<code>timedelta<\/code> \u662f\u300c\u65f6\u95f4\u5dee\u5bf9\u8c61\u300d**\uff1a<code>datetime - datetime<\/code> \u5f97\u5230 <code>timedelta<\/code>\uff0c<code>datetime + timedelta<\/code> \u5f97\u5230\u65b0\u65f6\u95f4\u2014\u2014\u65f6\u95f4\u8fd0\u7b97\u7684\u672c\u8d28\u662f\u5bf9\u8c61\u52a0\u51cf\u3002<code>strftime<\/code>\uff08f = format\uff09\u662f\u300c\u65f6\u95f4 \u2192 \u5b57\u7b26\u4e32\u300d\uff0c<code>strptime<\/code>\uff08p = parse\uff09\u662f\u300c\u5b57\u7b26\u4e32 \u2192 \u65f6\u95f4\u300d\uff0c\u65b9\u5411\u76f8\u53cd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>datetime<\/code> \u50cf<strong>\u7535\u5b50\u65e5\u5386\u4e0a\u7684\u4e00\u683c<\/strong>\uff0c<code>date<\/code> \u50cf\u53ea\u5199\u65e5\u671f\u7684\u7eb8\u8d28\u65e5\u5386\uff0c<code>timedelta<\/code> \u50cf<strong>\u6495\u65e5\u5386\u7684\u95f4\u9694<\/strong>\u2014\u2014\u300c\u4eca\u5929\u52a0 7 \u5929\u300d\u5c31\u662f\u5f80\u540e\u7ffb 7 \u683c\uff0c\u8de8\u6708\u3001\u8de8\u5e74\u3001\u95f0\u5e74\u81ea\u52a8\u5904\u7406\uff0c\u4e0d\u7528\u81ea\u5df1\u6570\u5929\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u6784\u9020\u65f6\u95f4\u5bf9\u8c61 + \u65e5\u671f\u52a0\u51cf + \u5dee\u503c\u6bd4\u8f83\uff08\u4e00\u6761\u9f99\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from datetime import datetime, timedelta\ndt = datetime(2026, 8, 4, 9, 30, 45)   # \u5e74\u3001\u6708\u3001\u65e5\u3001\u65f6\u3001\u5206\u3001\u79d2\nprint(dt)\nprint(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)\nprint(dt.strftime(\"%Y-%m-%d %H:%M:%S\"))   # \u683c\u5f0f\u5316\u6210\u5b57\u7b26\u4e32\nd1 = datetime(2026, 8, 10, 9, 30, 45)\ndiff = d1 - dt\nprint(\"\u5dee\u591a\u5c11\u5929:\", diff.days)\nprint(\"\u603b\u79d2\u6570:\", int(diff.total_seconds()))\nprint(\"\u4e00\u5468\u524d:\", dt - timedelta(days=7))\nprint(\"d1 \u5728 dt \u4e4b\u540e:\", d1 &gt; dt)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2026-08-04 09:30:45\n2026 8 4 9 30 45\n2026-08-04 09:30:45\n\u5dee\u591a\u5c11\u5929: 6\n\u603b\u79d2\u6570: 518400\n\u4e00\u5468\u524d: 2026-07-28 09:30:45\nd1 \u5728 dt \u4e4b\u540e: True\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6309\u300c\u5e74\u6708\u65e5\u65f6\u5206\u79d2\u300d\u6784\u9020\u5bf9\u8c61\uff0c<code>.year<\/code> \u7b49\u5c5e\u6027\u53d6\u51fa\u5404\u5b57\u6bb5\uff0c<code>strftime<\/code> \u53d8\u6210\u5b57\u7b26\u4e32\uff1b<code>d1 - dt<\/code> \u5f97\u5230\u65f6\u95f4\u5dee\u5bf9\u8c61\uff0c<code>.days<\/code> \u53d6\u5929\u6570\u3001<code>.total_seconds()<\/code> \u6362\u7b97\u603b\u79d2\u6570\uff1b<code>dt - timedelta(days=7)<\/code> \u5f80\u56de\u7ffb 7 \u5929\uff08\u8de8\u6708\u81ea\u52a8\u5904\u7406\uff09\uff1b\u65f6\u95f4\u5bf9\u8c61\u8fd8\u80fd\u76f4\u63a5\u6bd4\u5927\u5c0f\u2014\u2014<strong>\u65f6\u95f4\u662f\u5bf9\u8c61\uff1a\u53ef\u6784\u9020\u3001\u53ef\u8ba1\u7b97\u3001\u53ef\u6392\u5e8f\uff0c\u5b57\u7b26\u4e32\u53ea\u662f\u5b83\u7684\u300c\u7167\u7247\u300d<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4 + \u5e38\u7528\u683c\u5f0f\u7b26<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from datetime import datetime, date, timedelta\nnow = datetime.now()          # \u5f53\u524d\u65e5\u671f\u65f6\u95f4\ntoday = date.today()          # \u4eca\u5929\u7684\u65e5\u671f\nprint(now.strftime(\"%Y-%m-%d %H:%M:%S\"))\nprint(\"\u4e00\u5468\u540e:\", today + timedelta(days=7))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e38\u7528\u683c\u5f0f\u7b26\uff1a<code>%Y<\/code> \u56db\u4f4d\u5e74\u4efd\u3001<code>%m<\/code> \u4e24\u4f4d\u6708\u4efd\u3001<code>%d<\/code> \u4e24\u4f4d\u65e5\u671f\u3001<code>%H<\/code> \u5c0f\u65f6\u3001<code>%M<\/code> \u5206\u949f\u3001<code>%S<\/code> \u79d2\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>strptime \u5b57\u7b26\u4e32 \u2192 \u65f6\u95f4\uff08\u683c\u5f0f\u5fc5\u987b\u4e25\u683c\u5bf9\u5e94\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from datetime import datetime\ns = \"2026-08-03 09:30:00\"\ndt = datetime.strptime(s, \"%Y-%m-%d %H:%M:%S\")   # \u683c\u5f0f\u5fc5\u987b\u548c\u5b57\u7b26\u4e32\u4e25\u683c\u5bf9\u5e94\nprint(dt)\nprint(type(dt))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2026-08-03 09:30:00\n&lt;class 'datetime.datetime'&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>strptime(\"2026-8-4\", \"%Y-%m-%d\")<\/code> \u2192 \u95ee\u9898\uff1a<code>%m<\/code> \u8981\u6c42\u4e24\u4f4d\u6570\u5b57\uff0c\u62a5 <code>ValueError<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5b57\u7b26\u4e32\u4fdd\u6301\u4e24\u4f4d\uff08<code>2026-08-04<\/code>\uff09\uff0c\u6216\u6539\u7528\u6b63\u5219\u89e3\u6790\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u81ea\u5df1\u5199 <code>if month == 12: year += 1...<\/code> \u5904\u7406\u8de8\u5e74 \u2192 \u95ee\u9898\uff1a\u8fb9\u754c\u60c5\u51b5\u5bb9\u6613\u6f0f\uff0c\u95f0\u5e74\u7b97\u9519 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>timedelta<\/code>\uff0c\u5185\u90e8\u5904\u7406\u6240\u6709\u65e5\u5386\u89c4\u5219\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u65f6\u95f4\u662f\u5bf9\u8c61\uff0c\u5b57\u7b26\u4e32\u662f\u7167\u7247\uff1b\u52a0\u51cf\u7528 timedelta\uff0c\u8f6c\u6362\u9760 f \u548c p\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 collections \u2014\u2014 \u66f4\u806a\u660e\u7684\u300c\u5bb9\u5668\u4e09\u5144\u5f1f\u300d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>collections \u6a21\u5757<\/strong>\u63d0\u4f9b\u6bd4\u5185\u7f6e <code>list<\/code> \/ <code>dict<\/code> \/ <code>tuple<\/code> \u66f4\u9ad8\u7ea7\u7684\u5bb9\u5668\u3002\u4eca\u5929\u5b66\u4e09\u4e2a\u9ad8\u9891\u7684\uff1a<code>Counter<\/code>\uff08\u8ba1\u6570\u5668\uff09\u3001<code>defaultdict<\/code>\uff08\u5e26\u9ed8\u8ba4\u503c\u7684\u5b57\u5178\uff09\u3001<code>namedtuple<\/code>\uff08\u6709\u540d\u5b57\u7684\u5143\u7ec4\uff09\u2014\u2014<strong>\u5b83\u4eec\u8ba9\u4f60\u5c11\u5199\u4e00\u534a\u7684 if \u548c\u5faa\u73af<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Counter<\/code> \u672c\u8d28\u662f <code>dict<\/code> \u7684\u5b50\u7c7b\u2014\u2014\u952e\u662f\u5143\u7d20\u3001\u503c\u662f\u6b21\u6570\uff1b\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e<strong>\u8fd4\u56de 0 \u800c\u4e0d\u662f\u62a5 KeyError<\/strong>\uff08\u5185\u90e8 <code>__missing__<\/code> \u515c\u5e95\uff09\uff0c<code>most_common(n)<\/code> \u6392\u5e8f\u540e\u53d6\u524d n \u4e2a\u3002<code>defaultdict<\/code> \u4e5f\u662f <code>dict<\/code> \u5b50\u7c7b\uff0c\u6784\u9020\u65f6\u4f20\u300c\u9ed8\u8ba4\u503c\u5de5\u5382\u300d\uff08<code>list<\/code> \/ <code>int<\/code>\uff09\uff0c\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u81ea\u52a8\u521b\u5efa\u9ed8\u8ba4\u503c\u5e76<strong>\u5199\u56de\u5b57\u5178<\/strong>\u3002<code>namedtuple<\/code> \u662f <code>tuple<\/code> \u7684\u5b50\u7c7b\uff1a<strong>\u4e0d\u53ef\u53d8\u3001\u80fd\u4e0b\u6807\u3001\u80fd\u62c6\u5305<\/strong>\uff0c\u8fd8\u591a\u4e86\u5c5e\u6027\u540d\u8bbf\u95ee\uff08<code>p.x<\/code>\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Counter<\/code> \u50cf<strong>\u70b9\u9910\u8bb0\u5f55\u672c<\/strong>\uff1a\u6bcf\u70b9\u4e00\u6b21\u5212\u4e00\u7b14\uff0c\u6700\u540e\u544a\u8bc9\u4f60\u6bcf\u79cd\u83dc\u70b9\u4e86\u51e0\u4e2a\uff1b<code>defaultdict<\/code> \u50cf<strong>\u81ea\u52a8\u8865\u8d27\u7684\u50a8\u7269\u67dc<\/strong>\uff1a\u6ca1\u6709\u7684\u683c\u5b50\u6253\u5f00\u65f6\u81ea\u52a8\u653e\u4e00\u4e2a\u7a7a\u76d2\uff1b<code>namedtuple<\/code> \u50cf<strong>\u586b\u5199\u5b8c\u6574\u7684\u5feb\u9012\u5355<\/strong>\uff1a\u6bcf\u4e2a\u680f\u4f4d\u6709\u540d\u5b57\uff0c\u800c\u4e0d\u662f\u4e00\u4e32\u4e0d\u77e5\u9053\u662f\u4ec0\u4e48\u7684\u7f16\u53f7\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>Counter\u2014\u2014\u7edf\u8ba1\u8bcd\u9891\uff0c\u7f3a\u5931\u952e\u4e0d\u62a5\u9519<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from collections import Counter\nwords = &#91;\"\u82f9\u679c\", \"\u9999\u8549\", \"\u82f9\u679c\", \"\u68a8\", \"\u82f9\u679c\", \"\u9999\u8549\"]\ncounts = Counter(words)\nprint(counts)\nprint(\"\u82f9\u679c:\", counts&#91;\"\u82f9\u679c\"])\nprint(\"\u6700\u591a\u7684\u4e24\u4e2a:\", counts.most_common(2))\nprint(\"\u6ca1\u51fa\u73b0\u8fc7\u7684\u8bcd:\", counts&#91;\"\u897f\u74dc\"])   # \u7f3a\u5931\u952e\u8fd4\u56de 0\uff0c\u4e0d\u62a5\u9519\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Counter({'\u82f9\u679c': 3, '\u9999\u8549': 2, '\u68a8': 1})\n\u82f9\u679c: 3\n\u6700\u591a\u7684\u4e24\u4e2a: &#91;('\u82f9\u679c', 3), ('\u9999\u8549', 2)]\n\u6ca1\u51fa\u73b0\u8fc7\u7684\u8bcd: 0\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Counter(words)<\/code> \u4e00\u884c\u5b8c\u6210\u5168\u90e8\u8ba1\u6570\uff1b<strong>\u5173\u952e\uff1a\u67e5\u6ca1\u51fa\u73b0\u8fc7\u7684\u8bcd\u8fd4\u56de 0 \u800c\u4e0d\u662f KeyError<\/strong>\u2014\u2014\u624b\u5199 dict \u8ba1\u6570\u5fc5\u987b\u5148 <code>if key in d<\/code> \u5224\u65ad\uff0c\u9ebb\u70e6\u5f97\u591a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>defaultdict\u2014\u2014\u5206\u7ec4\u6536\u96c6\uff0c\u952e\u4e0d\u5b58\u5728\u81ea\u52a8\u8865\u9ed8\u8ba4\u503c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from collections import defaultdict\nscores = &#91;(\"\u8bed\u6587\", 90), (\"\u6570\u5b66\", 95), (\"\u8bed\u6587\", 88), (\"\u82f1\u8bed\", 92)]\nd = defaultdict(list)                 # \u9ed8\u8ba4\u503c\u5de5\u5382\uff1alist\nfor subject, score in scores:\n    d&#91;subject].append(score)\nprint(dict(d))\nprint(d&#91;\"\u7269\u7406\"])                      # \u4e0d\u5b58\u5728\u7684\u952e\u5f97\u5230\u7a7a\u5217\u8868 &#91;]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'\u8bed\u6587': &#91;90, 88], '\u6570\u5b66': &#91;95], '\u82f1\u8bed': &#91;92]}\n&#91;]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>defaultdict(list)<\/code> \u58f0\u660e\u300c\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u81ea\u52a8\u653e\u4e00\u4e2a\u7a7a\u5217\u8868\u300d\uff1b\u5faa\u73af\u91cc\u7b2c\u4e00\u6b21\u9047\u5230\u300c\u8bed\u6587\u300d\u81ea\u52a8\u5efa\u7a7a\u5217\u8868\u518d\u8ffd\u52a0\uff0c\u7b2c\u4e8c\u6b21\u76f4\u63a5\u8ffd\u52a0\u2014\u2014<strong>\u4e0d\u7528\u518d\u5199\u300c\u952e\u4e0d\u5b58\u5728\u5c31\u521d\u59cb\u5316\u300d\u7684\u5206\u652f<\/strong>\u3002\u9ed8\u8ba4\u503c\u662f <code>int<\/code> \u65f6\uff0c<code>d[key] += 1<\/code> \u53ef\u76f4\u63a5\u505a\u8ba1\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>namedtuple\u2014\u2014\u6709\u540d\u5b57\u7684\u5143\u7ec4<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from collections import namedtuple\nPoint = namedtuple(\"Point\", &#91;\"x\", \"y\"])   # \u5b9a\u4e49\u4e00\u79cd\u65b0\"\u7c7b\u578b\"\np = Point(3, 4)\nprint(p.x, p.y)        # \u5c5e\u6027\u8bbf\u95ee\nprint(p&#91;0], p&#91;1])      # \u4e0b\u6807\u8bbf\u95ee\nx, y = p               # \u8fd8\u80fd\u62c6\u5305\nprint(x, y)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3 4\n3 4\n3 4\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u624b\u5199\u8ba1\u6570 <code>if w in d: d[w] += 1 else: d[w] = 1<\/code> \u2192 \u95ee\u9898\uff1a\u5570\u55e6\u4e14\u5bb9\u6613\u6f0f\u5206\u652f \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>Counter(words)<\/code> \u6216 <code>defaultdict(int)<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u7ed9 namedtuple \u8d4b\u503c <code>p.x = 5<\/code> \u2192 \u95ee\u9898\uff1a\u5143\u7ec4\u4e0d\u53ef\u53d8\uff0c\u629b <code>AttributeError<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u91cd\u65b0\u6784\u9020 <code>Point(5, 4)<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u6570\u6570 Counter\uff0c\u5206\u7ec4 defaultdict\uff0c\u8f7b\u91cf\u7ed3\u6784 namedtuple\uff1b\u9ed8\u8ba4\u503c\u5de5\u5382\u522b\u5fd8\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 itertools \u2014\u2014 \u8fed\u4ee3\u5668\u7684\u300c\u7ec4\u5408\u79ef\u6728\u300d<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>itertools \u6a21\u5757<\/strong>\u662f\u4e00\u7ec4\u300c\u5904\u7406\u8fed\u4ee3\u5668\u7684\u5de5\u5177\u51fd\u6570\u300d\uff1a\u5408\u5e76\uff08<code>chain<\/code>\uff09\u3001\u5faa\u73af\uff08<code>cycle<\/code>\uff09\u3001\u65e0\u9650\u6570\u6570\uff08<code>count<\/code>\uff09\u3001\u7ec4\u5408\u914d\u5bf9\uff08<code>product<\/code>\uff09\u3001\u9650\u91cf\u5207\u7247\uff08<code>islice<\/code>\uff09\u3002\u5b83\u8ba9\u4f60<strong>\u7528\u4e00\u884c\u66ff\u4ee3\u624b\u5199\u5faa\u73af<\/strong>\uff0c\u800c\u4e14<strong>\u60f0\u6027\uff08lazy\uff09<\/strong>\u2014\u2014\u5143\u7d20\u300c\u7528\u5230\u624d\u7b97\u300d\uff0c\u4e0d\u4f1a\u4e00\u6b21\u6027\u5168\u90e8\u9020\u51fa\u6765\u5360\u5185\u5b58\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">itertools \u8fd4\u56de\u7684\u90fd\u662f<strong>\u8fed\u4ee3\u5668\u5bf9\u8c61<\/strong>\uff08\u4e0d\u662f\u5217\u8868\uff09\uff0c\u9075\u5faa Day 18 \u5b66\u7684\u60f0\u6027\u6c42\u503c\uff1a<code>itertools.count(1)<\/code> \u7406\u8bba\u4e0a\u6570\u5230\u65e0\u7a77\u5927\uff0c\u4f46<strong>\u4e0d\u4f1a\u771f\u7684\u751f\u6210\u65e0\u7a77\u4e2a\u6570\u5b57<\/strong>\uff0c\u53ea\u6709\u6bcf\u6b21 <code>next()<\/code> \u6216\u5faa\u73af\u53d6\u4e00\u4e2a\u624d\u7b97\u4e00\u4e2a\u3002\u6240\u4ee5\u76f4\u63a5 <code>list(itertools.count(1))<\/code> \u4f1a\u5361\u6b7b\uff0c\u5fc5\u987b\u7528 <code>islice<\/code>\uff08\u9650\u91cf\u53d6\u524d\u51e0\u4e2a\uff09\u6216 <code>for<\/code> \u5faa\u73af\u914d\u5408 <code>break<\/code>\u3002<code>chain<\/code> \u628a\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u300c\u9996\u5c3e\u76f8\u63a5\u300d\uff1b<code>product<\/code> \u8ba1\u7b97\u7b1b\u5361\u5c14\u79ef\uff08\u6240\u6709\u914d\u5bf9\uff09\uff0c\u7ed3\u679c\u6570 = \u5404\u5e8f\u5217\u957f\u5ea6\u76f8\u4e58\uff0c\u6ce8\u610f\u522b\u8ba9\u91cf\u7ea7\u7206\u70b8\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>count<\/code> \u50cf<strong>\u65e0\u9650\u957f\u7684\u53d6\u53f7\u673a<\/strong>\uff08\u8981\u51e0\u4e2a\u6309\u51e0\u4e2a\uff09\uff1b<code>cycle<\/code> \u50cf<strong>\u5faa\u73af\u64ad\u653e\u7684\u6b4c\u5355<\/strong>\uff1b<code>chain<\/code> \u50cf<strong>\u628a\u51e0\u6839\u7ef3\u5b50\u63a5\u6210\u4e00\u6839<\/strong>\uff1b<code>product<\/code> \u50cf<strong>\u70b9\u5957\u9910<\/strong>\u2014\u2014\u4e3b\u98df \u00d7 \u996e\u6599\u7684\u6240\u6709\u642d\u914d\uff1b<code>islice<\/code> \u50cf<strong>\u5207\u86cb\u7cd5<\/strong>\uff1a\u53ea\u5207\u7b2c 2 \u5230\u7b2c 4 \u5757\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>count + islice\u2014\u2014\u4ece\u65e0\u9650\u5e8f\u5217\u91cc\u300c\u9650\u91cf\u53d6\u300d\uff1bcycle\u2014\u2014\u5faa\u73af\u64ad\u653e<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import itertools\nfor n in itertools.islice(itertools.count(1), 3):\n    print(\"count:\", n)\nnames = &#91;\"\u5f20\u4e09\", \"\u674e\u56db\"]\nfor name in itertools.islice(itertools.cycle(names), 4):\n    print(\"cycle:\", name)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>count: 1\ncount: 2\ncount: 3\ncycle: \u5f20\u4e09\ncycle: \u674e\u56db\ncycle: \u5f20\u4e09\ncycle: \u674e\u56db\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>itertools.count(1)<\/code> \u4ece 1 \u5f00\u59cb\u65e0\u9650\u6570\u6570\uff0c<code>islice(it, 3)<\/code> \u53ea\u53d6\u524d 3 \u4e2a\uff1b<code>cycle(names)<\/code> \u628a\u5217\u8868\u53d8\u6210\u300c\u5faa\u73af\u64ad\u653e\u300d\uff0c\u914d\u5408 <code>islice<\/code> \u53d6\u524d 4 \u4e2a\u3002**\u7edd\u4e0d\u8981\u76f4\u63a5 <code>list(itertools.count(1))<\/code>**\u2014\u2014\u5b83\u6ca1\u6709\u5c3d\u5934\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>chain \u5408\u5e76 + product \u7b1b\u5361\u5c14\u79ef<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import itertools\nprint(list(itertools.chain(&#91;1, 2], &#91;\"a\", \"b\"])))   # \u5408\u5e76\u6210\u4e00\u4e2a\u5e8f\u5217\nfor combo in itertools.product(&#91;1, 2], &#91;\"a\", \"b\"]):\n    print(combo)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 2, 'a', 'b']\n(1, 'a')\n(1, 'b')\n(2, 'a')\n(2, 'b')\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>chain<\/code> \u628a\u4e24\u4e2a\u5217\u8868\u9996\u5c3e\u76f8\u63a5\uff08\u5408\u5e76\u5217\u8868\u4e0d\u7528\u5199\u4e24\u5c42\u5faa\u73af\uff09\uff1b<code>product<\/code> \u751f\u6210\u6240\u6709 <code>(\u6570\u5b57, \u5b57\u6bcd)<\/code> \u914d\u5bf9\u2014\u2014<strong>\u300c\u904d\u5386\u6240\u6709\u7ec4\u5408\u300d\u5c31\u9760\u5b83<\/strong>\uff08\u6bd4\u5982\u7a77\u4e3e 4 \u4f4d\u6570\u5b57\u5bc6\u7801\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>list(itertools.count(1))<\/code> \u2192 \u95ee\u9898\uff1a\u65e0\u9650\u5e8f\u5217\u8f6c\u5217\u8868\uff0c\u7a0b\u5e8f\u6c38\u8fdc\u8dd1\u4e0d\u5b8c \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>islice<\/code> \u9650\u91cf\u53d6\uff0c\u6216 <code>for<\/code> + <code>break<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u540c\u4e00\u4e2a\u8fed\u4ee3\u5668 for \u4e24\u6b21 \u2192 \u95ee\u9898\uff1a\u8fed\u4ee3\u5668\u662f\u300c\u4e00\u6b21\u6027\u300d\u7684\uff0c\u7b2c\u4e8c\u6b21\u4e3a\u7a7a \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u9700\u8981\u590d\u7528\u5c31\u8f6c\u6210 <code>list()<\/code> \u5b58\u4e0b\u6765\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1acount \u65e0\u9650\u6570\uff0ccycle \u5faa\u73af\u64ad\uff0cchain \u63a5\u7ef3\u5b50\uff0cproduct \u5168\u914d\u5bf9\uff1b\u65e0\u9650\u5e8f\u5217\u522b\u8d2a\u5fc3\uff0cislice \u9650\u91cf\u53d6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 re \u2014\u2014 \u7528\u300c\u6a21\u5f0f\u300d\u800c\u4e0d\u662f\u300c\u4ee3\u7801\u300d\u6765\u627e\u6587\u672c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff08regular expression\uff0c\u7b80\u79f0 regex\uff09\u662f\u4e00\u5957\u300c\u7528\u7b26\u53f7\u63cf\u8ff0\u6587\u672c\u6a21\u5f0f\u300d\u7684\u8ff7\u4f60\u8bed\u8a00\u3002<code>re<\/code> \u6a21\u5757\u8ba9\u4f60\u7528<strong>\u4e00\u4e2a\u6a21\u5f0f\u5b57\u7b26\u4e32<\/strong>\u56de\u7b54\u300c\u8fd9\u6bb5\u6587\u672c\u91cc\u6709\u6ca1\u6709\u7535\u8bdd\u53f7\u7801 \/ \u6240\u6709\u65e5\u671f\u5728\u54ea \/ \u628a\u6570\u5b57\u6362\u6210 #\u300d\u8fd9\u7c7b\u95ee\u9898\u3002\u5b57\u7b26\u4e32\u67e5\u627e\uff08<code>in<\/code>\u3001<code>find<\/code>\uff09\u53ea\u80fd\u5339\u914d\u56fa\u5b9a\u5185\u5bb9\uff0c\u6b63\u5219\u80fd\u5339\u914d<strong>\u4e00\u7c7b<\/strong>\u5185\u5bb9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>re<\/code> \u6a21\u5757\u628a\u6a21\u5f0f\u5b57\u7b26\u4e32<strong>\u7f16\u8bd1\u6210\u72b6\u6001\u673a<\/strong>\u518d\u626b\u63cf\u6587\u672c\uff1a<code>findall<\/code> \u627e\u51fa<strong>\u6240\u6709<\/strong>\u5339\u914d\u5b50\u4e32\u3001<code>search<\/code> \u627e<strong>\u7b2c\u4e00\u4e2a<\/strong>\u5339\u914d\uff08\u8fd4\u56de match \u5bf9\u8c61\uff09\u3001<code>sub<\/code> \u66ff\u6362\u6240\u6709\u5339\u914d\u3001<code>split<\/code> \u6309\u5339\u914d\u4f4d\u7f6e\u5207\u5206\u3002\u5e38\u7528\u7b26\u53f7\uff1a<code>\\d<\/code> \u6570\u5b57\u3001<code>\\w<\/code> \u5b57\u6bcd\u6570\u5b57\u4e0b\u5212\u7ebf\u3001<code>\\s<\/code> \u7a7a\u767d\uff1b<code>.<\/code> \u4efb\u610f\u5b57\u7b26\u3001<code>*<\/code> 0 \u6b21\u6216\u591a\u6b21\u3001<code>+<\/code> \u81f3\u5c11 1 \u6b21\u3001<code>?<\/code> 0 \u6b21\u6216 1 \u6b21\uff1b<code>{n,m}<\/code> \u51fa\u73b0 n \u5230 m \u6b21\uff1b<code>[...]<\/code> \u5b57\u7b26\u96c6\u5408\uff1b<code>( )<\/code> \u5206\u7ec4\u6355\u83b7\uff1b<code>^<\/code> \u5f00\u5934\u3001<code>$<\/code> \u7ed3\u5c3e\u3002**<code>r<\/code> \u524d\u7f00\uff08\u539f\u59cb\u5b57\u7b26\u4e32\uff09\u544a\u8bc9 Python\u300c\u522b\u8f6c\u4e49\u53cd\u659c\u6760\u300d**\uff0c<code>\\d<\/code> \u624d\u80fd\u539f\u6837\u4ea4\u7ed9\u6b63\u5219\u5f15\u64ce\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b63\u5219\u50cf<strong>\u5feb\u9012\u5206\u62e3\u89c4\u5219<\/strong>\uff1a\u300c\u628a\u6240\u6709\u7535\u8bdd\u53f7\uff083-4-4 \u4f4d\u6570\u5b57\u5e26\u6a2a\u6760\uff09\u7684\u5305\u88f9\u6311\u51fa\u6765\u300d\u2014\u2014\u4f60\u63cf\u8ff0\u89c4\u5219\uff0c\u673a\u5668\u6267\u884c\u3002<code>find<\/code> \u50cf\u300c\u627e\u5199\u7740\u4e00\u6a21\u4e00\u6837\u5b57\u6837\u7684\u5305\u88f9\u300d\uff0c\u6b63\u5219\u50cf\u300c\u627e\u7b26\u5408\u89c4\u5219\u7684\u5305\u88f9\u300d\uff0c<strong>\u4e00\u4e2a\u8ba4\u6b7b\u7406\uff0c\u4e00\u4e2a\u8ba4\u6a21\u5f0f<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>findall \u627e\u51fa\u6240\u6709\u5339\u914d + search \u5206\u7ec4\u62c6\u89e3<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\ntext = \"\u6211\u7684\u7535\u8bdd\u662f 138-1234-5678\uff0c\u5907\u7528 010-12345678\"\nphones = re.findall(r\"\\d{3}-\\d{4}-\\d{4}\", text)   # 3\u4f4d-4\u4f4d-4\u4f4d\nprint(phones)\ntext2 = \"\u4eca\u5929\u662f 2026-08-03\uff0c\u5929\u6c14\u4e0d\u9519\"\nm = re.search(r\"(\\d{4})-(\\d{2})-(\\d{2})\", text2)  # search \u627e\u7b2c\u4e00\u4e2a\nif m:\n    print(\"\u6574\u4e2a\u5339\u914d:\", m.group(0))\n    print(\"\u5e74:\", m.group(1), \"\u6708:\", m.group(2), \"\u65e5:\", m.group(3))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'138-1234-5678']\n\u6574\u4e2a\u5339\u914d: 2026-08-03\n\u5e74: 2026 \u6708: 08 \u65e5: 03\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>r\"\\d{3}-\\d{4}-\\d{4}\"<\/code> \u8868\u793a\u300c3 \u4e2a\u6570\u5b57-4 \u4e2a\u6570\u5b57-4 \u4e2a\u6570\u5b57\u300d\uff0c<code>findall<\/code> \u626b\u51fa\u6240\u6709\u5339\u914d\uff08<code>010-12345678<\/code> \u662f 3+8 \u4f4d\u4e0d\u7b26\u5408\u7ed3\u6784\u6240\u4ee5\u6ca1\u5339\u914d\u4e0a\uff09\uff1b<code>( )<\/code> \u628a\u6a21\u5f0f\u5207\u6210 3 \u7ec4\uff0c<code>search<\/code> \u627e\u5230\u7b2c\u4e00\u4e2a\u5339\u914d\u540e <code>m.group(0)<\/code> \u662f\u6574\u4e2a\u5339\u914d\uff0c<code>group(1)<\/code>~`group(3)` \u662f\u5404\u7ec4\u5185\u5bb9\u2014\u2014<strong>\u63d0\u53d6\u5e74\/\u6708\/\u65e5\uff0c\u62c6\u7ec4\u5373\u53ef<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>split \u6309\u4e00\u7c7b\u5b57\u7b26\u5207\u5206 + sub \u66ff\u6362<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import re\ntext = \"apple,banana;cherry orange\"\nprint(re.split(r\"&#91;,; ]+\", text))            # \u6309\u9017\u53f7\/\u5206\u53f7\/\u7a7a\u683c\u5207\u5206\nprint(re.sub(r\"\\d+\", \"#\", \"abc123def456\"))  # \u6240\u6709\u6570\u5b57\u66ff\u6362\u6210 #\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'apple', 'banana', 'cherry', 'orange']\nabc#def#\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>split(r\"[,; ]+\", text)<\/code> \u7528\u300c\u4e00\u4e2a\u6216\u591a\u4e2a\u9017\u53f7\/\u5206\u53f7\/\u7a7a\u683c\u300d\u505a\u5206\u9694\u7b26\u2014\u2014<strong>\u6bd4 <code>str.split(\",\")<\/code> \u4e00\u6b21\u53ea\u80fd\u6309\u4e00\u79cd\u5206\u9694\u7b26\u5f3a<\/strong>\uff1b<code>sub(r\"\\d+\", \"#\", ...)<\/code> \u628a\u8fde\u7eed\u6570\u5b57\u6574\u6bb5\u66ff\u6362\u6210 <code>#<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u6b63\u5219\u5b57\u7b26\u4e32\u4e0d\u5199 <code>r<\/code> \u524d\u7f00\uff1a<code>\"\\d{3}\"<\/code> \u2192 \u95ee\u9898\uff1a<code>\\d<\/code> \u88ab\u5f53\u6210 <code>d<\/code>\uff0c\u5339\u914d\u5168\u9519 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5199 <code>r\"\\d{3}\"<\/code>\uff0c\u539f\u6837\u4f20\u7ed9\u6b63\u5219\u5f15\u64ce\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>search<\/code> \u540e\u4e0d\u5224\u65ad\u76f4\u63a5 <code>m.group()<\/code> \u2192 \u95ee\u9898\uff1a\u6ca1\u5339\u914d\u65f6 <code>m<\/code> \u662f <code>None<\/code>\uff0c\u62a5 <code>AttributeError<\/code> \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5148 <code>if m:<\/code> \u5224\u65ad\u518d\u53d6\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\\d \u6570\u5b57 \\w \u5b57\uff0c\u52a0 r \u524d\u7f00\u522b\u5fd8\u8bb0\uff1bfindall \u627e\u5168\u90e8\uff0csearch \u53d6\u7b2c\u4e00\uff0csub \u6362 split \u5207\uff0c\u62ec\u53f7\u5206\u7ec4\u597d\u63d0\u53d6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5 \u52a8\u624b\u5b9e\u8df5\uff1a\u65e5\u671f\u4e0e\u6587\u672c\u5904\u7406\u5de5\u5177<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u505a\u4e00\u4e2a\u5de5\u5177\u811a\u672c\uff1a\u4ece\u4e00\u6bb5\u4f1a\u8bae\u8bb0\u5f55\u6587\u672c\u91cc\u63d0\u53d6\u6240\u6709\u65e5\u671f\uff08\u652f\u6301 <code>2026-08-03<\/code> \u548c <code>2026\/8\/3<\/code> \u4e24\u79cd\u5199\u6cd5\uff09\uff0c\u8f6c\u6210\u7edf\u4e00\u683c\u5f0f\uff0c\u7edf\u8ba1\u6bcf\u6708\u6709\u51e0\u573a\u4f1a\u8bae\uff0c\u5e76\u8ba1\u7b97\u6bcf\u573a\u8ddd\u79bb\u4eca\u5929\u8fd8\u6709\u51e0\u5929\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># text_tools.py \u2014\u2014 \u65e5\u671f\u4e0e\u6587\u672c\u5904\u7406\u5de5\u5177\nimport re\nfrom datetime import datetime\nfrom collections import Counter\n\ndef extract_dates(text):\n    \"\"\"\u63d0\u53d6\u4e24\u79cd\u683c\u5f0f\u7684\u65e5\u671f\uff1a2026-08-03 \u6216 2026\/8\/3\"\"\"\n    pattern = r\"(\\d{4})&#91;-\/](\\d{1,2})&#91;-\/](\\d{1,2})\"\n    results = &#91;]\n    for year, month, day in re.findall(pattern, text):\n        results.append(datetime(int(year), int(month), int(day)))\n    return results\n\ndef format_report(dates):\n    \"\"\"\u7edf\u8ba1\u6bcf\u6708\u4f1a\u8bae\u6570\u91cf\uff0c\u5e76\u8ba1\u7b97\u6bcf\u573a\u8ddd\u79bb\u4eca\u5929\u7684\u5929\u6570\"\"\"\n    month_counts = Counter(d.month for d in dates)\n    today = datetime.now().date()\n    lines = &#91;]\n    for d in dates:\n        delta = (d.date() - today).days\n        lines.append(f\"{d.strftime('%Y-%m-%d')}\uff08\u8ddd\u4eca {delta} \u5929\uff09\")\n    lines.append(\"\u6708\u4efd\u7edf\u8ba1: \" + \", \".join(f\"{m}\u6708\u00d7{n}\" for m, n in sorted(month_counts.items())))\n    return \"\\n\".join(lines)\n\ntext = \"\"\"\n\u4f1a\u8bae\u8bb0\u5f55\uff1a\n2026-08-03 \u9879\u76ee\u542f\u52a8\u4f1a\n2026\/8\/10 \u9700\u6c42\u8bc4\u5ba1\n2026-08-15 \u5f00\u53d1\u5b8c\u6210\n2026\/08\/20 \u4e0a\u7ebf\u53d1\u5e03\n\"\"\"\n\ndates = extract_dates(text)\nprint(format_report(dates))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff08\u4eca\u5929\u662f 2026-08-03 \u65f6\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2026-08-03\uff08\u8ddd\u4eca 0 \u5929\uff09\n2026-08-10\uff08\u8ddd\u4eca 7 \u5929\uff09\n2026-08-15\uff08\u8ddd\u4eca 12 \u5929\uff09\n2026-08-20\uff08\u8ddd\u4eca 17 \u5929\uff09\n\u6708\u4efd\u7edf\u8ba1: 8\u6708\u00d74\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b63\u5219\u5728 <code>-<\/code> \u548c <code>\/<\/code> \u4e4b\u95f4\u7528 <code>[-\/]<\/code> \u5b57\u7b26\u96c6\u5408\u517c\u5bb9\u4e86\u4e24\u79cd\u5199\u6cd5\uff0c<code>strftime<\/code> \u628a\u683c\u5f0f\u7edf\u4e00\u6210 <code>YYYY-MM-DD<\/code>\uff0c<code>Counter<\/code> \u7edf\u8ba1\u6708\u4efd\uff0c<code>timedelta<\/code> \u7684\u5dee\u503c\u7b97\u51fa\u300c\u8ddd\u4eca\u51e0\u5929\u300d\u2014\u2014\u4eca\u5929\u5b66\u7684\u56db\u4e2a\u6a21\u5757\u5728\u4e00\u5904\u914d\u5408\u4f7f\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5347\u7ea7\u6311\u6218<\/strong>\uff1a\u652f\u6301\u4e2d\u6587\u65e5\u671f <code>2026\u5e748\u67083\u65e5<\/code>\uff1b\u518d\u7ed9\u5de5\u5177\u52a0\u4e00\u4e2a\u51fd\u6570\uff0c\u7528 <code>re.sub<\/code> \u628a\u6587\u672c\u91cc\u7684\u65e5\u671f\u5168\u90e8\u66ff\u6362\u6210 <code>[\u65e5\u671f]<\/code> \u5360\u4f4d\u7b26\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4eca\u65e5\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>datetime<\/code> \u662f\u5bf9\u8c61\u4e0d\u662f\u5b57\u7b26\u4e32\uff1a\u6784\u9020\u3001\u53d6\u5c5e\u6027\u3001\u6bd4\u5927\u5c0f\u3001\u53ef\u6392\u5e8f\uff1b<code>timedelta<\/code> \u81ea\u52a8\u5904\u7406\u8de8\u6708\u8de8\u5e74\uff08<code>.days<\/code> \/ <code>.total_seconds()<\/code>\uff09\n<code>strftime<\/code> \u65f6\u95f4\u2192\u5b57\u7b26\u4e32\u3001<code>strptime<\/code> \u5b57\u7b26\u4e32\u2192\u65f6\u95f4\uff0c\u683c\u5f0f\u4e32\u4e25\u683c\u5bf9\u5e94\n<code>Counter<\/code> \u4e00\u952e\u8ba1\u6570\u3001\u7f3a\u5931\u952e\u8fd4\u56de 0\u3001<code>most_common(n)<\/code> \u53d6\u6392\u884c\n<code>defaultdict(list)<\/code> \u5206\u7ec4\u6536\u96c6\u3001<code>defaultdict(int)<\/code> \u8ba1\u6570\uff0c\u4e0d\u518d\u624b\u5199 if \u5224\u65ad\n<code>namedtuple<\/code> \u6709\u540d\u5b57\u7684\u5143\u7ec4\uff1a\u5c5e\u6027\u8bbf\u95ee + \u4e0b\u6807 + \u62c6\u5305\uff0c\u4e0d\u53ef\u53d8\nitertools \u5168\u662f\u60f0\u6027\u8fed\u4ee3\u5668\uff1a<code>count<\/code> \u65e0\u9650\u6570\u3001<code>cycle<\/code> \u5faa\u73af\u3001<code>chain<\/code> \u5408\u5e76\u3001<code>product<\/code> \u914d\u5bf9\uff0c<code>islice<\/code> \u9650\u91cf\u53d6\n\u6b63\u5219\u7b26\u53f7\uff1a<code>\\d<\/code>\u3001<code>+<\/code>\u3001<code>{n,m}<\/code>\u3001<code>[...]<\/code>\u3001<code>( )<\/code> \u5206\u7ec4\uff0c<code>r<\/code> \u524d\u7f00\u4fdd\u539f\u6837\n<code>re.findall<\/code> \u5168\u90e8 \/ <code>re.search<\/code> \u7b2c\u4e00\u4e2a\uff08\u5148\u5224\u65ad\u518d\u53d6\uff09\/ <code>re.sub<\/code> \u66ff\u6362 \/ <code>re.split<\/code> \u5207\u5206\n\u65e5\u671f\u4e0e\u6587\u672c\u5904\u7406\u5de5\u5177\u8fd0\u884c\u6b63\u5e38\uff1a\u63d0\u53d6\u4e24\u79cd\u65e5\u671f\u683c\u5f0f\u3001\u7edf\u8ba1\u6708\u4efd\u3001\u8ba1\u7b97\u5929\u6570\u5dee<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u62a5\u9519<\/th><th>\u539f\u56e0<\/th><th>\u4fee\u590d<\/th><\/tr><\/thead><tbody><tr><td><code>ValueError: time data '2026-8-3' does not match format '%Y-%m-%d'<\/code><\/td><td><code>strptime<\/code> \u7684\u683c\u5f0f\u548c\u5b57\u7b26\u4e32\u5bf9\u4e0d\u4e0a\uff0c<code>%m<\/code> \u8981\u6c42\u4e24\u4f4d\u6570\u5b57<\/td><td>\u8ba9\u5b57\u7b26\u4e32\u4fdd\u6301\u4e24\u4f4d\uff08<code>2026-08-03<\/code>\uff09\uff0c\u6216\u6539\u7528\u6b63\u5219\/\u624b\u52a8\u62c6\u5206\u89e3\u6790<\/td><\/tr><tr><td><code>KeyError: '\u82f1\u8bed'<\/code><\/td><td>\u666e\u901a <code>dict<\/code> \u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e<\/td><td>\u6362\u6210 <code>defaultdict(list)<\/code>\uff0c\u6216\u5148\u5224\u65ad <code>if key in d<\/code><\/td><\/tr><tr><td><code>TypeError: 'itertools.count' object is not subscriptable<\/code><\/td><td>\u5bf9\u60f0\u6027\u8fed\u4ee3\u5668\u7528\u4e86 <code>count[0]<\/code> \u8fd9\u79cd\u4e0b\u6807<\/td><td>\u7528 <code>itertools.islice(it, n)<\/code> \u9650\u91cf\u53d6\uff0c\u6216\u8f6c\u6210 <code>list()<\/code><\/td><\/tr><tr><td><code>re.error: missing ), unterminated subpattern<\/code><\/td><td>\u6b63\u5219\u91cc\u5706\u62ec\u53f7\u6ca1\u914d\u5bf9<\/td><td>\u68c0\u67e5 <code>( )<\/code> \u662f\u5426\u6210\u5bf9\uff0c\u6570\u91cf\u5bf9\u9f50<\/td><\/tr><tr><td><code>TypeError: expected string or bytes-like object<\/code><\/td><td><code>re.sub<\/code> \/ <code>re.findall<\/code> \u7b2c\u4e00\u4e2a\u53c2\u6570\u5199\u6210\u4e86\u5217\u8868<\/td><td>\u7b2c\u4e00\u53c2\u6570\u5fc5\u987b\u662f\u6b63\u5219\u5b57\u7b26\u4e32\uff08<code>r\"...\"<\/code>\uff09\uff0c\u4e0d\u8981\u4f20\u5217\u8868<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1 datetime \u2014\u2014 \u65f6\u95f4\u5bf9\u8c61\u662f\u600e\u4e48\u300c\u8fd0\u8f6c\u300d\u7684 \u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f datetime \u6a21\u5757\u662f Pytho [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[16,9],"tags":[17],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-python","category-code-dev","tag-python-day"],"_links":{"self":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":2,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/104\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}