{"id":988006,"date":"2024-12-27T07:58:31","date_gmt":"2024-12-26T23:58:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/988006.html"},"modified":"2024-12-27T07:58:33","modified_gmt":"2024-12-26T23:58:33","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/988006.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u63d0\u53d6\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063840\/aad08e84-ac0c-439a-8ce2-096a8b1f75cf.webp\" alt=\"python\u4e2d\u5982\u4f55\u63d0\u53d6\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u63d0\u53d6\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\u7d22\u5f15\u3001\u5207\u7247\u3001\u4f7f\u7528\u5185\u7f6e\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u7b49<\/strong>\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u63d0\u53d6\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u5143\u7d20\uff0c\u5177\u4f53\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7ed3\u6784\u7684\u7c7b\u578b\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u3001\u96c6\u5408\u7b49\u3002<strong>\u7d22\u5f15\u662f\u6700\u5e38\u7528\u7684\u76f4\u63a5\u63d0\u53d6\u65b9\u6cd5<\/strong>\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u8bbf\u95ee\u5217\u8868\u548c\u5143\u7ec4\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\u3002\u5207\u7247\u5219\u5141\u8bb8\u63d0\u53d6\u591a\u4e2a\u5143\u7d20\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u3001\u5217\u8868\u548c\u5143\u7ec4\u4e2d\u5e94\u7528\u3002\u9664\u6b64\u4e4b\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u65b9\u6cd5\u548c\u9ad8\u7ea7\u7279\u6027\uff0c\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u7b2c\u4e09\u65b9\u5e93\uff08\u5982NumPy\u548cPandas\uff09\uff0c\u7528\u4e8e\u66f4\u52a0\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u63d0\u53d6\u5143\u7d20\u662f\u6570\u636e\u64cd\u4f5c\u7684\u57fa\u7840\uff0c\u7406\u89e3\u5404\u79cd\u65b9\u6cd5\u53ca\u5176\u9002\u7528\u573a\u666f\u662f\u63d0\u5347\u7f16\u7a0b\u80fd\u529b\u7684\u5173\u952e\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u7d22\u5f15\u4e0e\u5207\u7247<\/strong><\/h2>\n<p><p>Python\u4e2d\u7684\u7d22\u5f15\u548c\u5207\u7247\u662f\u63d0\u53d6\u5143\u7d20\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5e8f\u5217\u6570\u636e\u7c7b\u578b\u5982\u5217\u8868\u3001\u5143\u7ec4\u548c\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>\u7d22\u5f15<\/h2>\n<\/p>\n<p><p>\u7d22\u5f15\u662f\u6307\u76f4\u63a5\u8bbf\u95ee\u6570\u636e\u7ed3\u6784\u4e2d\u67d0\u4e00\u4f4d\u7f6e\u7684\u5143\u7d20\u3002Python\u7684\u7d22\u5f15\u662f\u4ece0\u5f00\u59cb\u7684\uff0c\u5373\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f0\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5217\u8868\u7684\u7d22\u5f15<\/strong>\uff1a\u5047\u8bbe\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [10, 20, 30, 40, 50]<\/code>\uff0c\u8981\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_list[1]<\/code>\u3002<\/li>\n<li><strong>\u5143\u7ec4\u7684\u7d22\u5f15<\/strong>\uff1a\u7c7b\u4f3c\u4e8e\u5217\u8868\uff0c\u5143\u7ec4<code>my_tuple = (10, 20, 30)<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_tuple[0]<\/code>\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<li><strong>\u5b57\u7b26\u4e32\u7684\u7d22\u5f15<\/strong>\uff1a\u5bf9\u4e8e\u5b57\u7b26\u4e32<code>my_string = &quot;Hello&quot;<\/code>\uff0c<code>my_string[1]<\/code>\u5c06\u8fd4\u56de<code>&#39;e&#39;<\/code>\u3002<\/li>\n<\/ul>\n<p><h2>\u5207\u7247<\/h2>\n<\/p>\n<p><p>\u5207\u7247\u7528\u4e8e\u63d0\u53d6\u5e8f\u5217\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5e8f\u5217\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5217\u8868\u7684\u5207\u7247<\/strong>\uff1a<code>my_list[1:4]<\/code>\u5c06\u8fd4\u56de<code>[20, 30, 40]<\/code>\u3002\u5207\u7247\u7684\u5f62\u5f0f\u4e3a<code>[start:end]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff0c<code>end<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff0c\u4f46\u4e0d\u5305\u542b\u8be5\u4f4d\u7f6e\u7684\u5143\u7d20\u3002<\/li>\n<li><strong>\u5143\u7ec4\u7684\u5207\u7247<\/strong>\uff1a\u7c7b\u4f3c\u4e8e\u5217\u8868\uff0c<code>my_tuple[0:2]<\/code>\u5c06\u8fd4\u56de<code>(10, 20)<\/code>\u3002<\/li>\n<li><strong>\u5b57\u7b26\u4e32\u7684\u5207\u7247<\/strong>\uff1a<code>my_string[1:4]<\/code>\u5c06\u8fd4\u56de<code>&#39;ell&#39;<\/code>\u3002<\/li>\n<\/ul>\n<p><p>\u5207\u7247\u8fd8\u652f\u6301\u6b65\u957f\u53c2\u6570\uff0c\u4f8b\u5982<code>my_list[0:5:2]<\/code>\u5c06\u8fd4\u56de<code>[10, 30, 50]<\/code>\uff0c\u6b65\u957f\u4e3a2\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u5185\u7f6e\u65b9\u6cd5<\/strong><\/h2>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5185\u7f6e\u65b9\u6cd5\u7528\u4e8e\u63d0\u53d6\u548c\u64cd\u4f5c\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>\u5217\u8868\u65b9\u6cd5<\/h2>\n<\/p>\n<ul>\n<li><strong><code>list.append(x)<\/code><\/strong>\uff1a\u5411\u5217\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20<code>x<\/code>\u3002<\/li>\n<li><strong><code>list.extend(iterable)<\/code><\/strong>\uff1a\u901a\u8fc7\u6dfb\u52a0\u6307\u5b9a\u7684\u8fed\u4ee3\u5bf9\u8c61\u6765\u6269\u5c55\u5217\u8868\u3002<\/li>\n<li><strong><code>list.insert(i, x)<\/code><\/strong>\uff1a\u5728\u6307\u5b9a\u4f4d\u7f6e<code>i<\/code>\u63d2\u5165\u5143\u7d20<code>x<\/code>\u3002<\/li>\n<li><strong><code>list.pop([i])<\/code><\/strong>\uff1a\u79fb\u9664\u5e76\u8fd4\u56de\u5217\u8868\u4e2d<code>i<\/code>\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u82e5\u672a\u6307\u5b9a<code>i<\/code>\uff0c\u9ed8\u8ba4\u79fb\u9664\u5e76\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<li><strong><code>list.remove(x)<\/code><\/strong>\uff1a\u79fb\u9664\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20<code>x<\/code>\u3002<\/li>\n<\/ul>\n<p><h2>\u5b57\u5178\u65b9\u6cd5<\/h2>\n<\/p>\n<ul>\n<li><strong><code>dict.get(key[, default])<\/code><\/strong>\uff1a\u8fd4\u56de\u952e\u5bf9\u5e94\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002<\/li>\n<li><strong><code>dict.keys()<\/code><\/strong>\uff1a\u8fd4\u56de\u5b57\u5178\u7684\u6240\u6709\u952e\u3002<\/li>\n<li><strong><code>dict.values()<\/code><\/strong>\uff1a\u8fd4\u56de\u5b57\u5178\u7684\u6240\u6709\u503c\u3002<\/li>\n<li><strong><code>dict.items()<\/code><\/strong>\uff1a\u8fd4\u56de\u5b57\u5178\u4e2d\u6240\u6709\u7684\u952e\u503c\u5bf9\u3002<\/li>\n<\/ul>\n<p><h2>\u96c6\u5408\u65b9\u6cd5<\/h2>\n<\/p>\n<ul>\n<li><strong><code>set.add(elem)<\/code><\/strong>\uff1a\u5411\u96c6\u5408\u6dfb\u52a0\u5143\u7d20\u3002<\/li>\n<li><strong><code>set.remove(elem)<\/code><\/strong>\uff1a\u4ece\u96c6\u5408\u4e2d\u79fb\u9664\u5143\u7d20\u3002<\/li>\n<li><strong><code>set.pop()<\/code><\/strong>\uff1a\u79fb\u9664\u5e76\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<\/ul>\n<h2><strong>\u4e09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u3001\u9ad8\u6548\u7684\u751f\u6210\u548c\u8fc7\u6ee4\u5217\u8868\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u57fa\u7840\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>[expression for item in iterable]<\/code>\u3002\u4f8b\u5982\uff0c\u8981\u751f\u6210\u4e00\u4e2a\u5e73\u65b9\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>[x2 for x in range(10)]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u6761\u4ef6\u8fc7\u6ee4<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u52a0\u5165\u6761\u4ef6\uff0c\u4ee5\u8fc7\u6ee4\u5143\u7d20\u3002\u8bed\u6cd5\u4e3a<code>[expression for item in iterable if condition]<\/code>\u3002\u4f8b\u5982\uff0c<code>[x for x in range(10) if x % 2 == 0]<\/code>\u5c06\u8fd4\u56de\u6240\u6709\u5076\u6570<code>[0, 2, 4, 6, 8]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u5d4c\u5957\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\uff0c\u4ee5\u5904\u7406\u591a\u7ef4\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868<code>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>[num for row in matrix for num in row]<\/code>\u5c06\u5176\u5c55\u5e73\u6210\u4e00\u7ef4\u5217\u8868<code>[1, 2, 3, 4, 5, 6, 7, 8, 9]<\/code>\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u7b2c\u4e09\u65b9\u5e93<\/strong><\/h2>\n<p><p>Python\u7684\u4e30\u5bcc\u751f\u6001\u7cfb\u7edf\u4e2d\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7528\u4e8e\u63d0\u53d6\u548c\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>NumPy<\/h2>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u6838\u5fc3\u5e93\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u548c\u77e9\u9635\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u7ec4\u521b\u5efa\u4e0e\u7d22\u5f15<\/strong>\uff1a\u4f7f\u7528<code>numpy.array()<\/code>\u521b\u5efa\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u548c\u5207\u7247\u63d0\u53d6\u5143\u7d20\u3002<code>arr = numpy.array([[1, 2], [3, 4]])<\/code>\uff0c<code>arr[0, 1]<\/code>\u5c06\u8fd4\u56de<code>2<\/code>\u3002<\/li>\n<li><strong>\u5e03\u5c14\u7d22\u5f15<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\u521b\u5efa\u5e03\u5c14\u6570\u7ec4\uff0c\u7136\u540e\u7528\u6765\u63d0\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<code>arr[arr &gt; 2]<\/code>\u5c06\u8fd4\u56de<code>[3, 4]<\/code>\u3002<\/li>\n<li><strong>\u9ad8\u7ea7\u5207\u7247<\/strong>\uff1aNumPy\u652f\u6301\u591a\u79cd\u9ad8\u7ea7\u5207\u7247\u65b9\u6cd5\uff0c\u5305\u62ec\u6574\u6570\u6570\u7ec4\u7d22\u5f15\u548c\u82b1\u5f0f\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<p><h2>Pandas<\/h2>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u7279\u522b\u64c5\u957f\u5904\u7406\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u6846\u7684\u7d22\u5f15\u4e0e\u5207\u7247<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>loc<\/code>\u548c<code>iloc<\/code>\u5c5e\u6027\u63d0\u53d6\u6570\u636e\u3002\u4f8b\u5982\uff0c<code>df.loc[0, &#39;column_name&#39;]<\/code>\u6216<code>df.iloc[0, 1]<\/code>\u3002<\/li>\n<li><strong>\u6761\u4ef6\u7b5b\u9009<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\u63d0\u53d6\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u3002<code>df[df[&#39;column_name&#39;] &gt; 10]<\/code>\u5c06\u8fd4\u56de\u6240\u6709<code>column_name<\/code>\u5217\u503c\u5927\u4e8e10\u7684\u884c\u3002<\/li>\n<li><strong>\u591a\u5217\u9009\u62e9<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u5217\u540d\u5217\u8868\u9009\u62e9\u591a\u5217\u3002<code>df[[&#39;column1&#39;, &#39;column2&#39;]]<\/code>\u3002<\/li>\n<\/ul>\n<h2><strong>\u4e94\u3001\u5176\u4ed6\u9ad8\u7ea7\u7279\u6027<\/strong><\/h2>\n<p><p>Python\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7279\u6027\u548c\u5de5\u5177\uff0c\u7528\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u63d0\u53d6\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u4f7f\u7528\u5706\u62ec\u53f7\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u3002<code>(x2 for x in range(10))<\/code>\u521b\u5efa\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7<code>next()<\/code>\u51fd\u6570\u6216\u8fed\u4ee3\u6765\u63d0\u53d6\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>\u89e3\u5305<\/h2>\n<\/p>\n<p><p>Python\u652f\u6301\u5e8f\u5217\u89e3\u5305\uff0c\u53ef\u4ee5\u5c06\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u76f4\u63a5\u8d4b\u503c\u7ed9\u591a\u4e2a\u53d8\u91cf\u3002<code>a, b, c = (1, 2, 3)<\/code>\u5c06\u5143\u7ec4\u4e2d\u7684\u503c\u5206\u522b\u8d4b\u7ed9\u53d8\u91cf<code>a<\/code>\u3001<code>b<\/code>\u548c<code>c<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u8fed\u4ee3\u5de5\u5177<\/h2>\n<\/p>\n<p><p>Python\u7684<code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u590d\u6742\u7684\u8fed\u4ee3\u5668\uff0c\u6bd4\u5982<code>itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n()<\/code>\u53ef\u4ee5\u5c06\u591a\u4e2a\u8fed\u4ee3\u5bf9\u8c61\u94fe\u63a5\u5728\u4e00\u8d77\uff0c<code>itertools.islice()<\/code>\u7528\u4e8e\u4ece\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u63d0\u53d6\u5207\u7247\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u5f0f\uff0cPython\u63d0\u4f9b\u4e86\u4ece\u7b80\u5355\u5230\u590d\u6742\u7684\u591a\u79cd\u65b9\u6cd5\u6765\u63d0\u53d6\u548c\u64cd\u4f5c\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u5143\u7d20\u3002\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u591f\u63d0\u5347\u7f16\u7a0b\u6548\u7387\uff0c\u8fd8\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u5904\u7406\u5404\u79cd\u6570\u636e\u64cd\u4f5c\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u5217\u8868\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u8981\u63d0\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list[0]<\/code>\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u4f7f\u7528<code>list[1]<\/code>\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u5982\u679c\u5e0c\u671b\u63d0\u53d6\u591a\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\uff0c\u5982<code>list[start:end]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u4ece<code>start<\/code>\u7d22\u5f15\u5230<code>end-1<\/code>\u7d22\u5f15\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6216\u5b50\u4e32\uff1f<\/strong><br \/>\u8981\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u5b57\u7b26\u6216\u5b50\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<code>string[start:end]<\/code>\u5c06\u8fd4\u56de\u4ece<code>start<\/code>\u5230<code>end-1<\/code>\u7684\u5b57\u7b26\u3002\u5982\u679c\u9700\u8981\u67e5\u627e\u67d0\u4e2a\u5b50\u4e32\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>string.find(substring)<\/code>\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u5b50\u4e32\u7684\u8d77\u59cb\u7d22\u5f15\uff0c\u5982\u679c\u672a\u627e\u5230\u5219\u8fd4\u56de-1\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6587\u672c\u4e2d\u7684\u4fe1\u606f\uff1f<\/strong><br \/>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u63d0\u53d6\u6587\u672c\u4e2d\u7279\u5b9a\u6a21\u5f0f\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>re.compile(pattern)<\/code>\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528<code>pattern.findall(string)<\/code>\u65b9\u6cd5\u6765\u63d0\u53d6\u6240\u6709\u5339\u914d\u7684\u5b50\u4e32\u3002\u5982\u679c\u53ea\u9700\u63d0\u53d6\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pattern.search(string)<\/code>\u5e76\u901a\u8fc7<code>match.group()<\/code>\u65b9\u6cd5\u83b7\u53d6\u7ed3\u679c\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u975e\u5e38\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u590d\u6742\u7684\u6587\u672c\u63d0\u53d6\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u63d0\u53d6\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\u7d22\u5f15\u3001\u5207\u7247\u3001\u4f7f\u7528\u5185\u7f6e\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd [&hellip;]","protected":false},"author":3,"featured_media":988016,"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\/988006"}],"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=988006"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988006\/revisions"}],"predecessor-version":[{"id":988021,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988006\/revisions\/988021"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/988016"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=988006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=988006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=988006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}