{"id":174443,"date":"2024-05-08T18:31:35","date_gmt":"2024-05-08T10:31:35","guid":{"rendered":""},"modified":"2024-05-08T18:31:49","modified_gmt":"2024-05-08T10:31:49","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%86%e5%89%b2%e6%88%90%e5%8d%95%e4%b8%aa%e5%ad%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/174443.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u4e2a\u5b57\u7b26"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27052052\/44bcb264-ab66-4d4b-8e8c-f5b27113ef00.webp\" alt=\"python\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u4e2a\u5b57\u7b26\" \/><\/p>\n<p><p><strong>Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u4e2a\u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u904d\u5386\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8005\u901a\u8fc7<code>list()<\/code>\u51fd\u6570\u5b9e\u73b0\u3002<\/strong> \u5176\u4e2d\uff0c<code>list()<\/code>\u51fd\u6570\u662f\u6700\u76f4\u63a5\u3001\u6700\u7b80\u4fbf\u7684\u65b9\u6cd5\uff1a\u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u88ab\u4f20\u9012\u7ed9<code>list()<\/code>\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8be5\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5b57\u7b26\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>list(&#039;hello&#039;)<\/code>\u4f1a\u5f97\u5230<code>[&#039;h&#039;, &#039;e&#039;, &#039;l&#039;, &#039;l&#039;, &#039;o&#039;]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>list()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>list()<\/code>\u51fd\u6570\u662f\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5355\u4e2a\u5b57\u7b26\u5217\u8868\u7684\u6700\u76f4\u63a5\u65b9\u6cd5\u3002\u53ea\u9700\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>list()<\/code>\u51fd\u6570\uff0c\u5b83\u5c31\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5355\u4e2a\u5b57\u7b26\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_example = &quot;hello&quot;<\/p>\n<p>characters = list(string_example)<\/p>\n<p>print(characters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[&#039;h&#039;, &#039;e&#039;, &#039;l&#039;, &#039;l&#039;, &#039;o&#039;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u904d\u5386\u5b57\u7b26\u4e32\u5e76\u5b58\u50a8\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>list()<\/code>\u51fd\u6570\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u6765\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_example = &quot;world&quot;<\/p>\n<p>characters = []<\/p>\n<p>for char in string_example:<\/p>\n<p>    characters.append(char)<\/p>\n<p>print(characters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[&#039;w&#039;, &#039;o&#039;, &#039;r&#039;, &#039;l&#039;, &#039;d&#039;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u3001\u9ad8\u6548\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_example = &quot;python&quot;<\/p>\n<p>characters = [char for char in string_example]<\/p>\n<p>print(characters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[&#039;p&#039;, &#039;y&#039;, &#039;t&#039;, &#039;h&#039;, &#039;o&#039;, &#039;n&#039;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>split()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u867d\u7136<code>split()<\/code>\u65b9\u6cd5\u5e38\u7528\u4e8e\u5206\u5272\u5b57\u7b26\u4e32\u4e2d\u7684\u5355\u8bcd\u6216\u53e5\u5b50\uff0c\u4f46\u5b83\u4e0d\u9002\u5408\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u5b57\u7b26\uff0c\u56e0\u4e3a<code>split()<\/code>\u65b9\u6cd5\u9700\u8981\u4e00\u4e2a\u5206\u9694\u7b26\u3002\u5982\u679c\u5f3a\u884c\u4f7f\u7528\uff0c\u5982<code>split(&#039;&#039;)<\/code>\uff0c\u4f1a\u5bfc\u81f4<code>ValueError<\/code>\u3002\u56e0\u6b64\uff0c\u5728\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u5355\u4e2a\u5b57\u7b26\u65f6\uff0c\u63a8\u8350\u4f7f\u7528\u524d\u6587\u63d0\u5230\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>map()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>map()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u6765\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u4e2a\u5b57\u7b26\uff0c\u901a\u8fc7\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff08\u8fd9\u91cc\u662f<code>str<\/code>\uff09\uff0c\u5c06\u5176\u6620\u5c04\u4e3a\u5355\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_example = &quot;split&quot;<\/p>\n<p>characters = list(map(str, string_example))<\/p>\n<p>print(characters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[&#039;s&#039;, &#039;p&#039;, &#039;l&#039;, &#039;i&#039;, &#039;t&#039;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8003\u8651Unicode\u5b57\u7b26\u548c\u7f16\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u7279\u522b\u662f\u5305\u542bUnicode\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u9700\u8981\u8003\u8651\u6bcf\u4e2a\u5b57\u7b26\u7684\u7f16\u7801\u65b9\u5f0f\u3002\u5728Python\u4e2d\u5b57\u7b26\u4e32\u9ed8\u8ba4\u91c7\u7528Unicode\u7f16\u7801\u3002\u5982\u679c\u8981\u4fdd\u7559\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u975eASCII\u5b57\u7b26\u7684\u539f\u6837\u5f62\u5f0f\uff0c\u5e94\u5f53\u4f7f\u7528\u524d\u9762\u63d0\u5230\u7684\u65b9\u6cd5\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u7f16\u7801\u5f62\u5f0f\u5dee\u5f02\u8f83\u5927\u7684\u5b57\u7b26\uff08\u5982\u7ec4\u5408\u5b57\u7b26\u6216\u662f\u8868\u60c5\u7b49\uff09\uff0c\u5219\u9700\u8981\u5229\u7528\u5176\u4ed6\u5e93\u5982<code>unicodedata<\/code>\u8fdb\u884c\u66f4\u7ec6\u81f4\u7684\u5904\u7406\u4ee5\u786e\u4fdd\u6b63\u786e\u5730\u5206\u79bb\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p><strong>\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u4e2a\u5b57\u7b26\u5728Python\u4e2d\u975e\u5e38\u7b80\u5355\uff0c\u6700\u5e38\u7528\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u3002<\/strong> \u5176\u4ed6\u65b9\u6cd5\u5982\u904d\u5386\u5b57\u7b26\u4e32\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u540c\u6837\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f7f\u7528<code>split()<\/code>\u65b9\u6cd5\u5c1d\u8bd5\u5206\u5272\u5b57\u7b26\u4e32\u5230\u5355\u4e2a\u5b57\u7b26\u4f1a\u5f15\u53d1\u9519\u8bef\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002\u800c\u5728\u5904\u7406\u5305\u542b\u7279\u6b8aUnicode\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u5219\u53ef\u80fd\u9700\u8981\u66f4\u590d\u6742\u7684\u5904\u7406\u65b9\u6cd5\u6765\u786e\u4fdd\u6bcf\u4e2a\u5b57\u7b26\u80fd\u591f\u6b63\u786e\u5206\u79bb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u5355\u4e2a\u5b57\u7b26\uff1f<\/strong><\/p>\n<ul>\n<li>\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684 <code>for<\/code> \u5faa\u73af\u6765\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5b57\u7b26\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-python\">string = &quot;Hello&quot;\ncharacters = [char for char in string]\nprint(characters)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c06\u8fd4\u56de <code>[&#039;H&#039;, &#039;e&#039;, &#039;l&#039;, &#039;l&#039;, &#039;o&#039;]<\/code>\u3002<\/p>\n<ul>\n<li>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 <code>list()<\/code> \u51fd\u6570\u3002\u5b83\u5c06\u5b57\u7b26\u4e32\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u5c06\u5176\u5206\u5272\u6210\u5b57\u7b26\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-python\">string = &quot;Hello&quot;\ncharacters = list(string)\nprint(characters)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u76f8\u540c\u7684\u7ed3\u679c <code>[&#039;H&#039;, &#039;e&#039;, &#039;l&#039;, &#039;l&#039;, &#039;o&#039;]<\/code>\u3002<\/p>\n<ul>\n<li>\u5982\u679c\u4f60\u60f3\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u591a\u4e2a\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u5355\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u7a7a\u683c\u5c06\u4e00\u4e2a\u53e5\u5b50\u5206\u5272\u6210\u5355\u8bcd\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-python\">sentence = &quot;Hello, how are you?&quot;\nwords = sentence.split(&quot; &quot;)\nprint(words)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de <code>[&#039;Hello,&#039;, &#039;how&#039;, &#039;are&#039;, &#039;you?&#039;]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u5b57\u7b26\u4e32\u4ee5\u6307\u5b9a\u7684\u5bbd\u5ea6\u5206\u5272\uff1f<\/strong><\/p>\n<ul>\n<li>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u8fed\u4ee3\u5b57\u7b26\u4e32\uff0c\u5e76\u4ee5\u6307\u5b9a\u7684\u5bbd\u5ea6\u5206\u5272\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-python\">def split_string(string, width):\n    result = []\n    for i in range(0, len(string), width):  # \u4ece0\u5f00\u59cb\u4ee5\u6307\u5b9a\u7684\u5bbd\u5ea6\u8fed\u4ee3\u5b57\u7b26\u4e32\n        result.append(string[i:i+width])  # \u5c06\u5206\u5272\u540e\u7684\u5b50\u5b57\u7b26\u4e32\u6dfb\u52a0\u5230\u7ed3\u679c\u5217\u8868\u4e2d\n    return result\n\nstring = &quot;Hello, how are you?&quot;\nwidth = 5\n\nsplit_strings = split_string(string, width)\nprint(split_strings)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de <code>[&#039;Hello&#039;, &#039;, how&#039;, &#039; are&#039;, &#039; you?&#039;]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u8bcd\uff0c\u5e76\u53bb\u9664\u6807\u70b9\u7b26\u53f7\uff1f<\/strong><\/p>\n<ul>\n<li>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u8bcd\uff0c\u5e76\u4f7f\u7528 <code>string.punctuation<\/code> \u5f15\u5165\u7684 <code>punctuation<\/code> \u5e38\u91cf\u53bb\u9664\u6807\u70b9\u7b26\u53f7\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-python\">import string\n\ndef remove_punctuation(text):\n    # \u53bb\u9664\u6807\u70b9\u7b26\u53f7\n    text = text.translate(str.maketrans(&quot;&quot;, &quot;&quot;, string.punctuation))\n    return text\n\nsentence = &quot;Hello, how are you?&quot;\nwords = remove_punctuation(sentence).split()\nprint(words)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de <code>[&#039;Hello&#039;, &#039;how&#039;, &#039;are&#039;, &#039;you&#039;]<\/code>\u3002\u901a\u8fc7\u4f7f\u7528 <code>translate()<\/code> \u65b9\u6cd5\u548c <code>string.punctuation<\/code> \u5e38\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5220\u9664\u6240\u6709\u6807\u70b9\u7b26\u53f7\uff0c\u5e76\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u8bcd\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u5355\u4e2a\u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u904d\u5386\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8005\u901a\u8fc7list()\u51fd\u6570\u5b9e\u73b0\u3002 \u5176\u4e2d\uff0cl [&hellip;]","protected":false},"author":3,"featured_media":174471,"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\/174443"}],"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=174443"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/174443\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/174471"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=174443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=174443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=174443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}