{"id":7,"date":"2026-11-08T13:15:21","date_gmt":"2026-11-08T13:15:21","guid":{"rendered":"http:\/\/blog.antpython.net\/?p=7"},"modified":"2026-11-08T13:15:21","modified_gmt":"2026-11-08T13:15:21","slug":"%e5%bd%bb%e5%ba%95%e6%90%9e%e6%87%82-python-%e5%88%97%e8%a1%a8%e6%8e%a8%e5%af%bc%e5%bc%8f%e4%b8%8e%e7%94%9f%e6%88%90%e5%99%a8%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"http:\/\/blog.antpython.net\/?p=7","title":{"rendered":"\u5f7b\u5e95\u641e\u61c2 Python \u5217\u8868\u63a8\u5bfc\u5f0f\u4e0e\u751f\u6210\u5668\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p>\u5217\u8868\u63a8\u5bfc\u5f0f\u5199\u8d77\u6765\u77ed\u3001\u8bfb\u8d77\u6765\u5feb\uff0c\u662f Python \u91cc\u6700\u53d7\u6b22\u8fce\u7684\u8bed\u6cd5\u7cd6\u4e4b\u4e00\u3002\u4f46\u5b83\u4e5f\u5bb9\u6613\u88ab\u6ee5\u7528\u3002\u672c\u6587\u628a<strong>\u63a8\u5bfc\u5f0f\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u3001\u4f55\u65f6\u8be5\u7528 for<\/strong>\u4e00\u6b21\u8bb2\u6e05\u695a\u3002<\/p>\n<h2>\u57fa\u7840\u5199\u6cd5<\/h2>\n<pre><code>nums = [1, 2, 3, 4, 5]\nsquares = [n * n for n in nums]\nevens = [n for n in nums if n % 2 == 0]\nlabels = [\"\u5076\" if n % 2 == 0 else \"\u5947\" for n in nums]\n<\/code><\/pre>\n<h2>\u5d4c\u5957\u63a8\u5bfc\u5f0f\uff1a\u53ef\u8bfb\u6027\u4f18\u5148<\/h2>\n<pre><code>matrix = [[1, 2], [3, 4], [5, 6]]\nflat = [x for row in matrix for x in row]\n# \u7b49\u4ef7\u4e8e\uff1a\nflat2 = []\nfor row in matrix:\n    for x in row:\n        flat2.append(x)\n<\/code><\/pre>\n<p>\u8d85\u8fc7\u4e24\u5c42\u5d4c\u5957\uff0c\u5efa\u8bae\u6539\u56de\u666e\u901a <code>for<\/code>\uff0c\u56e2\u961f\u534f\u4f5c\u65f6\u66f4\u53cb\u597d\u3002<\/p>\n<h2>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\uff1a\u7701\u5185\u5b58<\/h2>\n<pre><code># \u5217\u8868\uff1a\u7acb\u523b\u5360\u6ee1\u5185\u5b58\ntotal1 = sum([i * i for i in range(10_000_000)])\n\n# \u751f\u6210\u5668\uff1a\u8fb9\u7b97\u8fb9\u4e22\uff0c\u5185\u5b58\u53cb\u597d\ntotal2 = sum(i * i for i in range(10_000_000))\n<\/code><\/pre>\n<p>\u5dee\u522b\u53ea\u5728\u62ec\u53f7\uff1a<code>[]<\/code> \u662f\u5217\u8868\uff0c<code>()<\/code> \u662f\u751f\u6210\u5668\u3002\u4f20\u7ed9 <code>sum<\/code>\u3001<code>any<\/code>\u3001<code>max<\/code> \u8fd9\u7c7b\u300c\u5403\u4e00\u904d\u5c31\u7ed3\u675f\u300d\u7684\u51fd\u6570\u65f6\uff0c\u4f18\u5148\u751f\u6210\u5668\u3002<\/p>\n<h2>\u5b57\u5178 \/ \u96c6\u5408\u63a8\u5bfc<\/h2>\n<pre><code>words = [\"python\", \"pandas\", \"agent\"]\nlength_map = {w: len(w) for w in words}\nunique_lens = {len(w) for w in words}\n<\/code><\/pre>\n<h2>\u5b9e\u6218\uff1a\u6e05\u6d17\u4e00\u884c\u810f\u6570\u636e<\/h2>\n<pre><code>raw = \"  Alice,  bob ,CHARLIE,, \"\nnames = [x.strip().title() for x in raw.split(\",\") if x.strip()]\nprint(names)  # ['Alice', 'Bob', 'Charlie']\n<\/code><\/pre>\n<h2>\u4ec0\u4e48\u65f6\u5019\u522b\u7528\u63a8\u5bfc\u5f0f<\/h2>\n<ul>\n<li>\u903b\u8f91\u91cc\u6709\u591a\u6b65\u526f\u4f5c\u7528\uff08\u5199\u6587\u4ef6\u3001\u6253\u65e5\u5fd7\u3001\u8c03\u63a5\u53e3\uff09<\/li>\n<li>\u5d4c\u5957\u8fc7\u6df1\uff0c\u540c\u4e8b\u8bfb\u4e0d\u61c2<\/li>\n<li>\u9700\u8981\u8c03\u8bd5\u4e2d\u95f4\u53d8\u91cf\u65f6<\/li>\n<\/ul>\n<p>\u53e3\u8bc0\uff1a<strong>\u53d8\u6362\u548c\u8fc7\u6ee4\u7528\u63a8\u5bfc\u5f0f\uff1b\u4e1a\u52a1\u6d41\u7a0b\u7528 for<\/strong>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5217\u8868\u63a8\u5bfc\u5f0f\u5199\u8d77\u6765\u77ed\u3001\u8bfb\u8d77\u6765\u5feb\uff0c\u662f Python \u91cc\u6700\u53d7\u6b22\u8fce\u7684\u8bed\u6cd5\u7cd6\u4e4b\u4e00\u3002\u4f46\u5b83\u4e5f\u5bb9\u6613\u88ab\u6ee5\u7528\u3002\u672c\u6587\u628a\u63a8\u5bfc\u5f0f\u3001\u751f\u6210\u5668 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-python-basics"],"_links":{"self":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7"}],"version-history":[{"count":0,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/7\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}