{"id":932282,"date":"2024-12-26T17:54:58","date_gmt":"2024-12-26T09:54:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/932282.html"},"modified":"2024-12-26T17:55:00","modified_gmt":"2024-12-26T09:55:00","slug":"python-%e5%85%83%e7%a5%96%e5%a6%82%e4%bd%95%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/932282.html","title":{"rendered":"python \u5143\u7956\u5982\u4f55\u7d22\u5f15"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070221\/d87e2ef4-f0e2-440f-a94d-d02383ea8d78.webp\" alt=\"python \u5143\u7956\u5982\u4f55\u7d22\u5f15\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7684\u5143\u7ec4\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u3001\u5207\u7247\u548c\u89e3\u5305\u7b49\u64cd\u4f5c\u8fdb\u884c\u8bbf\u95ee\u548c\u64cd\u4f5c\u3002<\/strong> \u9996\u5148\uff0c\u5143\u7ec4\u662fPython\u4e2d\u7684\u4e00\u79cd\u4e0d\u53ef\u53d8\u5e8f\u5217\u7c7b\u578b\uff0c\u65e0\u6cd5\u50cf\u5217\u8868\u4e00\u6837\u8fdb\u884c\u4fee\u6539\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u5176\u5143\u7d20\u3002\u4f7f\u7528\u65b9\u62ec\u53f7\u52a0\u4e0a\u7d22\u5f15\u4f4d\u7f6e\u7684\u65b9\u5f0f\u5373\u53ef\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002\u53ef\u4ee5\u4f7f\u7528\u8d1f\u7d22\u5f15\u6765\u4ece\u5143\u7ec4\u7684\u672b\u5c3e\u5f00\u59cb\u8bbf\u95ee\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5143\u7ec4<code>my_tuple = (1, 2, 3, 4)<\/code>\u4e2d\uff0c<code>my_tuple[0]<\/code>\u5c06\u8fd4\u56de1\uff0c\u800c<code>my_tuple[-1]<\/code>\u5c06\u8fd4\u56de4\u3002\u9664\u4e86\u57fa\u672c\u7684\u7d22\u5f15\u64cd\u4f5c\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\u6765\u8bbf\u95ee\u5143\u7ec4\u7684\u5b50\u96c6\uff0c\u8fd9\u4e0e\u5217\u8868\u7684\u5207\u7247\u64cd\u4f5c\u7c7b\u4f3c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5143\u7ec4\u7684\u57fa\u672c\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u5143\u7ec4\u662fPython\u4e2d\u7528\u4e8e\u5b58\u50a8\u591a\u4e2a\u9879\u76ee\u7684\u4e0d\u53ef\u53d8\u5e8f\u5217\u3002\u4e0e\u5217\u8868\u4e0d\u540c\uff0c\u5143\u7ec4\u7684\u5143\u7d20\u4e00\u65e6\u5b9a\u4e49\u5c31\u4e0d\u80fd\u88ab\u4fee\u6539\uff0c\u8fd9\u4f7f\u5f97\u5143\u7ec4\u5728\u9700\u8981\u4e0d\u53ef\u53d8\u6570\u636e\u7ed3\u6784\u65f6\u975e\u5e38\u6709\u7528\u3002\u5143\u7ec4\u901a\u8fc7\u5706\u62ec\u53f7\u5b9a\u4e49\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u5176\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6b63\u5411\u7d22\u5f15<\/strong><\/p>\n<\/p>\n<p><p>\u5143\u7ec4\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u8fd9\u610f\u5473\u7740\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f0\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f1\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u901a\u8fc7\u6b63\u5411\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;)<\/p>\n<p>print(my_tuple[0])  # \u8f93\u51fa\uff1aapple<\/p>\n<p>print(my_tuple[1])  # \u8f93\u51fa\uff1abanana<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u662f\u6700\u5e38\u89c1\u7684\u7d22\u5f15\u65b9\u5f0f\uff0c\u5728\u9700\u8981\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5177\u4f53\u5143\u7d20\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8d1f\u5411\u7d22\u5f15<\/strong><\/p>\n<\/p>\n<p><p>Python\u8fd8\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u8d1f\u7d22\u5f15\u6765\u4ece\u5143\u7ec4\u7684\u672b\u5c3e\u5f00\u59cb\u8bbf\u95ee\u5143\u7d20\u3002\u8d1f\u7d22\u5f15\u4ece-1\u5f00\u59cb\uff0c\u8fd9\u610f\u5473\u7740\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f-1\uff0c\u5012\u6570\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f-2\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;)<\/p>\n<p>print(my_tuple[-1])  # \u8f93\u51fa\uff1acherry<\/p>\n<p>print(my_tuple[-2])  # \u8f93\u51fa\uff1abanana<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8d1f\u5411\u7d22\u5f15\u5728\u9700\u8981\u4ece\u672b\u5c3e\u5f00\u59cb\u8bbf\u95ee\u5143\u7d20\u65f6\u7279\u522b\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u4e0d\u77e5\u9053\u5143\u7ec4\u7684\u957f\u5ea6\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u5143\u7ec4\u7684\u5207\u7247\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5207\u7247\u662fPython\u4e2d\u7528\u4e8e\u8bbf\u95ee\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5b57\u7b26\u4e32\u548c\u5143\u7ec4\uff09\u7684\u4e00\u90e8\u5206\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5207\u7247\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\u6765\u8bbf\u95ee\u5143\u7ec4\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u5207\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u5207\u7247\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>my_tuple[start:stop]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff08\u5305\u542b\uff09\uff0c<code>stop<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff08\u4e0d\u5305\u542b\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;)<\/p>\n<p>print(my_tuple[1:3])  # \u8f93\u51fa\uff1a(&#39;banana&#39;, &#39;cherry&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u8bbf\u95ee\u4e86\u4ece\u7d22\u5f151\u5f00\u59cb\u5230\u7d22\u5f153\u4e4b\u524d\u7684\u5143\u7d20\u3002\u5207\u7247\u64cd\u4f5c\u4e0d\u4f1a\u4fee\u6539\u539f\u59cb\u5143\u7ec4\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6b65\u957f\u5207\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u8d77\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\uff0c\u5207\u7247\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6b65\u957f\uff08<code>step<\/code>\uff09\u53c2\u6570\u3002\u6b65\u957f\u9ed8\u8ba4\u4e3a1\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u6b65\u957f\u6765\u6539\u53d8\u5207\u7247\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;)<\/p>\n<p>print(my_tuple[0:4:2])  # \u8f93\u51fa\uff1a(&#39;apple&#39;, &#39;cherry&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u6b65\u957f\u4e3a2\uff0c\u56e0\u6b64\u4ece\u7d22\u5f150\u5f00\u59cb\uff0c\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u5207\u7247\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5143\u7ec4\u7684\u89e3\u5305\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u89e3\u5305\u662fPython\u4e2d\u4e00\u79cd\u5c06\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u5206\u914d\u7ed9\u591a\u4e2a\u53d8\u91cf\u7684\u64cd\u4f5c\u3002\u8fd9\u4f7f\u5f97\u5728\u4e0d\u4f7f\u7528\u7d22\u5f15\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8bbf\u95ee\u5143\u7ec4\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u89e3\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u5143\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u5206\u914d\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u89e3\u5305\u5143\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;)<\/p>\n<p>fruit1, fruit2, fruit3 = my_tuple<\/p>\n<p>print(fruit1)  # \u8f93\u51fa\uff1aapple<\/p>\n<p>print(fruit2)  # \u8f93\u51fa\uff1abanana<\/p>\n<p>print(fruit3)  # \u8f93\u51fa\uff1acherry<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5143\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u88ab\u5206\u914d\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f7f\u5f97\u8bbf\u95ee\u5143\u7d20\u53d8\u5f97\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e26\u661f\u53f7\u7684\u89e3\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5143\u7ec4\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u53ef\u80fd\u4e0d\u786e\u5b9a\u3002\u5e26\u661f\u53f7\u7684\u89e3\u5305\u5141\u8bb8\u6211\u4eec\u5c06\u591a\u4e2a\u5143\u7d20\u5206\u914d\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;)<\/p>\n<p>fruit1, *other_fruits = my_tuple<\/p>\n<p>print(fruit1)         # \u8f93\u51fa\uff1aapple<\/p>\n<p>print(other_fruits)   # \u8f93\u51fa\uff1a[&#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>fruit1<\/code>\u88ab\u5206\u914d\u7ed9\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u800c<code>other_fruits<\/code>\u88ab\u5206\u914d\u7ed9\u5143\u7ec4\u4e2d\u5269\u4f59\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u5143\u7ec4\u8fdb\u884c\u6570\u636e\u4ea4\u6362<\/p>\n<\/p>\n<p><p>\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u7279\u6027\u4f7f\u5176\u6210\u4e3a\u4e00\u79cd\u7406\u60f3\u7684\u6570\u636e\u4ea4\u6362\u5de5\u5177\u3002\u901a\u8fc7\u4f7f\u7528\u5143\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\u4ea4\u6362\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4ea4\u6362\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5143\u7ec4\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u662fPython\u4e2d\u7684\u4e00\u79cd\u5e38\u89c1\u6280\u5de7\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = 10<\/p>\n<p>a, b = b, a<\/p>\n<p>print(a)  # \u8f93\u51fa\uff1a10<\/p>\n<p>print(b)  # \u8f93\u51fa\uff1a5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5229\u7528\u4e86\u5143\u7ec4\u7684\u89e3\u5305\u7279\u6027\uff0c\u4f7f\u5f97\u4ea4\u6362\u53d8\u91cf\u7684\u503c\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u591a\u53d8\u91cf\u4ea4\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u4e0d\u4ec5\u53ef\u4ee5\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u8fd8\u53ef\u4ee5\u4ea4\u6362\u591a\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a, b, c = 1, 2, 3<\/p>\n<p>a, b, c = c, a, b<\/p>\n<p>print(a, b, c)  # \u8f93\u51fa\uff1a3 1 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u4f9d\u8d56\u4e8e\u5143\u7ec4\u7684\u89e3\u5305\u7279\u6027\uff0c\u4f7f\u5f97\u591a\u53d8\u91cf\u4ea4\u6362\u53d8\u5f97\u76f4\u89c2\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5143\u7ec4\u7684\u5176\u4ed6\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u57fa\u672c\u64cd\u4f5c\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u6709\u7528\u7684\u5143\u7ec4\u64cd\u4f5c\uff0c\u4f8b\u5982\u8fde\u63a5\u3001\u91cd\u590d\u548c\u6210\u5458\u8d44\u683c\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5143\u7ec4\u8fde\u63a5<\/strong><\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7\uff08<code>+<\/code>\uff09\u64cd\u4f5c\u7b26\u8fde\u63a5\u4e24\u4e2a\u6216\u591a\u4e2a\u5143\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tuple1 = (1, 2, 3)<\/p>\n<p>tuple2 = (4, 5, 6)<\/p>\n<p>tuple3 = tuple1 + tuple2<\/p>\n<p>print(tuple3)  # \u8f93\u51fa\uff1a(1, 2, 3, 4, 5, 6)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fde\u63a5\u64cd\u4f5c\u5c06\u4e24\u4e2a\u5143\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5143\u7ec4\u91cd\u590d<\/strong><\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e58\u53f7\uff08<code>*<\/code>\uff09\u64cd\u4f5c\u7b26\u91cd\u590d\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (&#39;apple&#39;, &#39;banana&#39;)<\/p>\n<p>print(my_tuple * 2)  # \u8f93\u51fa\uff1a(&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;banana&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u91cd\u590d\u64cd\u4f5c\u5c06\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u91cd\u590d\u6307\u5b9a\u6b21\u6570\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6210\u5458\u8d44\u683c\u6d4b\u8bd5<\/strong><\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u548c<code>not in<\/code>\u64cd\u4f5c\u7b26\u6d4b\u8bd5\u5143\u7ec4\u4e2d\u662f\u5426\u5305\u542b\u7279\u5b9a\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;)<\/p>\n<p>print(&#39;apple&#39; in my_tuple)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p>print(&#39;grape&#39; not in my_tuple)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6210\u5458\u8d44\u683c\u6d4b\u8bd5\u5728\u9700\u8981\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5143\u7ec4\u4e2d\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5143\u7ec4\u7684\u6027\u80fd\u4f18\u52bf<\/p>\n<\/p>\n<p><p>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u5b83\u4eec\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5177\u6709\u6027\u80fd\u4f18\u52bf\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5b58\u50a8\u5927\u91cf\u6570\u636e\u4e14\u4e0d\u5e0c\u671b\u6570\u636e\u88ab\u4fee\u6539\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5185\u5b58\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u4e0e\u5217\u8868\u76f8\u6bd4\uff0c\u5143\u7ec4\u901a\u5e38\u5360\u7528\u66f4\u5c11\u7684\u5185\u5b58\u3002\u8fd9\u662f\u56e0\u4e3a\u5143\u7ec4\u7684\u7ed3\u6784\u66f4\u7b80\u5355\uff0c\u5e76\u4e14\u7531\u4e8e\u5176\u4e0d\u53ef\u53d8\u6027\uff0cPython\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u5185\u5b58\u7ba1\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd<\/strong><\/p>\n<\/p>\n<p><p>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u5b83\u4eec\u7684\u64cd\u4f5c\u901a\u5e38\u6bd4\u5217\u8868\u66f4\u5feb\u3002\u5c24\u5176\u662f\u5728\u9700\u8981\u9891\u7e41\u8bbf\u95ee\u4f46\u4e0d\u9700\u8981\u4fee\u6539\u7684\u6570\u636e\u65f6\uff0c\u4f7f\u7528\u5143\u7ec4\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u5168\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\u4f7f\u5f97\u5b83\u4eec\u5728\u9700\u8981\u786e\u4fdd\u6570\u636e\u4e0d\u88ab\u610f\u5916\u4fee\u6539\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u4f7f\u7528\u5143\u7ec4\u53ef\u4ee5\u907f\u514d\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u5143\u7ec4\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5143\u7ec4\u5728Python\u4e2d\u6709\u8bb8\u591a\u5e94\u7528\u573a\u666f\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u4e0d\u53ef\u53d8\u6570\u636e\u7ed3\u6784\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c<\/strong><\/p>\n<\/p>\n<p><p>\u5143\u7ec4\u7ecf\u5e38\u7528\u4e8e\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\u3002\u8fd9\u4f7f\u5f97\u5728\u4e00\u4e2a\u8fd4\u56de\u503c\u4e2d\u5305\u542b\u591a\u4e2a\u76f8\u5173\u6570\u636e\u53d8\u5f97\u7b80\u5355\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_coordinates():<\/p>\n<p>    return (10, 20)<\/p>\n<p>x, y = get_coordinates()<\/p>\n<p>print(x, y)  # \u8f93\u51fa\uff1a10 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u591a\u4e2a\u76f8\u5173\u503c\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b57\u5178\u952e<\/strong><\/p>\n<\/p>\n<p><p>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u4f5c\u5b57\u5178\u7684\u952e\u3002\u8fd9\u5728\u9700\u8981\u5c06\u591a\u4e2a\u503c\u7ec4\u5408\u4e3a\u4e00\u4e2a\u952e\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">coordinates = {(0, 0): &#39;Origin&#39;, (1, 1): &#39;Point1&#39;}<\/p>\n<p>print(coordinates[(0, 0)])  # \u8f93\u51fa\uff1aOrigin<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5143\u7ec4\u4f5c\u4e3a\u5b57\u5178\u952e\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e76\u5141\u8bb8\u901a\u8fc7\u5750\u6807\u7b49\u591a\u7ef4\u4fe1\u606f\u8fdb\u884c\u5feb\u901f\u67e5\u627e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516b\u3001\u5143\u7ec4\u7684\u5e38\u89c1\u64cd\u4f5c\u548c\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4f46Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u65b9\u6cd5\u548c\u64cd\u4f5c\u6765\u5904\u7406\u5143\u7ec4\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8ba1\u6570\u548c\u7d22\u5f15<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684\u5143\u7ec4\u63d0\u4f9b\u4e86\u4e24\u4e2a\u5185\u7f6e\u65b9\u6cd5\uff1a<code>count()<\/code>\u548c<code>index()<\/code>\u3002<code>count()<\/code>\u65b9\u6cd5\u7528\u4e8e\u8ba1\u7b97\u5143\u7ec4\u4e2d\u67d0\u4e2a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u800c<code>index()<\/code>\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;apple&#39;)<\/p>\n<p>print(my_tuple.count(&#39;apple&#39;))  # \u8f93\u51fa\uff1a2<\/p>\n<p>print(my_tuple.index(&#39;banana&#39;))  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u5728\u9700\u8981\u5206\u6790\u5143\u7ec4\u4e2d\u5143\u7d20\u7684\u5206\u5e03\u548c\u4f4d\u7f6e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5143\u7ec4\uff0c\u4f8b\u5982<code>len()<\/code>\u3001<code>min()<\/code>\u3001<code>max()<\/code>\u548c<code>sum()<\/code>\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u83b7\u53d6\u5143\u7ec4\u7684\u957f\u5ea6\u3001\u6700\u5c0f\u503c\u3001\u6700\u5927\u503c\u548c\u603b\u548c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = (1, 2, 3, 4, 5)<\/p>\n<p>print(len(numbers))  # \u8f93\u51fa\uff1a5<\/p>\n<p>print(min(numbers))  # \u8f93\u51fa\uff1a1<\/p>\n<p>print(max(numbers))  # \u8f93\u51fa\uff1a5<\/p>\n<p>print(sum(numbers))  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u51fd\u6570\u4f7f\u5f97\u5bf9\u5143\u7ec4\u7684\u57fa\u672c\u7edf\u8ba1\u64cd\u4f5c\u53d8\u5f97\u7b80\u5355\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\uff0cPython\u4e2d\u7684\u5143\u7ec4\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u9700\u8981\u4e0d\u53ef\u53d8\u6027\u548c\u9ad8\u6548\u8bbf\u95ee\u7684\u573a\u666f\u3002\u901a\u8fc7\u7d22\u5f15\u3001\u5207\u7247\u3001\u89e3\u5305\u548c\u5176\u4ed6\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u8bbf\u95ee\u548c\u5904\u7406\u5143\u7ec4\u4e2d\u7684\u6570\u636e\u3002\u540c\u65f6\uff0c\u5143\u7ec4\u5728\u6027\u80fd\u3001\u5b89\u5168\u6027\u548c\u5e94\u7528\u573a\u666f\u65b9\u9762\u4e5f\u5177\u6709\u72ec\u7279\u7684\u4f18\u52bf\u3002\u65e0\u8bba\u662f\u5728\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\u3001\u4f7f\u7528\u5b57\u5178\u952e\uff0c\u8fd8\u662f\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u786e\u4fdd\u6570\u636e\u5b89\u5168\uff0c\u5143\u7ec4\u90fd\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u800c\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u5143\u7ec4\u5e76\u8fdb\u884c\u7d22\u5f15\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5143\u7ec4\u53ef\u4ee5\u901a\u8fc7\u5706\u62ec\u53f7\u6765\u521b\u5efa\uff0c\u4f8b\u5982 <code>my_tuple = (1, 2, 3, 4)<\/code>\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u8981\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>my_tuple[0]<\/code> \u6765\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c<code>my_tuple[1]<\/code> \u83b7\u53d6\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u8d1f\u7d22\u5f15\u4e5f\u53ef\u4ee5\u4f7f\u7528\uff0c\u6bd4\u5982 <code>my_tuple[-1]<\/code> \u5c06\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>2. \u5143\u7ec4\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u5c1d\u8bd5\u4f7f\u7528\u8d85\u51fa\u5143\u7ec4\u8303\u56f4\u7684\u7d22\u5f15\uff0c\u4f8b\u5982 <code>my_tuple[5]<\/code> \u800c\u5143\u7ec4\u4ec5\u5305\u542b4\u4e2a\u5143\u7d20\uff0c\u5c06\u5f15\u53d1 <code>IndexError<\/code>\u3002\u8fd9\u610f\u5473\u7740\u4f60\u9700\u8981\u786e\u4fdd\u6240\u4f7f\u7528\u7684\u7d22\u5f15\u5728\u5143\u7ec4\u7684\u6709\u6548\u8303\u56f4\u5185\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u901a\u8fc7\u5207\u7247\u6765\u8bbf\u95ee\u5143\u7ec4\u7684\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5207\u7247\u662f\u4e00\u79cd\u83b7\u53d6\u5143\u7ec4\u90e8\u5206\u5143\u7d20\u7684\u65b9\u4fbf\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528 <code>:<\/code> \u8fd0\u7b97\u7b26\uff0c\u53ef\u4ee5\u6307\u5b9a\u5f00\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\uff0c\u4f8b\u5982 <code>my_tuple[1:3]<\/code> \u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u5143\u7ec4 <code>(2, 3)<\/code>\uff0c\u5305\u542b\u4ece\u7d22\u5f151\u5230\u7d22\u5f152\u7684\u5143\u7d20\uff08\u4e0d\u5305\u62ec\u7d22\u5f153\uff09\u3002\u5207\u7247\u4e5f\u652f\u6301\u8d1f\u7d22\u5f15\uff0c\u4f7f\u5f97\u8bbf\u95ee\u5143\u7ec4\u7684\u672b\u5c3e\u90e8\u5206\u53d8\u5f97\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<p><strong>4. \u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u662f\u5426\u53ef\u4ee5\u88ab\u4fee\u6539\uff1f<\/strong><br \/>\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\uff0c\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u4e0d\u80fd\u88ab\u66f4\u6539\u3002\u4f60\u4e0d\u80fd\u4f7f\u7528\u7d22\u5f15\u6765\u4fee\u6539\u5143\u7ec4\u4e2d\u7684\u503c\uff0c\u4f8b\u5982 <code>my_tuple[0] = 10<\/code> \u5c06\u5bfc\u81f4 <code>TypeError<\/code>\u3002\u5982\u679c\u9700\u8981\u4fee\u6539\u5143\u7d20\uff0c\u901a\u5e38\u7684\u505a\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5143\u7ec4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684\u5143\u7ec4\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u3001\u5207\u7247\u548c\u89e3\u5305\u7b49\u64cd\u4f5c\u8fdb\u884c\u8bbf\u95ee\u548c\u64cd\u4f5c\u3002 \u9996\u5148\uff0c\u5143\u7ec4\u662fPython\u4e2d\u7684\u4e00\u79cd\u4e0d\u53ef\u53d8\u5e8f\u5217 [&hellip;]","protected":false},"author":3,"featured_media":932284,"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\/932282"}],"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=932282"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932282\/revisions"}],"predecessor-version":[{"id":932285,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932282\/revisions\/932285"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/932284"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=932282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=932282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=932282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}