{"id":1129587,"date":"2025-01-08T20:30:52","date_gmt":"2025-01-08T12:30:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1129587.html"},"modified":"2025-01-08T20:30:54","modified_gmt":"2025-01-08T12:30:54","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%97%e8%a1%a8%e8%be%93%e5%87%ba%e5%8d%95%e7%8b%ac%e7%9a%84%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1129587.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u5217\u8868\u8f93\u51fa\u5355\u72ec\u7684\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095940\/538e6660-6ee4-479a-9a4a-4fab1a9c239a.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u5217\u8868\u8f93\u51fa\u5355\u72ec\u7684\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u5355\u72ec\u8f93\u51fa\u3002<\/strong> \u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u5217\u8868\u89e3\u6790\u3001\u4ee5\u53ca\u5185\u7f6e\u7684\u51fd\u6570\u548c\u5e93\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u7ed9\u51fa\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u662f\u6700\u57fa\u672c\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9010\u4e00\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for item in my_list:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u61c2\u3001\u6613\u4e8e\u5b9e\u73b0\uff0c\u4f46\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u5217\u8868\u65f6\uff0c\u53ef\u80fd\u4f1a\u8f83\u6162\u3002<\/strong><\/p>\n<\/p>\n<p><h4>\u4f18\u5316for\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u751f\u6210\u5668\u6216\u5176\u4ed6\u4f18\u5316\u6280\u672f\u3002\u751f\u6210\u5668\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u4f1a\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generator_example(lst):<\/p>\n<p>    for item in lst:<\/p>\n<p>        yield item<\/p>\n<p>gen = generator_example(my_list)<\/p>\n<p>for item in gen:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u904d\u5386\u548c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">[print(item) for item in my_list]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u3001\u6613\u8bfb\uff0c\u4f46\u4e0efor\u5faa\u73af\u76f8\u6bd4\uff0c\u6027\u80fd\u63d0\u5347\u5e76\u4e0d\u663e\u8457\uff0c\u66f4\u591a\u7684\u662f\u51cf\u5c11\u4ee3\u7801\u884c\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><h4>\u5217\u8868\u89e3\u6790\u7684\u8fdb\u9636\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u904d\u5386\u548c\u8f93\u51fa\uff0c\u8fd8\u53ef\u4ee5\u52a0\u5165\u6761\u4ef6\u548c\u5176\u4ed6\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ea\u8f93\u51fa\u5076\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">[print(item) for item in my_list if item % 2 == 0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5e93<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\u548c\u5e93\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u8f93\u51fa\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528map\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>map<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u5230\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list(map(print, my_list))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u770b\u4f3c\u7b80\u6d01\uff0c\u4f46\u9700\u8981\u6ce8\u610f<code>map<\/code>\u51fd\u6570\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u800c\u4e0d\u662f\u76f4\u63a5\u8f93\u51fa\u7ed3\u679c\uff0c\u56e0\u6b64\u53ef\u80fd\u9700\u8981\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u5904\u7406\u3002<\/strong><\/p>\n<\/p>\n<p><h4>\u4f7f\u7528numpy\u5e93<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>numpy<\/code>\u5e93\u3002<code>numpy<\/code>\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u6570\u636e\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>np_list = np.array(my_list)<\/p>\n<p>print(np_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\u662f\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u6548\u7387\u9ad8\uff0c\u4f46\u9700\u8981\u5b89\u88c5\u548c\u5bfc\u5165\u989d\u5916\u7684\u5e93\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u5217\u8868\u6216\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_print(lst):<\/p>\n<p>    for item in lst:<\/p>\n<p>        if isinstance(item, list):<\/p>\n<p>            recursive_print(item)<\/p>\n<p>        else:<\/p>\n<p>            print(item)<\/p>\n<p>nested_list = [1, [2, [3, 4], 5], 6]<\/p>\n<p>recursive_print(nested_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u5904\u7406\u4efb\u610f\u6df1\u5ea6\u7684\u5d4c\u5957\u5217\u8868\uff0c\u4f46\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6808\u6ea2\u51fa\uff0c\u9700\u8981\u8c28\u614e\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u6700\u4f73\u6548\u679c\u3002\u4f8b\u5982\uff0c\u5148\u4f7f\u7528\u751f\u6210\u5668\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u518d\u4f7f\u7528\u9012\u5f52\u5904\u7406\u5d4c\u5957\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def combined_method(lst):<\/p>\n<p>    def generator(lst):<\/p>\n<p>        for item in lst:<\/p>\n<p>            if isinstance(item, list):<\/p>\n<p>                yield from generator(item)<\/p>\n<p>            else:<\/p>\n<p>                yield item<\/p>\n<p>    gen = generator(lst)<\/p>\n<p>    for item in gen:<\/p>\n<p>        print(item)<\/p>\n<p>combined_method(nested_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u8c03\u6574\uff0c\u4f46\u5b9e\u73b0\u8d77\u6765\u53ef\u80fd\u8f83\u4e3a\u590d\u6742\uff0c\u9700\u8981\u638c\u63e1\u591a\u79cd\u6280\u672f\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6027\u80fd\u6bd4\u8f83\u4e0e\u4f18\u5316\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4e0d\u540c\u65b9\u6cd5\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u6027\u80fd\u8868\u73b0\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5c0f\u89c4\u6a21\u6570\u636e<\/strong>\uff1a\u4f18\u5148\u4f7f\u7528\u7b80\u5355\u7684for\u5faa\u73af\u6216\u5217\u8868\u89e3\u6790\u3002<\/li>\n<li><strong>\u5927\u89c4\u6a21\u6570\u636e<\/strong>\uff1a\u8003\u8651\u4f7f\u7528\u751f\u6210\u5668\u6216<code>numpy<\/code>\u5e93\u3002<\/li>\n<li><strong>\u5d4c\u5957\u5217\u8868<\/strong>\uff1a\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6216\u7ed3\u5408\u751f\u6210\u5668\u7684\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u6027\u80fd\u76d1\u6d4b<\/strong>\uff1a\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u6216\u5176\u4ed6\u6027\u80fd\u76d1\u6d4b\u5de5\u5177\uff0c\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>Example: Comparing for loop and map function<\/strong><\/h2>\n<p>setup_code = &#39;&#39;&#39;<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>for_loop_code = &#39;&#39;&#39;<\/p>\n<p>for item in my_list:<\/p>\n<p>    print(item)<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>map_function_code = &#39;&#39;&#39;<\/p>\n<p>list(map(print, my_list))<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>print(&#39;For loop:&#39;, timeit.timeit(setup=setup_code, stmt=for_loop_code, number=10000))<\/p>\n<p>print(&#39;Map function:&#39;, timeit.timeit(setup=setup_code, stmt=map_function_code, number=10000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u6027\u80fd\u76d1\u6d4b\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u5b9e\u9645\u9700\u6c42\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u5355\u72ec\u8f93\u51fa\u53ef\u80fd\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u5206\u6790<\/strong>\uff1a\u5904\u7406\u548c\u8f93\u51fa\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5206\u6790\u3002<\/li>\n<li><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u9010\u4e00\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u76d1\u63a7\u3002<\/li>\n<li><strong>\u7528\u6237\u4ea4\u4e92<\/strong>\uff1a\u5c06\u5217\u8868\u4e2d\u7684\u9009\u9879\u9010\u4e00\u8f93\u51fa\uff0c\u4f9b\u7528\u6237\u9009\u62e9\u3002<\/li>\n<li><strong>\u6587\u4ef6\u5904\u7406<\/strong>\uff1a\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u8f93\u51fa\uff0c\u6bcf\u884c\u4f5c\u4e3a\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<\/ol>\n<p><p><strong>\u6839\u636e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u5355\u72ec\u8f93\u51fa\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u9009\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u7efc\u5408\u8003\u8651\u4ee3\u7801\u7b80\u6d01\u6027\u3001\u6267\u884c\u6548\u7387\u548c\u5177\u4f53\u9700\u6c42\u3002\u901a\u8fc7\u6027\u80fd\u6bd4\u8f83\u548c\u5b9e\u9645\u5e94\u7528\uff0c\u53ef\u4ee5\u627e\u5230\u6700\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u6700\u4f73\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p><strong>\u5efa\u8bae\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u591a\u5c1d\u8bd5\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u4f18\u5316\u548c\u8c03\u6574\uff0c\u4e0d\u65ad\u63d0\u5347\u4ee3\u7801\u8d28\u91cf\u548c\u8fd0\u884c\u6548\u7387\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u53c2\u8003\u6587\u732e<\/h3>\n<\/p>\n<ol>\n<li>Python\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/docs.python.org\/3\/\">https:\/\/docs.python.org\/3\/<\/a><\/li>\n<li>NumPy\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/numpy.org\/doc\/\">https:\/\/numpy.org\/doc\/<\/a><\/li>\n<li>Python\u6027\u80fd\u4f18\u5316\u6307\u5357\uff1a<a href=\"https:\/\/realpython.com\/python-performance\/\">https:\/\/realpython.com\/python-performance\/<\/a><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u7684\u5217\u8868\u5904\u7406\u65b9\u6cd5\uff0c\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u904d\u5386\u5217\u8868\u5e76\u8f93\u51fa\u6bcf\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]\nfor item in my_list:\n    print(item)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u4f1a\u9010\u4e2a\u6253\u5370\u5217\u8868\u4e2d\u7684\u6570\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06Python\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u8f93\u51fa\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528join\u65b9\u6cd5\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cjoin\u65b9\u6cd5\u53ea\u80fd\u7528\u4e8e\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u524d\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3]\noutput = &#39;, &#39;.join(map(str, my_list))\nprint(output)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u201c1, 2, 3\u201d\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5217\u8868\u7684\u5143\u7d20\u8f93\u51fa\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u7edd\u5bf9\u53ef\u4ee5\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u5c06\u5217\u8868\u5143\u7d20\u5199\u5165\u6587\u4ef6\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]\nwith open(&#39;output.txt&#39;, &#39;w&#39;) as f:\n    for item in my_list:\n        f.write(f&quot;{item}\\n&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06\u6bcf\u4e2a\u5143\u7d20\u5355\u72ec\u5199\u5165\u540d\u4e3aoutput.txt\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u5355\u72ec\u8f93\u51fa\u3002 \u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u5217\u8868\u89e3\u6790\u3001\u4ee5\u53ca\u5185\u7f6e\u7684\u51fd\u6570\u548c [&hellip;]","protected":false},"author":3,"featured_media":1129596,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129587"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1129587"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129587\/revisions"}],"predecessor-version":[{"id":1129598,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1129587\/revisions\/1129598"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1129596"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1129587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1129587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1129587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}