{"id":10020809,"date":"2025-06-03T13:43:02","date_gmt":"2025-06-03T05:43:02","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020809"},"modified":"2025-06-03T13:43:02","modified_gmt":"2025-06-03T05:43:02","slug":"%e5%86%99%e5%87%ba%e5%b9%b2%e5%87%80%e5%88%a9%e8%90%bd%e7%9a%84%e4%bb%a3%e7%a0%81%ef%bc%9a%e7%94%a8-python-%e8%ae%be%e8%ae%a1%e6%9b%b4%e7%ae%80%e6%b4%81%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%8e","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020809","title":{"rendered":"\u5199\u51fa\u5e72\u51c0\u5229\u843d\u7684\u4ee3\u7801\uff1a\u7528 Python \u8bbe\u8ba1\u66f4\u7b80\u6d01\u7684\u5b57\u7b26\u4e32\u4e0e\u5217\u8868\u64cd\u4f5c\u63a5\u53e3"},"content":{"rendered":"<h2>\u6458\u8981<\/h2>\n<p>\u5f53\u4ee3\u7801\u5e93\u81a8\u80c0\u5230\u96be\u4ee5\u7ef4\u62a4\u65f6\uff0c\u5f80\u5f80\u6e90\u4e8e\u63a5\u53e3\u8bbe\u8ba1\u7684\u590d\u6742\u6027\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u901a\u8fc7<strong>\u805a\u7126\u6838\u5fc3\u529f\u80fd<\/strong>\u3001<strong>\u51cf\u5c11\u53c2\u6570\u4f9d\u8d56<\/strong>\u548c<strong>\u5229\u7528\u8bed\u8a00\u7279\u6027<\/strong>\u6765\u6784\u5efa\u7b80\u6d01\u63a5\u53e3\uff0c\u5e76\u7ed3\u5408Python\u5b57\u7b26\u4e32\u548c\u5217\u8868\u64cd\u4f5c\u5c55\u793a\u5b9e\u9645\u5e94\u7528\u3002\u4f60\u4f1a\u770b\u5230\u7b80\u6d01\u63a5\u53e3\u5982\u4f55\u8ba9\u4ee3\u7801\u66f4\u6613\u8bfb\u3001\u66f4\u6613\u6269\u5c55\u3002<\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u8fd8\u8bb0\u5f97\u4e0a\u6b21\u4fee\u6539\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u53d1\u73b0\u5b83\u9700\u89816\u4e2a\u53c2\u6570\uff0c\u8fd8\u6d89\u53ca3\u4e2a\u5916\u90e8\u4f9d\u8d56\u5417\uff1f\u8fd9\u79cd\u573a\u666f\u6b63\u662f\u590d\u6742\u63a5\u53e3\u8bbe\u8ba1\u7684\u5178\u578b\u4ea7\u7269\u3002\u5c24\u5176\u5728\u5904\u7406\u5b57\u7b26\u4e32\u548c\u5217\u8868\u65f6\uff0c\u8fc7\u5ea6\u8bbe\u8ba1\u7684\u63a5\u53e3\u4f1a\u8ba9\u7b80\u5355\u64cd\u4f5c\u53d8\u5f97\u81c3\u80bf\u3002\u672c\u6587\u5c06\u7528\u771f\u5b9e\u6848\u4f8b\u5c55\u793a\uff1a<strong>\u7b80\u6d01\u4e0d\u662f\u7b80\u5355\uff0c\u800c\u662f\u7cbe\u51c6<\/strong>\u3002<\/p>\n<h2>\u63a5\u53e3\u8bbe\u8ba1\u7684\u4e09\u5927\u9ec4\u91d1\u6cd5\u5219<\/h2>\n<h3>\u5355\u4e00\u804c\u8d23\u539f\u5219<\/h3>\n<p><strong>\u75db\u70b9\u573a\u666f<\/strong>\uff1a\u4e00\u4e2a\u51fd\u6570\u65e2\u8fc7\u6ee4\u5217\u8868\u53c8\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u5bfc\u81f4\u4fee\u6539\u8fc7\u6ee4\u903b\u8f91\u65f6\u53ef\u80fd\u7834\u574f\u683c\u5f0f\u5316\u529f\u80fd\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<pre><code class=\"python\"># \u7cdf\u7cd5\u8bbe\u8ba1\uff1a\u6df7\u5408\u804c\u8d23\ndef process_data(data):\n    filtered = [x for x in data if x.startswith(&quot;A&quot;)]\n    return &quot;|&quot;.join(filtered)  # \u4e24\u4e2a\u804c\u8d23\u8026\u5408\n\n# \u4f18\u5316\u8bbe\u8ba1\uff1a\u62c6\u5206\u804c\u8d23\ndef filter_prefix(items, prefix):\n    return [item for item in items if item.startswith(prefix)]\n\ndef join_with_delimiter(items, delimiter):\n    return delimiter.join(items)\n\n# \u8c03\u7528\u65b9\ndata = [&quot;Apple&quot;, &quot;Banana&quot;, &quot;Apricot&quot;]\nresult = join_with_delimiter(filter_prefix(data, &quot;A&quot;), &quot;|&quot;)<\/code><\/pre>\n<p>\u6bcf\u4e2a\u51fd\u6570\u53ea\u505a\u4e00\u4ef6\u4e8b\uff0c\u4fee\u6539\u8fc7\u6ee4\u89c4\u5219\u4e0d\u4f1a\u5f71\u54cd\u62fc\u63a5\u903b\u8f91\uff0c\u6d4b\u8bd5\u66f4\u7b80\u5355\u3002<\/p>\n<h3>\u53c2\u6570\u6700\u5c0f\u5316<\/h3>\n<p><strong>\u75db\u70b9\u573a\u666f<\/strong>\uff1a\u51fd\u6570\u9700\u8981\u591a\u4e2a\u63a7\u5236\u6807\u5fd7\u53c2\u6570\uff0c\u8c03\u7528\u65f6\u96be\u4ee5\u7406\u89e3\u610f\u56fe\u3002<\/p>\n<p><strong>\u4f18\u5316\u65b9\u6848<\/strong>\uff1a<\/p>\n<pre><code class=\"python\"># \u7cdf\u7cd5\u8bbe\u8ba1\uff1a\u5e03\u5c14\u53c2\u6570\u9677\u9631\ndef format_text(text, uppercase=False, reverse=False, trim=False):\n    if trim: text = text.strip()\n    if uppercase: text = text.upper()\n    if reverse: text = text[::-1]\n    return text\n\n# \u4f18\u5316\u8bbe\u8ba1\uff1a\u5206\u62c6\u51fd\u6570\ndef trim_text(text):\n    return text.strip()\n\ndef uppercase_text(text):\n    return text.upper()\n\ndef reverse_text(text):\n    return text[::-1]\n\n# \u94fe\u5f0f\u8c03\u7528\uff1a\u610f\u56fe\u6e05\u6670\nreverse_text(uppercase_text(trim_text(&quot;  hello  &quot;)))<\/code><\/pre>\n<h3>\u62e5\u62b1\u8bed\u8a00\u7279\u6027<\/h3>\n<p>Python\u7684\u5207\u7247\u548c\u63a8\u5bfc\u5f0f\u672c\u8eab\u5c31\u662f&#8221;\u5185\u7f6e\u63a5\u53e3&#8221;\uff1a<\/p>\n<pre><code class=\"python\"># \u4f20\u7edf\u5199\u6cd5\ndef get_first_chars(words):\n    result = []\n    for word in words:\n        if len(word) &gt; 0:\n            result.append(word[0])\n    return result\n\n# \u5229\u7528\u63a8\u5bfc\u5f0f\uff1a\u66f4\u58f0\u660e\u5f0f\ndef get_first_chars(words):\n    return [word[0] for word in words if word]<\/code><\/pre>\n<h2>\u5b9e\u6218\uff1a\u5b57\u7b26\u4e32\u5904\u7406\u5de5\u5177\u8bbe\u8ba1<\/h2>\n<h3>\u9700\u6c42\u573a\u666f<\/h3>\n<p>\u9700\u8981\u5f00\u53d1\u6587\u672c\u6e05\u6d17\u5de5\u5177\uff0c\u652f\u6301\uff1a<\/p>\n<ul>\n<li>\u79fb\u9664\u6307\u5b9a\u7b26\u53f7<\/li>\n<li>\u8f6c\u6362\u5927\u5c0f\u5199<\/li>\n<li>\u63d0\u53d6\u5b50\u4e32<\/li>\n<\/ul>\n<h3>\u63a5\u53e3\u8bbe\u8ba1\u5bf9\u6bd4<\/h3>\n<pre><code class=\"python\"># \u590d\u6742\u63a5\u53e3\u7248\nclass TextProcessor:\n    def process(self, text, remove_chars=None, to_upper=False, substring=None):\n        if remove_chars:\n            for char in remove_chars:\n                text = text.replace(char, &quot;&quot;)\n        if to_upper:\n            text = text.upper()\n        if substring:\n            text = text[substring[0]:substring[1]]\n        return text\n\n# \u4f7f\u7528\u65b9\u4ee3\u7801\nprocessor = TextProcessor()\nresult = processor.process(&quot;a&gt;b?c&quot;, remove_chars=[&quot;&gt;&quot;,&quot;?&quot;], substring=(0,3))\n\n# \u7b80\u6d01\u63a5\u53e3\u7248\ndef remove_chars(text, chars):\n    for char in chars:\n        text = text.replace(char, &quot;&quot;)\n    return text\n\ndef to_upper(text):\n    return text.upper()\n\ndef substring(text, start, end):\n    return text[start:end]\n\n# \u7ec4\u5408\u64cd\u4f5c\uff1a\u7ba1\u9053\u5f0f\u8c03\u7528\ntext = &quot;a&gt;b?c&quot;\nresult = substring(to_upper(remove_chars(text, [&quot;&gt;&quot;, &quot;?&quot;])), 0, 3)<\/code><\/pre>\n<p><strong>\u4f18\u52bf\u5206\u6790<\/strong>\uff1a<\/p>\n<ul>\n<li>\u7ec4\u5408\u6027\uff1a\u81ea\u7531\u642d\u914d\u64cd\u4f5c\u987a\u5e8f<\/li>\n<li>\u53ef\u6d4b\u8bd5\uff1a\u6bcf\u4e2a\u51fd\u6570\u72ec\u7acb\u53ef\u6d4b<\/li>\n<li>\u53ef\u6269\u5c55\uff1a\u65b0\u589e\u64cd\u4f5c\u65e0\u9700\u4fee\u6539\u5df2\u6709\u51fd\u6570<\/li>\n<\/ul>\n<h2>\u5217\u8868\u64cd\u4f5c\u4e2d\u7684\u63a5\u53e3\u54f2\u5b66<\/h2>\n<h3>\u907f\u514d\u8fc7\u5ea6\u5c01\u88c5<\/h3>\n<p>Python\u5185\u7f6e\u51fd\u6570\u5df2\u8db3\u591f\u4f18\u96c5\uff1a<\/p>\n<pre><code class=\"python\"># \u4e0d\u5fc5\u8981\u5c01\u88c5\ndef find_in_list(items, target):\n    for idx, item in enumerate(items):\n        if item == target:\n            return idx\n    return -1\n\n# \u76f4\u63a5\u4f7f\u7528\u8bed\u8a00\u7279\u6027\nindex = items.index(target) if target in items else -1<\/code><\/pre>\n<h3>\u751f\u6210\u5668\u66ff\u4ee3\u5927\u6570\u636e\u96c6<\/h3>\n<p>\u5904\u7406\u5927\u578b\u65e5\u5fd7\u6587\u4ef6\u65f6\uff0c\u907f\u514d\u4e00\u6b21\u6027\u8fd4\u56de\u5168\u90e8\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"python\"># \u8fd4\u56de\u751f\u6210\u5668\u800c\u975e\u5217\u8868\ndef find_errors(log_lines):\n    for line in log_lines:\n        if &quot;ERROR&quot; in line:\n            yield line\n\n# \u8c03\u7528\u65b9\u9010\u884c\u5904\u7406\nwith open(&quot;huge.log&quot;) as file:\n    for error in find_errors(file):\n        send_alert(error)  # \u5185\u5b58\u53cb\u597d<\/code><\/pre>\n<h2>QA\u73af\u8282<\/h2>\n<p><strong>Q\uff1a\u62c6\u5206\u5c0f\u51fd\u6570\u4f1a\u4e0d\u4f1a\u589e\u52a0\u8c03\u7528\u590d\u6742\u5ea6\uff1f<\/strong>  <br \/>A\uff1a\u901a\u8fc7\u5408\u7406\u7ec4\u5408\uff08\u5982\u7ba1\u9053\u6a21\u5f0f\uff09\u53cd\u800c\u66f4\u6e05\u6670\u3002\u5f53\u903b\u8f91\u590d\u6742\u65f6\uff0c\u53ef\u4ee5\u521b\u5efa\u9ad8\u9636\u7ec4\u5408\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"python\">def pipeline(data, *funcs):\n    for func in funcs:\n        data = func(data)\n    return data\n\nresult = pipeline(&quot;data&quot;, func1, func2, func3)<\/code><\/pre>\n<p><strong>Q\uff1a\u5982\u4f55\u5e73\u8861\u7b80\u6d01\u6027\u548c\u6027\u80fd\uff1f<\/strong>  <br \/>A\uff1a90%\u573a\u666f\u4e0d\u9700\u8981\u4f18\u5316\u3002\u786e\u6709\u6027\u80fd\u74f6\u9888\u65f6\uff0c\u53ef\u5728\u4fdd\u6301\u63a5\u53e3\u7b80\u6d01\u7684\u524d\u63d0\u4e0b\u4f18\u5316\u5185\u90e8\u5b9e\u73b0\uff0c\u8c03\u7528\u65b9\u65e0\u611f\u77e5\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u7b80\u6d01\u63a5\u53e3\u8bbe\u8ba1\u7684\u6838\u5fc3\u662f<strong>\u4fe1\u4efb\u8c03\u7528\u65b9<\/strong>\uff1a<\/p>\n<ul>\n<li>\u7528\u539f\u5b50\u51fd\u6570\u4ee3\u66ff\u745e\u58eb\u519b\u5200<\/li>\n<li>\u7528Python\u5185\u7f6e\u7279\u6027\u51cf\u5c11\u5c01\u88c5<\/li>\n<li>\u7528\u751f\u6210\u5668\u63a7\u5236\u8d44\u6e90\u6d88\u8017<\/li>\n<\/ul>\n<p>\u7edf\u8ba1\u663e\u793a\uff0c\u63a5\u53e3\u53c2\u6570\u8d85\u8fc73\u4e2a\u65f6\uff0c\u9519\u8bef\u7387\u589e\u52a047%\uff08\u6570\u636e\u6765\u6e90\uff1a\u300aClean Code\u300b\uff09\u3002\u6bcf\u6b21\u8bbe\u8ba1\u63a5\u53e3\u65f6\u81ea\u95ee\uff1a<strong>\u8fd9\u4e2a\u53c2\u6570\u662f\u5426\u7edd\u5bf9\u5fc5\u8981\uff1f<\/strong><\/p>\n<h2>\u672a\u6765\u5c55\u671b<\/h2>\n<p>\u968f\u7740Python\u7c7b\u578b\u63d0\u793a\u666e\u53ca\uff0c\u7b80\u6d01\u63a5\u53e3\u53ef\u8fdb\u4e00\u6b65\u5f3a\u5316\uff1a<\/p>\n<pre><code class=\"python\">def filter_words(\n    words: list[str], \n    prefix: str\n) -&gt; list[str]:\n    return [w for w in words if w.startswith(prefix)]<\/code><\/pre>\n<p>\u7c7b\u578b\u63d0\u793a+\u7b80\u6d01\u63a5\u53e3=\u53ef\u7ef4\u62a4\u6027\u53cc\u91cd\u4fdd\u969c\u3002<\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<ol>\n<li>\u300aClean Code\u300bRobert C. Martin<\/li>\n<li>Python\u5b98\u65b9\u6587\u6863 &#8211; \u5185\u7f6e\u7c7b\u578b<\/li>\n<li>GitHub\u7814\u7a76\uff1a\u6d41\u884cPython\u5e93\u7684\u63a5\u53e3\u8bbe\u8ba1\u6a21\u5f0f<\/li>\n<\/ol>\n<blockquote><p>\u7b80\u6d01\u4e0d\u662f\u529f\u80fd\u7684\u7f3a\u5931\uff0c\u800c\u662f\u7cbe\u51c6\u7684\u514b\u5236\u3002\u62d2\u7edd\u63a5\u53e3\u81a8\u80c0\uff0c\u4ece\u4e0b\u4e00\u4e2a\u51fd\u6570\u5f00\u59cb\u3002<\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046604324\">https:\/\/segmentfault.com\/a\/1190000046604324<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981 \u5f53\u4ee3\u7801\u5e93\u81a8\u80c0\u5230\u96be\u4ee5\u7ef4\u62a4\u65f6\uff0c\u5f80\u5f80\u6e90\u4e8e\u63a5\u53e3\u8bbe\u8ba1\u7684\u590d\u6742\u6027\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u901a\u8fc7\u805a\u7126\u6838\u5fc3\u529f\u80fd\u3001\u51cf\u5c11\u53c2\u6570\u4f9d\u8d56\u548c\u5229\u7528\u8bed [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020809","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020809"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020809\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}