{"id":1083704,"date":"2025-01-08T13:00:33","date_gmt":"2025-01-08T05:00:33","guid":{"rendered":""},"modified":"2025-01-08T13:00:39","modified_gmt":"2025-01-08T05:00:39","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e4%b8%80%e4%b8%aa%e5%85%83%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1083704.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u5143\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194130\/0e370d85-a4b2-47c8-aba8-7aa28d8ba990.webp\" alt=\"python\u4e2d\u5982\u4f55\u8f93\u5165\u4e00\u4e2a\u5143\u7ec4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8f93\u5165\u4e00\u4e2a\u5143\u7ec4\u3001\u4f7f\u7528\u62ec\u53f7\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528\u8fed\u4ee3\u5668\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u62ec\u53f7\u548c\u9017\u53f7\u5206\u9694\u5143\u7d20\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5143\u7ec4\u662f\u4e00\u79cd\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u5b58\u50a8\u4e00\u7ec4\u6709\u5e8f\u7684\u5143\u7d20\u3002\u8981\u521b\u5efa\u4e00\u4e2a\u5143\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u5c0f\u62ec\u53f7 <code>()<\/code> \u5c06\u5143\u7d20\u62ec\u8d77\u6765\uff0c\u5e76\u7528\u9017\u53f7 <code>,<\/code> \u5206\u9694\u6bcf\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>(1, 2, 3)<\/code> \u662f\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u6574\u6570\u7684\u5143\u7ec4\u3002\u5143\u7ec4\u7684\u5143\u7d20\u53ef\u4ee5\u662f\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f8b\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u7b49\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u521b\u5efa\u5143\u7ec4\uff0c\u80fd\u591f\u6709\u6548\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u62ec\u53f7\u548c\u9017\u53f7\u8f93\u5165\u5143\u7ec4<\/h3>\n<\/p>\n<p><p>\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5c0f\u62ec\u53f7 <code>()<\/code> \u548c\u9017\u53f7 <code>,<\/code> \u6765\u8f93\u5165\u5143\u7ec4\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5143\u7ec4<\/p>\n<p>tuple1 = (1, 2, 3)<\/p>\n<p>print(tuple1)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u5143\u7ec4<\/strong><\/h2>\n<p>tuple2 = (1, &quot;hello&quot;, 3.14)<\/p>\n<p>print(tuple2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u5143\u7ec4\u3002\u7b2c\u4e00\u4e2a\u5143\u7ec4 <code>tuple1<\/code> \u5305\u542b\u4e09\u4e2a\u6574\u6570\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7ec4 <code>tuple2<\/code> \u5305\u542b\u4e00\u4e2a\u6574\u6570\u3001\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4e00\u4e2a\u6d6e\u70b9\u6570\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u5143\u7ec4\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>tuple()<\/code><\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570 <code>tuple()<\/code>\uff0c\u53ef\u4ee5\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>tuple1 = tuple(list1)<\/p>\n<p>print(tuple1)<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5143\u7ec4<\/strong><\/h2>\n<p>string1 = &quot;hello&quot;<\/p>\n<p>tuple2 = tuple(string1)<\/p>\n<p>print(tuple2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>tuple()<\/code> \u51fd\u6570\u5c06\u4e00\u4e2a\u5217\u8868\u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002<code>tuple()<\/code> \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8be5\u5bf9\u8c61\u5143\u7d20\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u8fed\u4ee3\u5668\u8f93\u5165\u5143\u7ec4<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u5668\u6765\u751f\u6210\u5143\u7ec4\u3002\u8fed\u4ee3\u5668\u662f\u4e00\u79cd\u53ef\u4ee5\u9010\u4e2a\u8bbf\u95ee\u5176\u5143\u7d20\u7684\u5bf9\u8c61\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa\u4e00\u4e2a\u5143\u7ec4<\/p>\n<p>tuple1 = tuple(x for x in range(5))<\/p>\n<p>print(tuple1)<\/p>\n<h2><strong>\u4f7f\u7528 map() \u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5143\u7ec4<\/strong><\/h2>\n<p>tuple2 = tuple(map(str, range(5)))<\/p>\n<p>print(tuple2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c <code>map()<\/code> \u51fd\u6570\u521b\u5efa\u4e86\u4e24\u4e2a\u5143\u7ec4\u3002\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c <code>map()<\/code> \u51fd\u6570\u90fd\u662f\u521b\u5efa\u8fed\u4ee3\u5668\u7684\u5e38\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u751f\u6210\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ece\u7528\u6237\u8f93\u5165\u521b\u5efa\u5143\u7ec4<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4ece\u7528\u6237\u8f93\u5165\u83b7\u53d6\u6570\u636e\u5e76\u521b\u5efa\u5143\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\uff0c\u5e76\u4f7f\u7528 <code>split()<\/code> \u65b9\u6cd5\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5217\u8868\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u7528\u6237\u8f93\u5165\u83b7\u53d6\u6570\u636e\u5e76\u521b\u5efa\u5143\u7ec4<\/p>\n<p>input_str = input(&quot;\u8bf7\u8f93\u5165\u4e00\u7ec4\u6570\u5b57\uff0c\u7528\u9017\u53f7\u5206\u9694\uff1a&quot;)<\/p>\n<p>input_list = input_str.split(&#39;,&#39;)<\/p>\n<p>input_tuple = tuple(map(int, input_list))<\/p>\n<p>print(input_tuple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528 <code>split(&#39;,&#39;)<\/code> \u65b9\u6cd5\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5217\u8868\uff0c\u6700\u540e\u4f7f\u7528 <code>map(int, input_list)<\/code> \u5c06\u5217\u8868\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5d4c\u5957\u5143\u7ec4<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u53ef\u4ee5\u5305\u542b\u5176\u4ed6\u5143\u7ec4\u4f5c\u4e3a\u5143\u7d20\uff0c\u5f62\u6210\u5d4c\u5957\u7ed3\u6784\u3002\u8fd9\u79cd\u5d4c\u5957\u5143\u7ec4\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u5143\u7ec4<\/p>\n<p>nested_tuple = ((1, 2), (3, 4), (5, 6))<\/p>\n<p>print(nested_tuple)<\/p>\n<h2><strong>\u8bbf\u95ee\u5d4c\u5957\u5143\u7ec4\u7684\u5143\u7d20<\/strong><\/h2>\n<p>print(nested_tuple[0])     # \u8f93\u51fa (1, 2)<\/p>\n<p>print(nested_tuple[1][1])  # \u8f93\u51fa 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5143\u7ec4\u7684\u5d4c\u5957\u5143\u7ec4 <code>nested_tuple<\/code>\u3002\u5d4c\u5957\u5143\u7ec4\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u591a\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u77e9\u9635\u3001\u8868\u683c\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5143\u7ec4\u89e3\u5305<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u89e3\u5305\u662f\u4e00\u79cd\u5c06\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u8d4b\u503c\u7ed9\u591a\u4e2a\u53d8\u91cf\u7684\u65b9\u6cd5\u3002\u5143\u7ec4\u89e3\u5305\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5143\u7ec4\u89e3\u5305<\/p>\n<p>tuple1 = (1, 2, 3)<\/p>\n<p>a, b, c = tuple1<\/p>\n<p>print(a, b, c)<\/p>\n<h2><strong>\u5d4c\u5957\u5143\u7ec4\u89e3\u5305<\/strong><\/h2>\n<p>nested_tuple = ((1, 2), (3, 4), (5, 6))<\/p>\n<p>(x1, y1), (x2, y2), (x3, y3) = nested_tuple<\/p>\n<p>print(x1, y1, x2, y2, x3, y3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5143\u7ec4\u89e3\u5305\u5c06\u5143\u7ec4 <code>tuple1<\/code> \u4e2d\u7684\u5143\u7d20\u8d4b\u503c\u7ed9\u53d8\u91cf <code>a<\/code>\u3001<code>b<\/code> \u548c <code>c<\/code>\u3002\u540c\u6837\uff0c\u6211\u4eec\u8fd8\u6f14\u793a\u4e86\u5d4c\u5957\u5143\u7ec4\u7684\u89e3\u5305\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5143\u7ec4\u7684\u5e38\u7528\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u4f5c\u4e3a\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u652f\u6301\u591a\u79cd\u5e38\u7528\u64cd\u4f5c\uff0c\u5305\u62ec\u7d22\u5f15\u8bbf\u95ee\u3001\u5207\u7247\u3001\u8fde\u63a5\u3001\u91cd\u590d\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5143\u7ec4\u7684\u5e38\u7528\u64cd\u4f5c<\/p>\n<p>tuple1 = (1, 2, 3, 4, 5)<\/p>\n<h2><strong>\u7d22\u5f15\u8bbf\u95ee<\/strong><\/h2>\n<p>print(tuple1[0])  # \u8f93\u51fa 1<\/p>\n<p>print(tuple1[-1]) # \u8f93\u51fa 5<\/p>\n<h2><strong>\u5207\u7247<\/strong><\/h2>\n<p>print(tuple1[1:3]) # \u8f93\u51fa (2, 3)<\/p>\n<p>print(tuple1[:3])  # \u8f93\u51fa (1, 2, 3)<\/p>\n<p>print(tuple1[3:])  # \u8f93\u51fa (4, 5)<\/p>\n<h2><strong>\u8fde\u63a5<\/strong><\/h2>\n<p>tuple2 = (6, 7, 8)<\/p>\n<p>print(tuple1 + tuple2) # \u8f93\u51fa (1, 2, 3, 4, 5, 6, 7, 8)<\/p>\n<h2><strong>\u91cd\u590d<\/strong><\/h2>\n<p>print(tuple1 * 2) # \u8f93\u51fa (1, 2, 3, 4, 5, 1, 2, 3, 4, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5143\u7ec4\u7684\u7d22\u5f15\u8bbf\u95ee\u3001\u5207\u7247\u3001\u8fde\u63a5\u548c\u91cd\u590d\u64cd\u4f5c\u3002\u8fd9\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7075\u6d3b\u5730\u5904\u7406\u548c\u64cd\u4f5c\u5143\u7ec4\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5143\u7ec4\u548c\u5217\u8868\u7684\u533a\u522b<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u548c\u5217\u8868\u662fPython\u4e2d\u4e24\u79cd\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4eec\u6709\u4e00\u4e9b\u76f8\u4f3c\u4e4b\u5904\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u91cd\u8981\u7684\u533a\u522b\u3002<\/p>\n<\/p>\n<p><h4>\u4e0d\u53ef\u53d8\u6027<\/h4>\n<\/p>\n<p><p>\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4e00\u65e6\u521b\u5efa\uff0c\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u4e0d\u80fd\u4fee\u6539\u3001\u6dfb\u52a0\u6216\u5220\u9664\u3002\u800c\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u5728\u5217\u8868\u4e2d\u8fdb\u884c\u4fee\u6539\u3001\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5143\u7ec4\u4e0d\u53ef\u53d8<\/p>\n<p>tuple1 = (1, 2, 3)<\/p>\n<h2><strong>tuple1[0] = 4  # \u4f1a\u5f15\u53d1 TypeError<\/strong><\/h2>\n<h2><strong>\u5217\u8868\u53ef\u53d8<\/strong><\/h2>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list1[0] = 4<\/p>\n<p>print(list1)  # \u8f93\u51fa [4, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5143\u7ec4\u7684\u6027\u80fd\u53ef\u80fd\u4f18\u4e8e\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5143\u7ec4\u7684\u54c8\u5e0c\u503c\u53ef\u4ee5\u8ba1\u7b97\u548c\u7f13\u5b58\uff0c\u9002\u5408\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5143\u7ec4\u53ef\u4ee5\u4f5c\u4e3a\u5b57\u5178\u7684\u952e<\/p>\n<p>dict1 = {(1, 2): &quot;value1&quot;, (3, 4): &quot;value2&quot;}<\/p>\n<p>print(dict1[(1, 2)])  # \u8f93\u51fa &quot;value1&quot;<\/p>\n<h2><strong>\u5217\u8868\u4e0d\u80fd\u4f5c\u4e3a\u5b57\u5178\u7684\u952e<\/strong><\/h2>\n<h2><strong>dict2 = {[1, 2]: &quot;value1&quot;}  # \u4f1a\u5f15\u53d1 TypeError<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5143\u7ec4\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4f46\u5143\u7ec4\u5bf9\u8c61\u4ecd\u7136\u6709\u4e00\u4e9b\u5185\u7f6e\u65b9\u6cd5\uff0c\u4f8b\u5982 <code>count()<\/code> \u548c <code>index()<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5143\u7ec4\u7684\u65b9\u6cd5<\/p>\n<p>tuple1 = (1, 2, 3, 2, 4, 2)<\/p>\n<h2><strong>count() \u65b9\u6cd5<\/strong><\/h2>\n<p>print(tuple1.count(2))  # \u8f93\u51fa 3<\/p>\n<h2><strong>index() \u65b9\u6cd5<\/strong><\/h2>\n<p>print(tuple1.index(3))  # \u8f93\u51fa 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5143\u7ec4\u7684 <code>count()<\/code> \u548c <code>index()<\/code> \u65b9\u6cd5\u3002<code>count()<\/code> \u65b9\u6cd5\u7528\u4e8e\u7edf\u8ba1\u5143\u7ec4\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\uff0c<code>index()<\/code> \u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u67d0\u4e2a\u5143\u7d20\u5728\u5143\u7ec4\u4e2d\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5728Python\u4e2d\u8f93\u5165\u548c\u4f7f\u7528\u5143\u7ec4\u3002\u5143\u7ec4\u4f5c\u4e3a\u4e00\u79cd\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5177\u6709\u9ad8\u6548\u3001\u7b80\u6d01\u548c\u6613\u4e8e\u4f7f\u7528\u7684\u7279\u70b9\u3002\u65e0\u8bba\u662f\u7528\u4e8e\u5b58\u50a8\u7b80\u5355\u7684\u6570\u636e\uff0c\u8fd8\u662f\u8868\u793a\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5143\u7ec4\u90fd\u662f\u4e00\u79cd\u975e\u5e38\u6709\u7528\u7684\u6570\u636e\u7c7b\u578b\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u7406\u89e3\u548c\u4f7f\u7528\u5143\u7ec4\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5143\u7ec4\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u5143\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u5c0f\u62ec\u53f7\u5c06\u4e00\u7ec4\u503c\u5305\u88f9\u8d77\u6765\u3002\u4f8b\u5982\uff0c<code>my_tuple = (1, 2, 3)<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u6574\u6570\u7684\u5143\u7ec4\u3002\u5982\u679c\u4f60\u5e0c\u671b\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u6570\u636e\u7684\u5143\u7ec4\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>mixed_tuple = (1, &quot;hello&quot;, 3.14)<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5143\u7ec4\u548c\u5217\u8868\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>\u5143\u7ec4\u4e0e\u5217\u8868\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u4fee\u6539\u5176\u4e2d\u7684\u5143\u7d20\u3002\u76f8\u8f83\u4e4b\u4e0b\uff0c\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u968f\u610f\u6dfb\u52a0\u3001\u5220\u9664\u6216\u4fee\u6539\u5143\u7d20\u3002\u6b64\u5916\uff0c\u5143\u7ec4\u901a\u5e38\u7528\u4e8e\u5b58\u50a8\u56fa\u5b9a\u7684\u3001\u76f8\u5173\u7684\u6570\u636e\uff0c\u800c\u5217\u8868\u5219\u9002\u5408\u7528\u4e8e\u9700\u8981\u9891\u7e41\u4fee\u6539\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5143\u7ec4<code>my_tuple = (10, 20, 30)<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_tuple[0]<\/code>\u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u8fd4\u56de\u503c\u4e3a10\u3002\u5982\u679c\u9700\u8981\u8bbf\u95ee\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u8d1f\u7d22\u5f15\uff0c<code>my_tuple[-1]<\/code>\u5c06\u8fd4\u56de30\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u5904\u7406\u6570\u636e\u65f6\u975e\u5e38\u7075\u6d3b\uff0c\u65b9\u4fbf\u63d0\u53d6\u9700\u8981\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8f93\u5165\u4e00\u4e2a\u5143\u7ec4\u3001\u4f7f\u7528\u62ec\u53f7\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528\u8fed\u4ee3\u5668\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u62ec\u53f7\u548c\u9017\u53f7\u5206\u9694 [&hellip;]","protected":false},"author":3,"featured_media":1083716,"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\/1083704"}],"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=1083704"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083704\/revisions"}],"predecessor-version":[{"id":1083719,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083704\/revisions\/1083719"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1083716"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1083704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1083704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1083704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}