{"id":66,"date":"2026-06-13T16:54:55","date_gmt":"2026-06-13T08:54:55","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=66"},"modified":"2026-06-13T16:54:55","modified_gmt":"2026-06-13T08:54:55","slug":"python-%e6%8e%a8%e5%af%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/06\/13\/python-%e6%8e%a8%e5%af%bc%e5%bc%8f\/","title":{"rendered":"Python \u63a8\u5bfc\u5f0f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 Python 3 \u4e2d\uff0c<strong>\u63a8\u5bfc\u5f0f (Comprehensions)<\/strong> \u662f\u6700\u5177\u6807\u5fd7\u6027\u3001\u6700\u4f18\u96c5\u4e14\u6700\u9ad8\u6548\u7684\u8bed\u6cd5\u7279\u6027\u4e4b\u4e00\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u7528\u4e00\u884c\u7b80\u6d01\u3001\u58f0\u660e\u5f0f\u7684\u4ee3\u7801\uff0c\u57fa\u4e8e\u73b0\u6709\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5feb\u901f\u6784\u5efa\u51fa\u65b0\u7684\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u6216\u751f\u6210\u5668\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a8\u5bfc\u5f0f\u4e0d\u4ec5\u4ec5\u662f\u4e00\u79cd\u201c\u8bed\u6cd5\u7cd6\u201d\uff0c\u5b83\u5728 CPython \u5e95\u5c42\u7ecf\u8fc7\u4e86\u9ad8\u5ea6\u4f18\u5316\uff0c\u5176\u6267\u884c\u901f\u5ea6\u901a\u5e38\u663e\u8457\u5feb\u4e8e\u7b49\u6548\u7684\u4f20\u7edf <code>for<\/code> \u5faa\u73af\u914d\u5408 <code>append()<\/code> \u7684\u5199\u6cd5\u3002\u638c\u63e1\u63a8\u5bfc\u5f0f\uff0c\u662f\u533a\u5206 Python \u65b0\u624b\u4e0e\u8d44\u6df1\u5f00\u53d1\u8005\u7684\u91cd\u8981\u6807\u5fd7\uff0c\u4e5f\u662f\u5199\u51fa\u201cPythonic\u201d\uff08\u5730\u9053 Python \u98ce\u683c\uff09\u4ee3\u7801\u7684\u5fc5\u7ecf\u4e4b\u8def\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u5bf9 Python 3 \u63a8\u5bfc\u5f0f\u7684\u5168\u9762\u3001\u6df1\u5ea6\u89e3\u6790\uff0c\u6db5\u76d6\u56db\u5927\u63a8\u5bfc\u5f0f\u7c7b\u578b\u3001\u5e95\u5c42\u539f\u7406\u3001\u6027\u80fd\u5206\u6790\u4ee5\u53ca\u6838\u5fc3\u907f\u5751\u6307\u5357\u3002\uff08\u672c\u6587\u7bc7\u5e45\u8f83\u957f\uff0c\u65e8\u5728\u63d0\u4f9b\u4e00\u4efd\u53ef\u4f5c\u4e3a\u751f\u4ea7\u73af\u5883\u53c2\u8003\u624b\u518c\u7684\u6df1\u5ea6\u6307\u5357\uff09<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u3001 \u5217\u8868\u63a8\u5bfc\u5f0f (List Comprehension)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u5e38\u7528\u3001\u6700\u57fa\u7840\u7684\u63a8\u5bfc\u5f0f\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u6781\u5176\u7d27\u51d1\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u65b0\u5217\u8868\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u57fa\u7840\u8bed\u6cd5<\/h4>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">[expression for item in iterable]<\/code><\/pre><\/div><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>iterable<\/code>\uff1a\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u7b26\u4e32\u3001<code>range<\/code> \u7b49\uff09\u3002<\/li>\n\n\n\n<li><code>item<\/code>\uff1a\u6bcf\u6b21\u8fed\u4ee3\u4ece <code>iterable<\/code> \u4e2d\u53d6\u51fa\u7684\u5143\u7d20\u3002<\/li>\n\n\n\n<li><code>expression<\/code>\uff1a\u57fa\u4e8e <code>item<\/code> \u8ba1\u7b97\u5f97\u51fa\u7684\u65b0\u503c\uff0c\u5b83\u5c06\u4f5c\u4e3a\u65b0\u5217\u8868\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u9700\u6c42\uff1a\u751f\u6210 0 \u5230 9 \u7684\u5e73\u65b9\u5217\u8868\n\n# \u274c \u4f20\u7edf\u5199\u6cd5 (\u5197\u957f)\nsquares = []\nfor i in range(10):\n    squares.append(i ** 2)\n\n# \u2705 \u5217\u8868\u63a8\u5bfc\u5f0f\u5199\u6cd5 (\u7b80\u6d01\u3001\u9ad8\u6548)\nsquares = [i ** 2 for i in range(10)]\n# \u7ed3\u679c: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u5e26\u6761\u4ef6\u8fc7\u6ee4 (Filtering)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u5728\u63a8\u5bfc\u5f0f\u672b\u5c3e\u6dfb\u52a0 <code>if<\/code> \u5b50\u53e5\uff0c\u4ec5\u5f53\u6761\u4ef6\u4e3a <code>True<\/code> \u65f6\uff0c\u624d\u5c06\u5143\u7d20\u52a0\u5165\u65b0\u5217\u8868\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u9700\u6c42\uff1a\u751f\u6210 0 \u5230 19 \u4e2d\u6240\u6709\u5076\u6570\u7684\u5e73\u65b9\neven_squares = [i ** 2 for i in range(20) if i % 2 == 0]\n# \u7ed3\u679c: [0, 4, 16, 36, 64, 100, 144, 196, 256, 324]<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u5e26\u6761\u4ef6\u8868\u8fbe\u5f0f (Ternary Operator)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u9700\u8981\u6839\u636e\u6761\u4ef6\u5bf9\u5143\u7d20\u8fdb\u884c<strong>\u4e0d\u540c\u7684\u8f6c\u6362<\/strong>\uff08\u800c\u4e0d\u662f\u8fc7\u6ee4\uff09\uff0c\u53ef\u4ee5\u5c06\u4e09\u5143\u8fd0\u7b97\u7b26\u653e\u5728 <code>expression<\/code> \u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u9700\u6c42\uff1a\u5c06\u5076\u6570\u6807\u8bb0\u4e3a &quot;Even&quot;\uff0c\u5947\u6570\u6807\u8bb0\u4e3a &quot;Odd&quot;\nlabels = [&quot;Even&quot; if i % 2 == 0 else &quot;Odd&quot; for i in range(5)]\n# \u7ed3\u679c: [&#039;Even&#039;, &#039;Odd&#039;, &#039;Even&#039;, &#039;Odd&#039;, &#039;Even&#039;]<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u26a0\ufe0f \u6ce8\u610f<\/em>\uff1a<code>if-else<\/code> \u653e\u5728 <code>for<\/code> \u4e4b\u524d\u662f<strong>\u6761\u4ef6\u8868\u8fbe\u5f0f<\/strong>\uff08\u5fc5\u987b\u540c\u65f6\u6709 <code>if<\/code> \u548c <code>else<\/code>\uff09\uff1b\u653e\u5728 <code>for<\/code> \u4e4b\u540e\u662f<strong>\u8fc7\u6ee4\u6761\u4ef6<\/strong>\uff08\u53ea\u80fd\u6709 <code>if<\/code>\uff0c\u4e0d\u80fd\u6709 <code>else<\/code>\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4. \u5d4c\u5957\u5faa\u73af (Nested Loops)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5217\u8868\u63a8\u5bfc\u5f0f\u652f\u6301\u591a\u4e2a <code>for<\/code> \u5b50\u53e5\uff0c\u5176\u6267\u884c\u987a\u5e8f\u4e0e\u5d4c\u5957\u7684 <code>for<\/code> \u5faa\u73af\u5b8c\u5168\u4e00\u81f4\uff08\u4ece\u5de6\u5230\u53f3\uff09\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u9700\u6c42\uff1a\u751f\u6210\u4e24\u4e2a\u5217\u8868\u7684\u7b1b\u5361\u5c14\u79ef (\u6240\u6709\u53ef\u80fd\u7684\u5750\u6807\u5bf9)\nx_vals = [1, 2]\ny_vals = [&#039;a&#039;, &#039;b&#039;]\n\ncoords = [(x, y) for x in x_vals for y in y_vals]\n# \u7ed3\u679c: [(1, &#039;a&#039;), (1, &#039;b&#039;), (2, &#039;a&#039;), (2, &#039;b&#039;)]\n\n# \u7b49\u4ef7\u7684\u4f20\u7edf\u5199\u6cd5\uff1a\n# coords = []\n# for x in x_vals:\n#     for y in y_vals:\n#         coords.append((x, y))<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">5. \u66ff\u4ee3 <code>map<\/code> \u548c <code>filter<\/code><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python 2 \u65f6\u4ee3\uff0c<code>map<\/code> \u548c <code>filter<\/code> \u7ed3\u5408 <code>lambda<\/code> \u5f88\u5e38\u89c1\u3002\u4f46\u5728 Python 3 \u4e2d\uff0c<strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u5728\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u4e0a\u901a\u5e38\u4f18\u4e8e <code>map<\/code>\/<code>filter<\/code><\/strong>\uff0c\u662f\u5b98\u65b9\u66f4\u63a8\u8350\u7684\u505a\u6cd5\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">nums = [1, 2, 3, 4, 5]\n\n# \u4f7f\u7528 map + lambda (\u53ef\u8bfb\u6027\u8f83\u5dee)\ndoubled = list(map(lambda x: x * 2, nums))\n\n# \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f (\u66f4\u6e05\u6670\u3001\u66f4 Pythonic)\ndoubled = [x * 2 for x in nums]<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001 \u5b57\u5178\u63a8\u5bfc\u5f0f (Dictionary Comprehension)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b57\u5178\u63a8\u5bfc\u5f0f\u7684\u8bed\u6cd5\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u6781\u5176\u76f8\u4f3c\uff0c\u53ea\u662f\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code>\uff0c\u5e76\u4e14 <code>expression<\/code> \u90e8\u5206\u5fc5\u987b\u662f <code>key: value<\/code> \u7684\u5f62\u5f0f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u57fa\u7840\u8bed\u6cd5<\/h4>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">{key_expression: value_expression for item in iterable}<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u7ecf\u5178\u5e94\u7528\u573a\u666f<\/h4>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u573a\u666f 1\uff1a\u5feb\u901f\u521b\u5efa\u6620\u5c04\u5173\u7cfb\nsquares_dict = {x: x**2 for x in range(1, 6)}\n# \u7ed3\u679c: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}\n\n# \u573a\u666f 2\uff1a\u53cd\u8f6c\u5b57\u5178\u7684\u952e\u548c\u503c (\u524d\u63d0\u662f\u539f\u5b57\u5178\u7684\u503c\u662f\u53ef\u54c8\u5e0c\u4e14\u552f\u4e00\u7684)\noriginal = {&quot;a&quot;: 1, &quot;b&quot;: 2, &quot;c&quot;: 3}\nreversed_dict = {v: k for k, v in original.items()}\n# \u7ed3\u679c: {1: &#039;a&#039;, 2: &#039;b&#039;, 3: &#039;c&#039;}\n\n# \u573a\u666f 3\uff1a\u8fc7\u6ee4\u5b57\u5178\u4e2d\u7684\u7279\u5b9a\u9879\nscores = {&quot;Alice&quot;: 85, &quot;Bob&quot;: 42, &quot;Charlie&quot;: 90}\npassed_students = {name: score for name, score in scores.items() if score &gt;= 60}\n# \u7ed3\u679c: {&#039;Alice&#039;: 85, &#039;Charlie&#039;: 90}\n\n# \u573a\u666f 4\uff1a\u4ece\u4e24\u4e2a\u5217\u8868\u6784\u5efa\u5b57\u5178 (\u7ed3\u5408 zip)\nkeys = [&quot;name&quot;, &quot;age&quot;, &quot;city&quot;]\nvalues = [&quot;Alice&quot;, 28, &quot;NY&quot;]\nuser_dict = {k: v for k, v in zip(keys, values)}\n# \u7ed3\u679c: {&#039;name&#039;: &#039;Alice&#039;, &#039;age&#039;: 28, &#039;city&#039;: &#039;NY&#039;}<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001 \u96c6\u5408\u63a8\u5bfc\u5f0f (Set Comprehension)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u96c6\u5408\u63a8\u5bfc\u5f0f\u540c\u6837\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code>\uff0c\u4f46\u5176 <code>expression<\/code> \u90e8\u5206\u53ea\u6709\u4e00\u4e2a\u503c\uff08\u6ca1\u6709\u5192\u53f7\uff09\u3002\u5b83\u7684\u6838\u5fc3\u7279\u6027\u662f<strong>\u81ea\u52a8\u53bb\u91cd<\/strong>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u57fa\u7840\u8bed\u6cd5<\/h4>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">{expression for item in iterable}<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u7ecf\u5178\u5e94\u7528\u573a\u666f<\/h4>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u573a\u666f 1\uff1a\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u552f\u4e00\u5143\u97f3\u5b57\u6bcd\u5e76\u8f6c\u4e3a\u5927\u5199\ntext = &quot;hello world, welcome to python&quot;\nvowels = {char.upper() for char in text if char in &quot;aeiou&quot;}\n# \u7ed3\u679c: {&#039;E&#039;, &#039;O&#039;} (\u6ce8\u610f\uff1a\u96c6\u5408\u662f\u65e0\u5e8f\u7684)\n\n# \u573a\u666f 2\uff1a\u5bf9\u5217\u8868\u8fdb\u884c\u53bb\u91cd\u5e76\u8f6c\u6362\nraw_ids = [&quot;001&quot;, &quot;002&quot;, &quot;001&quot;, &quot;003&quot;]\nunique_ids = {int(id_str) for id_str in raw_ids}\n# \u7ed3\u679c: {1, 2, 3}<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001 \u751f\u6210\u5668\u63a8\u5bfc\u5f0f (Generator Expression) \ud83c\udf1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u63a8\u5bfc\u5f0f\u5bb6\u65cf\u4e2d<strong>\u6700\u91cd\u8981\u3001\u6700\u5bb9\u6613\u88ab\u5ffd\u89c6<\/strong>\u7684\u6210\u5458\u3002\u5b83\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u8bed\u6cd5<strong>\u552f\u4e00\u533a\u522b<\/strong>\u5728\u4e8e\uff1a\u4f7f\u7528<strong>\u5706\u62ec\u53f7 <code>()<\/code><\/strong> \u800c\u4e0d\u662f\u65b9\u62ec\u53f7 <code>[]<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u6838\u5fc3\u7279\u6027\uff1a\u60f0\u6027\u6c42\u503c (Lazy Evaluation)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5217\u8868\u63a8\u5bfc\u5f0f\u4f1a<strong>\u7acb\u5373<\/strong>\u5728\u5185\u5b58\u4e2d\u8ba1\u7b97\u5e76\u751f\u6210\u5b8c\u6574\u7684\u5217\u8868\u3002\u800c\u751f\u6210\u5668\u63a8\u5bfc\u5f0f\u8fd4\u56de\u7684\u662f\u4e00\u4e2a<strong>\u751f\u6210\u5668\u5bf9\u8c61 (Generator Object)<\/strong>\u3002\u5b83\u4e0d\u4f1a\u7acb\u5373\u8ba1\u7b97\u6240\u6709\u503c\uff0c\u800c\u662f\u6bcf\u6b21\u5728\u8fed\u4ee3\u65f6\uff08\u5982\u8c03\u7528 <code>next()<\/code> \u6216\u5728 <code>for<\/code> \u5faa\u73af\u4e2d\uff09\u624d\u52a8\u6001\u8ba1\u7b97\u5e76\u4ea7\u51fa\u4e0b\u4e00\u4e2a\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u9707\u64bc\u7684\u5185\u5b58\u4f18\u52bf<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u5904\u7406\u6d77\u91cf\u6570\u636e\u65f6\uff0c\u751f\u6210\u5668\u63a8\u5bfc\u5f0f\u662f\u62ef\u6551\u5185\u5b58\u7684\u552f\u4e00\u9009\u62e9\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">import sys\n\n# \u5217\u8868\u63a8\u5bfc\u5f0f\uff1a\u7acb\u5373\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u5305\u542b 1000 \u4e07\u4e2a\u6574\u6570\u7684\u5217\u8868\nlist_comp = [x ** 2 for x in range(10_000_000)]\nprint(sys.getsizeof(list_comp))  # \u7ea6 81,528,000 bytes (\u7ea6 81 MB)\n\n# \u751f\u6210\u5668\u63a8\u5bfc\u5f0f\uff1a\u4ec5\u521b\u5efa\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u751f\u6210\u5668\u5bf9\u8c61\ngen_exp = (x ** 2 for x in range(10_000_000))\nprint(sys.getsizeof(gen_exp))    # \u7ea6 104 bytes (\u4ec5\u7ea6 0.1 KB\uff01)\n\n# \u4f7f\u7528\u751f\u6210\u5668\nprint(next(gen_exp))  # 0\nprint(next(gen_exp))  # 1\n# ... \u53ea\u6709\u5728\u9700\u8981\u65f6\u624d\u8ba1\u7b97\uff0c\u5185\u5b58\u5360\u7528\u59cb\u7ec8\u4fdd\u6301\u5728\u6781\u4f4e\u6c34\u5e73<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u9690\u5f0f\u751f\u6210\u5668\u63a8\u5bfc\u5f0f<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u751f\u6210\u5668\u63a8\u5bfc\u5f0f\u4f5c\u4e3a\u51fd\u6570\u7684<strong>\u552f\u4e00\u53c2\u6570<\/strong>\u65f6\uff0c\u5916\u5c42\u7684\u5706\u62ec\u53f7\u53ef\u4ee5\u7701\u7565\uff0c\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u8ba1\u7b97 0 \u5230 9 \u7684\u5e73\u65b9\u548c\n# \u663e\u5f0f\u5199\u6cd5\ntotal = sum((x ** 2 for x in range(10)))\n\n# \u9690\u5f0f\u5199\u6cd5 (\u63a8\u8350\uff0c\u66f4\u4f18\u96c5)\ntotal = sum(x ** 2 for x in range(10))<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e94\u3001 \u63a8\u5bfc\u5f0f\u7684\u5e95\u5c42\u539f\u7406\u4e0e\u6027\u80fd\u5206\u6790<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4ec0\u4e48\u63a8\u5bfc\u5f0f\u901a\u5e38\u6bd4\u7b49\u6548\u7684 <code>for<\/code> \u5faa\u73af\u66f4\u5feb\uff1f\u8fd9\u6e90\u4e8e CPython \u89e3\u91ca\u5668\u5728\u5b57\u8282\u7801\u5c42\u9762\u7684\u4f18\u5316\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u907f\u514d\u4e86\u5c5e\u6027\u67e5\u627e\u5f00\u9500<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f20\u7edf <code>for<\/code> \u5faa\u73af\u4e2d\uff1a<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">result = []\nfor i in range(1000):\n    result.append(i)<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bcf\u6b21\u5faa\u73af\u8fed\u4ee3\u65f6\uff0cPython \u90fd\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728 <code>result<\/code> \u5bf9\u8c61\u4e2d\u67e5\u627e <code>append<\/code> \u5c5e\u6027\uff08<code>LOAD_ATTR<\/code> \u5b57\u8282\u7801\uff09\u3002<\/li>\n\n\n\n<li>\u52a0\u8f7d\u53c2\u6570 <code>i<\/code>\uff08<code>LOAD_FAST<\/code>\uff09\u3002<\/li>\n\n\n\n<li>\u8c03\u7528\u8be5\u51fd\u6570\uff08<code>CALL_FUNCTION<\/code>\uff09\u3002<br>\u8fd9\u79cd\u91cd\u590d\u7684\u5c5e\u6027\u67e5\u627e\u5728\u767e\u4e07\u6b21\u5faa\u73af\u4e2d\u4f1a\u4ea7\u751f\u663e\u8457\u7684\u6027\u80fd\u635f\u8017\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u4e13\u7528\u7684\u5b57\u8282\u7801\u6307\u4ee4<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\uff0cCPython \u4f7f\u7528\u4e86\u4e13\u7528\u7684 <code>LIST_APPEND<\/code> \u5b57\u8282\u7801\u6307\u4ee4\u3002\u8fd9\u4e2a\u6307\u4ee4\u76f4\u63a5\u5728 C \u8bed\u8a00\u5c42\u9762\u64cd\u4f5c\u5217\u8868\u7684\u5e95\u5c42\u6570\u7ec4\uff0c<strong>\u5b8c\u5168\u7ed5\u8fc7\u4e86 Python \u5c42\u9762\u7684\u5c5e\u6027\u67e5\u627e\u548c\u51fd\u6570\u8c03\u7528\u5f00\u9500<\/strong>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5 (Benchmark)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u8fdb\u884c\u5bf9\u6bd4\uff1a<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">import timeit\n\n# \u4f20\u7edf for \u5faa\u73af\nsetup = &quot;result = []&quot;\nstmt_for = &quot;for i in range(1000): result.append(i)&quot;\ntime_for = timeit.timeit(stmt_for, setup=setup, number=10000)\n\n# \u5217\u8868\u63a8\u5bfc\u5f0f\nstmt_comp = &quot;[i for i in range(1000)]&quot;\ntime_comp = timeit.timeit(stmt_comp, number=10000)\n\nprint(f&quot;For \u5faa\u73af\u8017\u65f6: {time_for:.4f} \u79d2&quot;)\nprint(f&quot;\u63a8\u5bfc\u5f0f\u8017\u65f6:   {time_comp:.4f} \u79d2&quot;)\n# \u5178\u578b\u7ed3\u679c\uff1a\u63a8\u5bfc\u5f0f\u901a\u5e38\u6bd4 for \u5faa\u73af\u5feb 20% \u5230 50%\u3002<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u516d\u3001 \u6838\u5fc3\u907f\u5751\u6307\u5357\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c3d\u7ba1\u63a8\u5bfc\u5f0f\u6781\u5176\u5f3a\u5927\uff0c\u4f46\u6ee5\u7528\u4f1a\u5bfc\u81f4\u4ee3\u7801\u53ef\u8bfb\u6027\u6025\u5267\u4e0b\u964d\uff0c\u751a\u81f3\u5f15\u5165\u96be\u4ee5\u5bdf\u89c9\u7684 Bug\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u274c \u9677\u9631\u4e00\uff1a\u8fc7\u5ea6\u5d4c\u5957\uff0c\u727a\u7272\u53ef\u8bfb\u6027<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a8\u5bfc\u5f0f\u7684\u76ee\u7684\u662f<strong>\u63d0\u9ad8\u53ef\u8bfb\u6027<\/strong>\u3002\u5982\u679c\u903b\u8f91\u590d\u6742\u5230\u9700\u8981\u9605\u8bfb\u8d85\u8fc7 2 \u79d2\u624d\u80fd\u7406\u89e3\uff0c\u8bf7\u679c\u65ad\u9000\u56de\u5230\u4f20\u7edf\u7684 <code>for<\/code> \u5faa\u73af\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u274c \u53cd\u6a21\u5f0f\uff1a\u4e09\u5c42\u5d4c\u5957 + \u590d\u6742\u6761\u4ef6\uff0c\u5b9b\u5982\u5929\u4e66\nresult = [x * y for x in range(10) for y in range(10) if x % 2 == 0 if y % 3 == 0 if x + y &gt; 5]\n\n# \u2705 \u6700\u4f73\u5b9e\u8df5\uff1a\u4f7f\u7528\u4f20\u7edf\u5faa\u73af\uff0c\u903b\u8f91\u6e05\u6670\uff0c\u6613\u4e8e\u8c03\u8bd5\nresult = []\nfor x in range(10):\n    if x % 2 != 0:\n        continue\n    for y in range(10):\n        if y % 3 != 0:\n            continue\n        if x + y &gt; 5:\n            result.append(x * y)<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u7ecf\u9a8c\u6cd5\u5219<\/em>\uff1a\u63a8\u5bfc\u5f0f\u4e2d\u7684 <code>for<\/code> \u5b50\u53e5\u548c <code>if<\/code> \u5b50\u53e5\u603b\u6570<strong>\u4e0d\u5e94\u8d85\u8fc7 2 \u4e2a<\/strong>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u274c \u9677\u9631\u4e8c\uff1a\u5728\u63a8\u5bfc\u5f0f\u4e2d\u5f15\u5165\u201c\u526f\u4f5c\u7528 (Side Effects)\u201d<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a8\u5bfc\u5f0f\u7684\u8bed\u4e49\u662f <strong>\u201c\u6784\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u7ed3\u6784\u201d<\/strong>\u3002\u5b83<strong>\u4e0d\u5e94\u8be5<\/strong>\u88ab\u7528\u6765\u6267\u884c\u4fee\u6539\u5916\u90e8\u72b6\u6001\u7684\u64cd\u4f5c\uff08\u5982\u6253\u5370\u3001\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3001\u5199\u5165\u6587\u4ef6\uff09\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u274c \u4e25\u91cd\u53cd\u6a21\u5f0f\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4ec5\u4ec5\u4e3a\u4e86\u6267\u884c print<\/code><\/pre><\/div><\/div>\n\n\n<p>[print(x) for x in range(5)]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"># \u7ed3\u679c\uff1a\u867d\u7136\u6253\u5370\u4e86 0-4\uff0c\u4f46\u540c\u65f6\u4e5f\u521b\u5efa\u4e86\u4e00\u4e2a\u65e0\u7528\u7684 [None, None, None, None, None] \u5217\u8868\uff0c\u6d6a\u8d39\u5185\u5b58\u3002 # \u2705 \u6b63\u786e\u505a\u6cd5\uff1a\u4f7f\u7528\u666e\u901a\u7684 for \u5faa\u73af\u5904\u7406\u526f\u4f5c\u7528 for x in range(5): print(x)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u274c \u9677\u9631\u4e09\uff1a\u53d8\u91cf\u6cc4\u9732 (Variable Leakage)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <strong>Python 2<\/strong> \u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u7684\u5faa\u73af\u53d8\u91cf\u4f1a\u201c\u6cc4\u9732\u201d\u5230\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\uff0c\u8fd9\u66fe\u662f\u4e00\u4e2a\u8457\u540d\u7684 Bug\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># Python 2 \u7684\u884c\u4e3a (\u5df2\u5e9f\u5f03)\n# x = 10\n# [x for x in range(3)]\n# print(x) # \u8f93\u51fa 2\uff0c\u5916\u90e8\u53d8\u91cf\u88ab\u6c61\u67d3\uff01<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u2705 Python 3 \u7684\u4fee\u590d<\/strong>\uff1a\u5728 Python 3 \u4e2d\uff0c\u5217\u8868\u3001\u96c6\u5408\u548c\u5b57\u5178\u63a8\u5bfc\u5f0f\u90fd\u62e5\u6709<strong>\u72ec\u7acb\u7684\u4f5c\u7528\u57df<\/strong>\u3002\u5faa\u73af\u53d8\u91cf\u4e0d\u4f1a\u6cc4\u9732\u5230\u5916\u90e8\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># Python 3 \u7684\u884c\u4e3a (\u5b89\u5168)\nx = 100\nsquares = [x ** 2 for x in range(3)]\nprint(x)  # \u8f93\u51fa: 100 (\u5916\u90e8\u53d8\u91cf\u672a\u53d7\u5f71\u54cd)<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>(\u6ce8\uff1a\u751f\u6210\u5668\u63a8\u5bfc\u5f0f\u5728 Python 2 \u548c Python 3 \u4e2d\u59cb\u7ec8\u62e5\u6709\u72ec\u7acb\u4f5c\u7528\u57df\uff0c\u4ece\u672a\u6709\u8fc7\u6cc4\u9732\u95ee\u9898\u3002)<\/em><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4. \ud83d\udca1 \u6700\u4f73\u5b9e\u8df5\uff1a\u4f55\u65f6\u9009\u62e9\u54ea\u79cd\u63a8\u5bfc\u5f0f\uff1f<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9700\u8981\u5b8c\u6574\u7684\u7ed3\u679c\u5217\u8868\uff0c\u4e14\u6570\u636e\u91cf\u9002\u4e2d<\/strong> \u2794 <strong>\u5217\u8868\u63a8\u5bfc\u5f0f <code>[]<\/code><\/strong><\/li>\n\n\n\n<li><strong>\u9700\u8981\u952e\u503c\u6620\u5c04\u6216\u53cd\u8f6c\u5b57\u5178<\/strong> \u2794 <strong>\u5b57\u5178\u63a8\u5bfc\u5f0f <code>{k: v}<\/code><\/strong><\/li>\n\n\n\n<li><strong>\u9700\u8981\u552f\u4e00\u5143\u7d20\u96c6\u5408<\/strong> \u2794 <strong>\u96c6\u5408\u63a8\u5bfc\u5f0f <code>{x}<\/code><\/strong><\/li>\n\n\n\n<li><strong>\u6570\u636e\u91cf\u6781\u5927\u3001\u53ea\u9700\u904d\u5386\u4e00\u6b21\u3001\u6216\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570<\/strong> \u2794 <strong>\u751f\u6210\u5668\u63a8\u5bfc\u5f0f <code>()<\/code><\/strong> (\u9996\u9009\uff01)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e03\u3001 \u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u7684\u63a8\u5bfc\u5f0f\u662f\u8bed\u8a00\u8bbe\u8ba1\u4e2d\u201c\u4f18\u96c5\u201d\u4e0e\u201c\u5b9e\u7528\u201d\u5b8c\u7f8e\u7ed3\u5408\u7684\u5178\u8303\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8bed\u6cd5\u5c42\u9762<\/strong>\uff1a\u5b83\u5c06\u591a\u884c\u7684\u5faa\u73af\u3001\u6761\u4ef6\u5224\u65ad\u548c\u6570\u636e\u7ed3\u6784\u6784\u5efa\u538b\u7f29\u4e3a\u4e00\u884c\u9ad8\u5ea6\u58f0\u660e\u5f0f\u7684\u4ee3\u7801\uff0c\u6781\u5927\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u8868\u8fbe\u529b\u3002<\/li>\n\n\n\n<li><strong>\u6027\u80fd\u5c42\u9762<\/strong>\uff1a\u5f97\u76ca\u4e8e CPython \u5e95\u5c42\u7684 <code>LIST_APPEND<\/code> \u7b49\u4e13\u7528\u5b57\u8282\u7801\u4f18\u5316\uff0c\u63a8\u5bfc\u5f0f\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6bd4\u624b\u5199\u7684 <code>for<\/code> + <code>append<\/code> \u8fd0\u884c\u5f97\u66f4\u5feb\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5b58\u5c42\u9762<\/strong>\uff1a\u751f\u6210\u5668\u63a8\u5bfc\u5f0f <code>( )<\/code> \u63d0\u4f9b\u7684\u60f0\u6027\u6c42\u503c\u673a\u5236\uff0c\u4f7f\u5f97 Python \u80fd\u591f\u4ee5\u6781\u4f4e\u7684\u5185\u5b58\u5f00\u9500\u4f18\u96c5\u5730\u5904\u7406 GB \u7ea7\u522b\u7684\u6570\u636e\u6d41\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u638c\u63e1\u63a8\u5bfc\u5f0f\uff0c\u610f\u5473\u7740\u4f60\u5f00\u59cb\u6446\u8131\u5176\u4ed6\u547d\u4ee4\u5f0f\u8bed\u8a00\u7684\u601d\u7ef4\u60ef\u6027\uff0c\u771f\u6b63\u62e5\u62b1 Python \u7684\u51fd\u6570\u5f0f\u548c\u58f0\u660e\u5f0f\u7f16\u7a0b\u8303\u5f0f\u3002\u5728\u65e5\u5e38\u7f16\u7801\u4e2d\uff0c\u517b\u6210\u201c\u9047\u5230\u6570\u636e\u8f6c\u6362\u9996\u5148\u601d\u8003\u80fd\u5426\u7528\u63a8\u5bfc\u5f0f\u89e3\u51b3\u201d\u7684\u4e60\u60ef\uff0c\u4f60\u7684\u4ee3\u7801\u8d28\u91cf\u5c06\u5f97\u5230\u8d28\u7684\u98de\u8dc3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5bf9\u63a8\u5bfc\u5f0f\u5728\u7279\u5b9a\u590d\u6742\u573a\u666f\u4e0b\u7684\u5e94\u7528\uff08\u5982\u7ed3\u5408 <code>itertools<\/code> \u5904\u7406\u591a\u7ef4\u6570\u636e\uff09\uff0c\u6216\u8005\u5bf9\u751f\u6210\u5668\u63a8\u5bfc\u5f0f\u4e0e <code>yield<\/code> \u5173\u952e\u5b57\u7684\u6df1\u5ea6\u5bf9\u6bd4\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u968f\u65f6\u6df1\u5165\u63a2\u8ba8\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python 3 \u4e2d\uff0c\u63a8\u5bfc\u5f0f (Compreh&#8230;<\/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-66","post","type-post","status-publish","format-standard","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/66","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=66"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/66\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}