{"id":176537,"date":"2024-05-08T19:12:49","date_gmt":"2024-05-08T11:12:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176537.html"},"modified":"2024-05-08T19:12:57","modified_gmt":"2024-05-08T11:12:57","slug":"python%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%9e%82%e7%9b%b4%e8%be%93%e5%87%ba%e8%a6%81%e6%80%8e%e4%b9%88%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/176537.html","title":{"rendered":"python\u5b57\u7b26\u4e32\u5782\u76f4\u8f93\u51fa\u8981\u600e\u4e48\u64cd\u4f5c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060205\/e69b0b89-2f04-4a55-9fdb-191aacaa17e1.webp\" alt=\"python\u5b57\u7b26\u4e32\u5782\u76f4\u8f93\u51fa\u8981\u600e\u4e48\u64cd\u4f5c\" \/><\/p>\n<p><p>Python\u5b57\u7b26\u4e32\u7684\u5782\u76f4\u8f93\u51fa\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528map()\u51fd\u6570\u3001\u4ee5\u53ca\u5229\u7528zip()\u51fd\u6570\u548c\u89e3\u5305\u6280\u5de7\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52bf\uff0c\u800c\u5faa\u73af\u65b9\u6cd5\u5219\u662f\u6700\u76f4\u89c2\u3001\u5bb9\u6613\u7406\u89e3\u7684\u4e00\u79cd\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5faa\u73af\u65b9\u6cd5\uff1a\u8981\u5782\u76f4\u8f93\u51fa\u5b57\u7b26\u4e32\uff0c\u9996\u5148\u9700\u8981\u641e\u6e05\u695a\u6211\u4eec\u7684\u76ee\u6807\u662f\u6309\u5b57\u7b26\u4e00\u884c\u884c\u8f93\u51fa\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5bf9\u4e8e\u7ed9\u5b9a\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u9700\u8981\u8bbf\u95ee\u5b57\u7b26\u4e32\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u6253\u5370\u5230\u65b0\u7684\u884c\u4e0a\u3002\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u53ef\u4ee5\u50cf\u5217\u8868\u4e00\u6837\u8fed\u4ee3\uff0c\u8fd9\u4f7f\u5f97\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5b57\u7b26\u5e76\u6253\u5370\u5b83\u53d8\u5f97\u5341\u5206\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u5782\u76f4\u8f93\u51fa\u662f\u6700\u57fa\u7840\u4e14\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u9996\u5148\uff0c\u9700\u8981\u901a\u8fc7for\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\uff0c\u6253\u5370\u5f53\u524d\u5b57\u7b26\uff0c\u5e76\u901a\u8fc7print\u51fd\u6570\u7684end\u53c2\u6570\u6307\u5b9a\u6bcf\u4e2a\u5b57\u7b26\u540e\u4e0d\u8ddf\u968f\u6362\u884c\u7b26\uff0c\u800c\u662f\u65b0\u7684\u4e00\u884c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4ee3\u7801\u7b80\u6d01\u6613\u61c2\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\u5c24\u4e3a\u53cb\u597d\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32&#039;stack&#039;\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u5782\u76f4\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &#039;stack&#039;<\/p>\n<p>for char in s:<\/p>\n<p>    print(char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5c06\u5b57\u7b26\u4e32&#039;stack&#039;\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u9010\u884c\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528map()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>map()\u51fd\u6570\u662fPython\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u4e3b\u8981\u7528\u4e8e\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u6307\u5b9a\u7684\u51fd\u6570\uff0c\u5e76\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u7ed3\u679c\u3002\u7ed3\u5408lambda\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u5782\u76f4\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528map()\u51fd\u6570\u5bf9\u6bcf\u4e2a\u5b57\u7b26\u5e94\u7528print\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u66f4\u52a0Pythonic\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5bf9\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\u7684\u8ffd\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u53ef\u4ee5\u8fd9\u6837\u7f16\u5199\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &#039;hello&#039;<\/p>\n<p>list(map(lambda x: print(x), s))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u5229\u7528zip()\u51fd\u6570\u548c\u89e3\u5305\u6280\u5de7<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6709\u591a\u4e2a\u5b57\u7b26\u4e32\u9700\u8981\u6309\u5217\u5bf9\u9f50\u5782\u76f4\u8f93\u51fa\u7684\u60c5\u51b5\uff0czip()\u51fd\u6570\u914d\u5408\u89e3\u5305\u6280\u5de7\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002zip()\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6253\u5305\u4e3a\u4e00\u4e2a\u5143\u7ec4\u7684\u5217\u8868\uff0c\u5982\u679c\u7ed3\u5408*\u8fd0\u7b97\u7b26\u8fdb\u884c\u89e3\u5305\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u5782\u76f4\u5bf9\u9f50\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u8003\u8651\u5230\u5b57\u7b26\u4e32\u957f\u5ea6\u53ef\u80fd\u4e0d\u540c\uff0c\u53ef\u4ee5\u4f7f\u7528itertools\u6a21\u5757\u4e2d\u7684zip_longest()\u51fd\u6570\u6765\u786e\u4fdd\u5b57\u7b26\u4e32\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from itertools import zip_longest<\/p>\n<p>s1 = &#039;Hello&#039;<\/p>\n<p>s2 = &#039;World!&#039;<\/p>\n<p>s3 = &#039;Python&#039;<\/p>\n<h2><strong>\u786e\u4fdd\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u80fd\u5bf9\u9f50\uff0c\u672a\u5bf9\u9f50\u7684\u4f4d\u7f6e\u7528\u7a7a\u683c\u586b\u5145<\/strong><\/h2>\n<p>for chars in zip_longest(s1, s2, s3, fillvalue=&#039; &#039;):<\/p>\n<p>    print(&#039; &#039;.join(chars))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u5c24\u5176\u9002\u7528\u4e8e\u9700\u8981\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u6309\u5217\u5782\u76f4\u5bf9\u9f50\u8f93\u51fa\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u8fdb\u9636\u65b9\u6cd5\uff1a\u7ed3\u5408\u6570\u636e\u5904\u7406\u5e93<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u9700\u6c42\uff0c\u7ed3\u5408\u5982Pandas\u7b49\u6570\u636e\u5904\u7406\u5e93\u8fdb\u884c\u5b57\u7b26\u4e32\u7684\u5782\u76f4\u8f93\u51fa\u53ef\u4ee5\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u6548\u7387\u63d0\u5347\u548c\u7075\u6d3b\u6027\u3002\u901a\u8fc7\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aPandas\u7684Series\u6216DataFrame\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u5e93\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u6765\u5b9e\u73b0\u9ad8\u7ea7\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u8f93\u51fa\u683c\u5f0f\u5b9a\u5236\u3002\u6b64\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u3001\u79d1\u5b66\u8ba1\u7b97\u7b49\u9886\u57df\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528Pandas\u5e93\u5782\u76f4\u8f93\u51fa\u5b57\u7b26\u4e32\u7684\u7b80\u5355\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>s = &#039;amazing&#039;<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3aSeries\u5bf9\u8c61\uff0c\u6bcf\u4e2a\u5b57\u7b26\u4e3a\u4e00\u884c<\/strong><\/h2>\n<p>s_series = pd.Series(list(s))<\/p>\n<h2><strong>\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>print(s_series)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u5782\u76f4\u8f93\u51fa\u7684\u65b9\u6cd5\uff0c\u4ece\u57fa\u7840\u7684\u5faa\u73af\u5230\u9ad8\u7ea7\u7684\u6570\u636e\u5904\u7406\u5e93\u7ed3\u5408\u3002\u6839\u636e\u5177\u4f53\u9700\u8981\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u80fd\u591f\u6709\u6548\u5730\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u5782\u76f4\u8f93\u51fa\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u98981\uff1a<\/strong> \u5982\u4f55\u7528Python\u5c06\u5b57\u7b26\u4e32\u5782\u76f4\u8f93\u51fa\uff1f<\/p>\n<p>\u56de\u7b54\uff1a\u8981\u5c06\u5b57\u7b26\u4e32\u5782\u76f4\u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684for\u5faa\u73af\u548c\u5b57\u7b26\u4e32\u5207\u7247\u64cd\u4f5c\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\u5206\u5272\u5f00\u6765\uff0c\u7136\u540e\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u8fd9\u4e9b\u5b57\u7b26\uff0c\u9010\u884c\u8f93\u51fa\u3002<\/p>\n<pre><code class=\"language-python\">string = &quot;Hello World&quot;\nfor char in string:\n    print(char)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u6253\u5370\u51fa\u6bcf\u4e2a\u5b57\u7b26\u5360\u636e\u4e00\u884c\u7684\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>H\ne\nl\nl\no\n\nW\no\nr\nl\nd\n<\/code><\/pre>\n<p><strong>\u95ee\u98982\uff1a<\/strong> \u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u5782\u76f4\u8f93\u51fa\uff1f<\/p>\n<p>\u56de\u7b54\uff1a\u662f\u7684\uff0c\u9664\u4e86\u4f7f\u7528for\u5faa\u73af\u548c\u5b57\u7b26\u4e32\u5207\u7247\u7684\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u7b26\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528join\u51fd\u6570\u8fde\u63a5\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u5e76\u7528\u6362\u884c\u7b26\u5206\u9694\uff0c\u6700\u540e\u6253\u5370\u51fa\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"language-python\">string = &quot;Hello World&quot;\nvertical_string = &quot;\\n&quot;.join([char for char in string])\nprint(vertical_string)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u4ea7\u751f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684\u5782\u76f4\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<p><strong>\u95ee\u98983\uff1a<\/strong> \u6211\u60f3\u628a\u5782\u76f4\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u600e\u4e48\u505a\uff1f<\/p>\n<p>\u56de\u7b54\uff1a\u8981\u5c06\u5782\u76f4\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u6587\u4ef6\u64cd\u4f5c\u3002\u9996\u5148\uff0c\u5c06\u5782\u76f4\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u4fdd\u5b58\u5230\u4e00\u4e2a\u53d8\u91cf\u4e2d\uff0c\u7136\u540e\u4f7f\u7528open\u51fd\u6570\u4ee5\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528write\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u4e2d\uff0c\u6700\u540e\u4f7f\u7528close\u51fd\u6570\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"language-python\">string = &quot;Hello World&quot;\nvertical_string = &quot;\\n&quot;.join([char for char in string])\nfile = open(&quot;output.txt&quot;, &quot;w&quot;)\nfile.write(vertical_string)\nfile.close()\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aoutput.txt\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5782\u76f4\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u5199\u5165\u5230\u8be5\u6587\u4ef6\u4e2d\u3002\u4f60\u53ef\u4ee5\u5728\u6587\u4ef6\u4e2d\u67e5\u770b\u5782\u76f4\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b57\u7b26\u4e32\u7684\u5782\u76f4\u8f93\u51fa\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528map()\u51fd\u6570\u3001\u4ee5\u53ca\u5229\u7528zip() [&hellip;]","protected":false},"author":3,"featured_media":176548,"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\/176537"}],"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=176537"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/176537\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/176548"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=176537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=176537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=176537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}