{"id":305953,"date":"2024-05-20T19:58:28","date_gmt":"2024-05-20T11:58:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/305953.html"},"modified":"2024-05-20T19:58:45","modified_gmt":"2024-05-20T11:58:45","slug":"%e7%bc%96%e7%a8%8b%e4%b8%ad%e7%9a%84-a123456-78-a152%e6%98%af%e4%bb%80%e4%b9%88%e6%84%8f%e6%80%9d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/305953.html","title":{"rendered":"\u7f16\u7a0b\u4e2d\u7684 a=&#8217;123456 78&#8242;  a[1:5:2]\u662f\u4ec0\u4e48\u610f\u601d"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26222248\/a49a27bd-101a-451b-a363-19f5a5b535a7.webp\" alt=\"\u7f16\u7a0b\u4e2d\u7684 a='123456 78'  a[1:5:2]\u662f\u4ec0\u4e48\u610f\u601d\" \/><\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u8868\u8fbe\u5f0f<code>a=&#039;123456 78&#039;<\/code>\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf<code>a<\/code>\uff0c\u800c<code>a[1:5:2]<\/code>\u5219\u662f\u5bf9\u8fd9\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u5207\u7247\uff08slicing\uff09\u64cd\u4f5c\u7684\u4e00\u4e2a\u4f8b\u5b50\u3002\u5177\u4f53\u6765\u8bf4\uff0c<code>a[1:5:2]<\/code>\u7684\u610f\u601d\u662f\u4ece\u5b57\u7b26\u4e32<code>a<\/code>\u4e2d\u9009\u53d6\u7d22\u5f15\u53f7\u4ece1\u5f00\u59cb\u52305\uff08\u4e0d\u5305\u542b5\uff09\u7684\u5143\u7d20\uff0c\u4e14\u6b65\u957f\u4e3a2\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f<code>&#039;24&#039;<\/code>\u3002\u6b64\u64cd\u4f5c\u80fd\u591f\u7075\u6d3b\u63d0\u53d6\u5b57\u7b26\u4e32\u7684\u90e8\u5206\u5185\u5bb9\uff0c\u662fPython\u4e2d\u975e\u5e38\u5b9e\u7528\u7684\u7279\u6027\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u5207\u7247\u7684\u4e00\u822c\u5f62\u5f0f\u4e3a<code>[\u5f00\u59cb\u7d22\u5f15:\u7ed3\u675f\u7d22\u5f15:\u6b65\u957f]<\/code>\u3002\u5176\u4e2d\uff0c\u5f00\u59cb\u7d22\u5f15\u6307\u7684\u662f\u5207\u7247\u5f00\u59cb\u7684\u4f4d\u7f6e\uff1b\u7ed3\u675f\u7d22\u5f15\u662f\u5207\u7247\u7ed3\u675f\u7684\u4f4d\u7f6e\uff0c\u5b9e\u9645\u5f97\u5230\u7684\u5207\u7247\u4e0d\u5305\u62ec\u8fd9\u4e2a\u4f4d\u7f6e\u7684\u5b57\u7b26\uff1b\u6b65\u957f\u662f\u6307\u5728\u5207\u7247\u65f6\u6bcf\u6b21\u8df3\u8fc7\u7684\u5b57\u7b26\u6570\u91cf\uff0c\u9ed8\u8ba4\u4e3a1\u3002\u5728<code>a[1:5:2]<\/code>\u4e2d\uff0c\u5f00\u59cb\u7d22\u5f151\u4ee3\u8868\u4ece<code>&#039;2&#039;<\/code>\u5f00\u59cb\uff0c\u7ed3\u675f\u7d22\u5f155\u4ee3\u8868\u5230<code>&#039;6&#039;<\/code>\u7ed3\u675f\u4f46\u4e0d\u5305\u542b<code>&#039;6&#039;<\/code>\uff0c\u6b65\u957f2\u8868\u793a\u4ece<code>&#039;2&#039;<\/code>\u5f00\u59cb\u6bcf\u6b21\u8df3\u8fc7\u4e00\u4e2a\u5b57\u7b26\u8fdb\u884c\u9009\u53d6\u3002\u6b64\u64cd\u4f5c\u5bf9\u4e8e\u5904\u7406\u548c\u5206\u6790\u5b57\u7b26\u4e32\u6570\u636e\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u4e32\u5207\u7247\u7684\u57fa\u672c\u7406\u89e3<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u662fPython\u4e2d\u7528\u6765\u5b58\u50a8\u6587\u672c\u4fe1\u606f\u7684\u6570\u636e\u7c7b\u578b\u3002\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u63d0\u53d6\u6216\u8005\u5206\u6790\u5b57\u7b26\u4e32\u7684\u67d0\u4e00\u90e8\u5206\uff0c\u8fd9\u5c31\u6d89\u53ca\u5230\u4e86\u5b57\u7b26\u4e32\u5207\u7247\u7684\u64cd\u4f5c\u3002\u5207\u7247\u64cd\u4f5c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u4ece\u5b57\u7b26\u4e32\u4e2d\u83b7\u53d6\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u7406\u89e3\u5207\u7247\u64cd\u4f5c\u7684\u57fa\u672c\u8bed\u6cd5\u662f\u5b66\u4f1a\u4f7f\u7528\u5b83\u7684\u5173\u952e\u3002Python\u4e2d\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u5728\u6267\u884c<code>a[1:5:2]<\/code>\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u4ece\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u5f00\u59cb\uff0c\u5230\u7b2c\u4e94\u4e2a\u5b57\u7b26\u7ed3\u675f\uff08\u4f46\u4e0d\u5305\u542b\u7b2c\u4e94\u4e2a\u5b57\u7b26\uff09\uff0c\u5e76\u4e14\u6bcf\u6b21\u63d0\u53d6\u65f6\u8df3\u8fc7\u4e00\u4e2a\u5b57\u7b26\u3002\u8fd9\u79cd\u7075\u6d3b\u7684\u64cd\u4f5c\u4f7f\u5f97\u5b57\u7b26\u4e32\u6570\u636e\u7684\u5904\u7406\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b57\u7b26\u4e32\u5207\u7247\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u5207\u7247\u64cd\u4f5c\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u66f4\u591a\u4e0e\u5207\u7247\u76f8\u5173\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u6bd4\u5982\u8d1f\u6570\u7d22\u5f15\u548c\u7701\u7565\u5f00\u59cb\u6216\u7ed3\u675f\u7d22\u5f15\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u8d1f\u6570\u7d22\u5f15\u53ef\u4ee5\u4ece\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u5f00\u59cb\u8ba1\u7b97\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c<code>a[-1]<\/code>\u4f1a\u8fd4\u56de\u5b57\u7b26\u4e32<code>a<\/code>\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u3002\u8fd9\u5728\u5904\u7406\u4ee5\u7279\u5b9a\u6a21\u5f0f\u7ed3\u675f\u7684\u5b57\u7b26\u4e32\u65f6\u7279\u522b\u6709\u7528\u3002\u800c\u5728\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u65f6\u7701\u7565\u5f00\u59cb\u6216\u7ed3\u675f\u7d22\u5f15\u53ef\u4ee5\u65b9\u4fbf\u5730\u8868\u793a\u4ece\u5f00\u59cb\u6216\u5207\u7247\u5230\u7ed3\u5c3e\u3002\u4f8b\u5982\uff0c<code>a[:3]<\/code>\u4ee3\u8868\u4ece\u5b57\u7b26\u4e32\u5f00\u59cb\u5230\u7d22\u5f153\uff08\u4e0d\u5305\u542b3\uff09\u7684\u5b50\u4e32\uff0c<code>a[3:]<\/code>\u5219\u8868\u793a\u4ece\u7d22\u5f153\u5230\u5b57\u7b26\u4e32\u7ed3\u675f\u7684\u5b50\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5207\u7247\u64cd\u4f5c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u91cd\u8981\u6027<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u3002\u65e0\u8bba\u662f\u5728\u6570\u636e\u5904\u7406\u3001\u6587\u4ef6\u64cd\u4f5c\u8fd8\u662f\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u5408\u7406\u5229\u7528\u5207\u7247\u64cd\u4f5c\u90fd\u80fd\u6781\u5927\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u662f\u5728\u6587\u672c\u5206\u6790\u4e2d\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u5904\u7406\u65e5\u5fd7\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\u5feb\u901f\u63d0\u53d6\u6bcf\u4e00\u884c\u4e2d\u7684\u65e5\u671f\u3001\u65f6\u95f4\u6216\u662f\u7279\u5b9a\u6807\u8bc6\u7b26\u7b49\u4fe1\u606f\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u6216\u7edf\u8ba1\u5de5\u4f5c\u3002\u540c\u6837\uff0c\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\u6765\u9a8c\u8bc1\u5176\u683c\u5f0f\u662f\u5426\u7b26\u5408\u9884\u671f\uff0c\u6216\u662f\u4ece\u4e2d\u63d0\u53d6\u5173\u952e\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5207\u7247\u4e0e\u6570\u636e\u5904\u7406\u7684\u5173\u7cfb<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\uff0c\u5207\u7247\u64cd\u4f5c\u4e0d\u4ec5\u5c40\u9650\u4e8e\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u5728Python\u7684\u6570\u636e\u5206\u6790\u5e93\u5982NumPy\u548cPandas\u4e2d\uff0c\u5207\u7247\u7684\u6982\u5ff5\u88ab\u6269\u5c55\u5230\u4e86\u6570\u7ec4\u548c\u6570\u636e\u6846\uff08DataFrame\uff09\u7684\u5904\u7406\u4e0a\u3002\u8fd9\u4f7f\u5f97\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u7b5b\u9009\u3001\u6c47\u603b\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728NumPy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u5b57\u7b26\u4e32\u5207\u7247\u7684\u8bed\u6cd5\u6765\u64cd\u4f5c\u6570\u7ec4\uff0c\u4ece\u800c\u9ad8\u6548\u5730\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\uff1b\u800c\u5728Pandas\u4e2d\uff0c\u5229\u7528\u5207\u7247\u64cd\u4f5c\u53ef\u4ee5\u4fbf\u6377\u5730\u5bf9\u6570\u636e\u96c6\u8fdb\u884c\u7b5b\u9009\u3001\u6392\u5e8f\u548c\u805a\u5408\uff0c\u6781\u5927\u5730\u7b80\u5316\u4e86\u6570\u636e\u5904\u7406\u7684\u590d\u6742\u6027\u3002\u638c\u63e1\u5207\u7247\u64cd\u4f5c\uff0c\u80fd\u591f\u4e3a\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u6253\u4e0b\u575a\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662fPython\u4e2d\u7684\u5207\u7247\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>\u5207\u7247\u64cd\u4f5c\u662f\u6307\u901a\u8fc7\u7d22\u5f15\u5bf9\u5e8f\u5217\uff08\u5982\u5b57\u7b26\u4e32\u3001\u5217\u8868\u7b49\uff09\u8fdb\u884c\u90e8\u5206\u622a\u53d6\u7684\u64cd\u4f5c\u3002\u5728\u8fd9\u79cd\u64cd\u4f5c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u8d77\u70b9\u3001\u7ec8\u70b9\u548c\u6b65\u957f\u6765\u83b7\u53d6\u5e8f\u5217\u7684\u5b50\u5e8f\u5217\u3002\u5728\u4f60\u63d0\u5230\u7684\u4f8b\u5b50\u4e2d\uff0ca[1:5:2]\u8868\u793a\u4ece\u7d22\u5f15\u4e3a1\u7684\u4f4d\u7f6e\u5f00\u59cb\uff0c\u5230\u7d22\u5f15\u4e3a5\u7684\u4f4d\u7f6e\u7ed3\u675f\uff0c\u4ee5\u6b65\u957f2\u622a\u53d6a\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>2. \u5728\u5207\u7247\u64cd\u4f5c\u4e2d\uff0ca[1:5:2]\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u5bf9\u4e8ea=&#039;123456 78&#039;\u8fd9\u4e2a\u5b57\u7b26\u4e32\uff0ca[1:5:2]\u5c06\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u4ece\u7d22\u5f151\u5230\u7d22\u5f155\u7684\u5b57\u7b26\uff0c\u4e14\u6b65\u957f\u4e3a2\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u7ed3\u679c\u5c06\u5305\u62ec\u7d22\u5f15\u4e3a1\uff08\u5373\u5b57\u7b26&#039;2&#039;\uff09\u548c3\uff08\u5373\u5b57\u7b26&#039;4&#039;\uff09\u7684\u5b57\u7b26\uff0c\u4e0d\u5305\u62ec\u7d22\u5f15\u4e3a5\u7684\u5b57\u7b26\uff08\u5373\u5b57\u7b26&#039;5&#039;\uff09\u3002\u56e0\u6b64\uff0ca[1:5:2]\u8fd4\u56de\u7684\u7ed3\u679c\u662f&#039;24&#039;\u3002<\/p>\n<p><strong>3. \u6211\u8be5\u5982\u4f55\u7406\u89e3\u5207\u7247\u64cd\u4f5c\u4e2d\u7684\u8d77\u70b9\u3001\u7ec8\u70b9\u548c\u6b65\u957f\u53c2\u6570\uff1f<\/strong><\/p>\n<p>\u5728\u5207\u7247\u64cd\u4f5c\u4e2d\uff0c\u8d77\u70b9\u6307\u5b9a\u4e86\u65b0\u5e8f\u5217\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u7ec8\u70b9\u6307\u5b9a\u4e86\u65b0\u5e8f\u5217\u7684\u7ed3\u675f\u4f4d\u7f6e\uff08\u4e0d\u5305\u542b\u8be5\u4f4d\u7f6e\u7684\u5b57\u7b26\uff09\uff0c\u800c\u6b65\u957f\u5219\u51b3\u5b9a\u4e86\u622a\u53d6\u7684\u95f4\u9694\u3002\u4f8b\u5982\uff0ca[1:5:2]\u4e2d\u7684\u8d77\u70b9\u4e3a\u7d22\u5f151\uff0c\u5373\u5b57\u7b26&#039;2&#039;\uff0c\u7ec8\u70b9\u4e3a\u7d22\u5f155\uff0c\u5373\u5b57\u7b26&#039;6&#039;\uff0c\u6b65\u957f\u4e3a2\uff0c\u8868\u793a\u6bcf\u9694\u4e00\u4e2a\u5b57\u7b26\u622a\u53d6\u4e00\u6b21\u3002\u8fd9\u610f\u5473\u7740\u8fd4\u56de\u7684\u65b0\u5e8f\u5217\u5c06\u5305\u542b\u5b57\u7b26&#039;2&#039;\u548c&#039;4&#039;\uff0c\u800c\u5b57\u7b26&#039;6&#039;\u5219\u4e0d\u4f1a\u88ab\u5305\u62ec\u5728\u5185\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u8868\u8fbe\u5f0fa=&#039;123456 78&#039;\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cfa\uff0c\u800ca[1 [&hellip;]","protected":false},"author":3,"featured_media":305981,"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\/305953"}],"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=305953"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/305953\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/305981"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=305953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=305953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=305953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}