{"id":1177824,"date":"2025-01-15T18:02:50","date_gmt":"2025-01-15T10:02:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1177824.html"},"modified":"2025-01-15T18:02:53","modified_gmt":"2025-01-15T10:02:53","slug":"python%e5%85%83%e7%bb%84%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e4%b8%8e%e5%88%a0%e9%99%a4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1177824.html","title":{"rendered":"python\u5143\u7ec4\u5982\u4f55\u521b\u5efa\u4e0e\u5220\u9664"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25112637\/a1900f4b-911a-4239-a6af-962e3d296799.webp\" alt=\"python\u5143\u7ec4\u5982\u4f55\u521b\u5efa\u4e0e\u5220\u9664\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7684\u5143\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5c0f\u62ec\u53f7<code>()<\/code>\u6216\u8005<code>tuple()<\/code>\u51fd\u6570\u8fdb\u884c\u521b\u5efa\u3001\u5143\u7ec4\u7684\u5143\u7d20\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u53ef\u4fee\u6539\u3001\u5220\u9664\u5143\u7ec4\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5<\/strong>\uff0c\u5177\u4f53\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u5143\u7ec4\u7684\u57fa\u672c\u65b9\u6cd5\u662f\u4f7f\u7528\u5c0f\u62ec\u53f7\u62ec\u8d77\u6765\u7684\u9017\u53f7\u5206\u9694\u7684\u5143\u7d20\u5e8f\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (1, 2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>tuple()<\/code>\u51fd\u6570\u5c06\u5176\u4ed6\u5e8f\u5217\u7c7b\u578b\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3]<\/p>\n<p>my_tuple = tuple(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5143\u7ec4\u4e00\u65e6\u521b\u5efa\uff0c\u5176\u5143\u7d20\u662f\u4e0d\u53ef\u4fee\u6539\u7684\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u975e\u5e38\u9002\u5408\u4f5c\u4e3a\u4e0d\u53d8\u7684\u6570\u636e\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><p>\u8981\u5220\u9664\u6574\u4e2a\u5143\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (1, 2, 3)<\/p>\n<p>del my_tuple<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5b8c\u5168\u5220\u9664<code>my_tuple<\/code>\uff0c\u5e76\u4e14\u4e4b\u540e\u518d\u8bbf\u95ee<code>my_tuple<\/code>\u4f1a\u5f15\u53d1<code>NameError<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5143\u7ec4\u7684\u521b\u5efa<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u5143\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u5c0f\u62ec\u53f7<\/h4>\n<\/p>\n<p><p>\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u521b\u5efa\u5143\u7ec4\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u76f4\u63a5\u5c06\u5143\u7d20\u653e\u5728\u5c0f\u62ec\u53f7\u5185\uff0c\u4ee5\u9017\u53f7\u5206\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tuple1 = (1, 2, 3)<\/p>\n<p>tuple2 = (&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5355\u5143\u7d20\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5143\u7ec4\u4e2d\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u5fc5\u987b\u5728\u5143\u7d20\u540e\u9762\u52a0\u4e00\u4e2a\u9017\u53f7\uff0c\u5426\u5219\u5b83\u5c06\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u666e\u901a\u7684\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">single_element_tuple = (1,)<\/p>\n<p>not_a_tuple = (1)  # \u8fd9\u53ea\u662f\u4e00\u4e2a\u6574\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u65e0\u5143\u7d20\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7a7a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">empty_tuple = ()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4f7f\u7528tuple()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>tuple()<\/code>\u51fd\u6570\u5c06\u5176\u4ed6\u5e8f\u5217\u7c7b\u578b\uff08\u5982\u5217\u8868\u3001\u5b57\u7b26\u4e32\uff09\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>tuple_from_list = tuple(list1)<\/p>\n<p>string1 = &quot;hello&quot;<\/p>\n<p>tuple_from_string = tuple(string1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5143\u7ec4\u7684\u7279\u6027<\/h3>\n<\/p>\n<p><h4>1\u3001\u4e0d\u53ef\u53d8\u6027<\/h4>\n<\/p>\n<p><p>\u5143\u7ec4\u4e00\u65e6\u521b\u5efa\uff0c\u5176\u5185\u5bb9\u5c31\u65e0\u6cd5\u4fee\u6539\u3002\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u80fd\u5728\u521b\u5efa\u540e\u66f4\u6539\u3001\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u4e0d\u53ef\u53d8\u6027\u4f7f\u5f97\u5143\u7ec4\u5728\u9700\u8981\u4e00\u7ec4\u5e38\u91cf\u503c\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u7528\u5143\u7ec4\u6765\u8868\u793a\u4e00\u7ec4\u4e0d\u53d8\u7684\u914d\u7f6e\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u5143\u7ec4\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mixed_tuple = (1, &quot;apple&quot;, 3.14)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5d4c\u5957<\/h4>\n<\/p>\n<p><p>\u5143\u7ec4\u53ef\u4ee5\u5d4c\u5957\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5143\u7ec4\u5185\u5305\u542b\u53e6\u4e00\u4e2a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_tuple = ((1, 2), (3, 4))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbf\u95ee\u5143\u7ec4\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (1, 2, 3, 4)<\/p>\n<p>print(my_tuple[0])  # \u8f93\u51fa1<\/p>\n<p>print(my_tuple[3])  # \u8f93\u51fa4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8d1f\u7d22\u5f15\u4ece\u5143\u7ec4\u7684\u672b\u5c3e\u5f00\u59cb\u8bbf\u95ee\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(my_tuple[-1])  # \u8f93\u51fa4<\/p>\n<p>print(my_tuple[-2])  # \u8f93\u51fa3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5143\u7ec4\u7684\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5143\u7ec4\u7684\u5143\u7d20\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4f46\u4f60\u53ef\u4ee5\u5bf9\u5143\u7ec4\u8fdb\u884c\u4e00\u4e9b\u57fa\u672c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u52a0\u53f7<code>+<\/code>\u64cd\u4f5c\u7b26\u6765\u8fde\u63a5\u4e24\u4e2a\u6216\u591a\u4e2a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tuple1 = (1, 2)<\/p>\n<p>tuple2 = (3, 4)<\/p>\n<p>tuple3 = tuple1 + tuple2<\/p>\n<p>print(tuple3)  # \u8f93\u51fa(1, 2, 3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u91cd\u590d<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e58\u53f7<code>*<\/code>\u64cd\u4f5c\u7b26\u6765\u91cd\u590d\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tuple1 = (1, 2)<\/p>\n<p>tuple2 = tuple1 * 3<\/p>\n<p>print(tuple2)  # \u8f93\u51fa(1, 2, 1, 2, 1, 2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u7b26<code>:<\/code>\u6765\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u4e00\u90e8\u5206\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (1, 2, 3, 4, 5)<\/p>\n<p>print(my_tuple[1:3])  # \u8f93\u51fa(2, 3)<\/p>\n<p>print(my_tuple[:3])   # \u8f93\u51fa(1, 2, 3)<\/p>\n<p>print(my_tuple[3:])   # \u8f93\u51fa(4, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5143\u7ec4\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4f46\u5b83\u4eec\u4ecd\u7136\u6709\u4e00\u4e9b\u5185\u7f6e\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001count()<\/h4>\n<\/p>\n<p><p><code>count()<\/code>\u65b9\u6cd5\u7528\u4e8e\u8ba1\u7b97\u67d0\u4e2a\u5143\u7d20\u5728\u5143\u7ec4\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (1, 2, 3, 1, 2, 1)<\/p>\n<p>print(my_tuple.count(1))  # \u8f93\u51fa3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001index()<\/h4>\n<\/p>\n<p><p><code>index()<\/code>\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u67d0\u4e2a\u5143\u7d20\u5728\u5143\u7ec4\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (1, 2, 3, 1, 2, 1)<\/p>\n<p>print(my_tuple.index(3))  # \u8f93\u51fa2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5143\u7ec4\u7684\u5220\u9664<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5143\u7ec4\u7684\u5143\u7d20\u4e0d\u53ef\u53d8\uff0c\u4f46\u4f60\u53ef\u4ee5\u5220\u9664\u6574\u4e2a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528del\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u5220\u9664\u6574\u4e2a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (1, 2, 3)<\/p>\n<p>del my_tuple<\/p>\n<h2><strong>\u5c1d\u8bd5\u8bbf\u95eemy_tuple\u5c06\u5f15\u53d1NameError\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u88ab\u5220\u9664<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5143\u7ec4\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u5728Python\u7f16\u7a0b\u4e2d\u6709\u8bb8\u591a\u5e94\u7528\u573a\u666f\uff0c\u4e0b\u9762\u5217\u51fa\u4e86\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f5c\u4e3a\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u4f5c\u5b58\u50a8\u5e38\u91cf\u503c\u7684\u5b89\u5168\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u7528\u5143\u7ec4\u6765\u5b58\u50a8\u4e00\u7ec4\u914d\u7f6e\u53c2\u6570\u3001\u56fa\u5b9a\u7684\u5750\u6807\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c<\/h4>\n<\/p>\n<p><p>\u5143\u7ec4\u5e38\u7528\u4e8e\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002\u901a\u8fc7\u5c06\u591a\u4e2a\u8fd4\u56de\u503c\u6253\u5305\u6210\u5143\u7ec4\uff0c\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u591a\u4e2a\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_coordinates():<\/p>\n<p>    return (40.7128, 74.0060)  # \u8fd4\u56de\u7ebd\u7ea6\u5e02\u7684\u7ecf\u7eac\u5ea6<\/p>\n<p>coordinates = get_coordinates()<\/p>\n<p>print(coordinates)  # \u8f93\u51fa(40.7128, 74.0060)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f5c\u4e3a\u952e\u503c\u5bf9<\/h4>\n<\/p>\n<p><p>\u5728\u5b57\u5178\u4e2d\uff0c\u5143\u7ec4\u53ef\u4ee5\u7528\u4f5c\u952e\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u4e0d\u53ef\u53d8\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {(1, 2): &quot;a&quot;, (3, 4): &quot;b&quot;}<\/p>\n<p>print(my_dict[(1, 2)])  # \u8f93\u51fa&quot;a&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4e0e\u5217\u8868\u7684\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u548c\u5217\u8868\u90fd\u662fPython\u4e2d\u7528\u4e8e\u5b58\u50a8\u5e8f\u5217\u6570\u636e\u7684\u5e38\u7528\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u5b83\u4eec\u6709\u4e00\u4e9b\u5173\u952e\u533a\u522b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u53ef\u53d8\u6027<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5728\u521b\u5efa\u540e\u4fee\u6539\u5176\u5143\u7d20\u3002\u800c\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u4fee\u6539\u5176\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u5b83\u4eec\u901a\u5e38\u6bd4\u5217\u8868\u66f4\u8f7b\u91cf\u7ea7\u548c\u9ad8\u6548\u3002\u5982\u679c\u4f60\u6709\u4e00\u7ec4\u4e0d\u9700\u8981\u4fee\u6539\u7684\u6570\u636e\uff0c\u4f7f\u7528\u5143\u7ec4\u53ef\u80fd\u4f1a\u6bd4\u5217\u8868\u66f4\u597d\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u7528\u9014<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u901a\u5e38\u7528\u4e8e\u9700\u8981\u9891\u7e41\u4fee\u6539\u7684\u5e8f\u5217\u6570\u636e\uff0c\u800c\u5143\u7ec4\u5219\u7528\u4e8e\u5b58\u50a8\u4e0d\u53d8\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5143\u7ec4\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u8fd8\u53ef\u4ee5\u7528\u4e8e\u4e00\u4e9b\u9ad8\u7ea7\u7684Python\u7f16\u7a0b\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u89e3\u5305<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u89e3\u5305\u64cd\u4f5c\u5c06\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u76f4\u63a5\u8d4b\u503c\u7ed9\u591a\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (1, 2, 3)<\/p>\n<p>a, b, c = my_tuple<\/p>\n<p>print(a, b, c)  # \u8f93\u51fa1 2 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4ea4\u6362\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5143\u7ec4\u89e3\u5305\uff0c\u4f60\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 1<\/p>\n<p>b = 2<\/p>\n<p>a, b = b, a<\/p>\n<p>print(a, b)  # \u8f93\u51fa2 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f5c\u4e3a\u96c6\u5408\u7684\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528\u4f5c\u96c6\u5408\uff08set\uff09\u6216\u5b57\u5178\uff08dictionary\uff09\u7684\u952e\uff0c\u800c\u5217\u8868\u5219\u4e0d\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = {(1, 2), (3, 4)}<\/p>\n<p>print((1, 2) in my_set)  # \u8f93\u51faTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u5143\u7ec4\u7684\u5185\u5b58\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u5728\u5185\u5b58\u7ba1\u7406\u65b9\u9762\u6709\u4e00\u4e9b\u4f18\u52bf\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5143\u7ec4\u65f6\uff0cPython\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5206\u914d\u5185\u5b58\uff0c\u56e0\u4e3a\u5b83\u77e5\u9053\u5143\u7ec4\u7684\u5927\u5c0f\u4e0d\u4f1a\u6539\u53d8\u3002\u8fd9\u4f7f\u5f97\u5143\u7ec4\u5728\u9700\u8981\u9ad8\u6548\u5185\u5b58\u7ba1\u7406\u7684\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u662fPython\u4e2d\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u5b58\u50a8\u4e0d\u53ef\u53d8\u6570\u636e\u3002\u901a\u8fc7\u7406\u89e3\u5143\u7ec4\u7684\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u5e94\u7528\u573a\u666f\uff0c\u4f60\u53ef\u4ee5\u5728\u7f16\u7a0b\u4e2d\u66f4\u9ad8\u6548\u5730\u4f7f\u7528\u5b83\u4eec\u3002\u8bb0\u4f4f\uff0c<strong>\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\u662f\u5176\u6700\u5927\u7684\u7279\u70b9<\/strong>\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u5728\u8bb8\u591a\u9700\u8981\u6570\u636e\u4e0d\u53ef\u53d8\u7684\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5143\u7ec4\u7684\u6700\u4f73\u5b9e\u8df5\u5305\u62ec\uff1a\u5229\u7528\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\u6765\u5b58\u50a8\u5e38\u91cf\u503c\u3001\u5c06\u5143\u7ec4\u7528\u4f5c\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3001\u4ee5\u53ca\u5728\u9700\u8981\u9ad8\u6548\u5185\u5b58\u7ba1\u7406\u7684\u573a\u666f\u4e2d\u4f7f\u7528\u5143\u7ec4\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528\u5143\u7ec4\u6765\u7f16\u5199\u9ad8\u6548\u3001\u53ef\u9760\u7684Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5143\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u5143\u7ec4\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u5c0f\u62ec\u53f7\u5c06\u591a\u4e2a\u5143\u7d20\u5305\u88f9\u8d77\u6765\uff0c\u5143\u7d20\u4e4b\u95f4\u7528\u9017\u53f7\u5206\u9694\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\u6b64\u5916\uff0c\u5143\u7ec4\u4e5f\u53ef\u4ee5\u53ea\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff0c\u9700\u5728\u5143\u7d20\u540e\u52a0\u9017\u53f7\uff0c\u4f8b\u5982 <code>single_element_tuple = (1,)<\/code>\u3002\u5982\u679c\u4e0d\u52a0\u9017\u53f7\uff0cPython\u4f1a\u5c06\u5176\u89c6\u4e3a\u666e\u901a\u7684\u6574\u6570\u800c\u4e0d\u662f\u5143\u7ec4\u3002<\/p>\n<p><strong>2. \u5143\u7ec4\u4e0e\u5217\u8868\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u5143\u7ec4\uff1f<\/strong><br \/>\u5143\u7ec4\u4e0e\u5217\u8868\u5728\u8bb8\u591a\u65b9\u9762\u76f8\u4f3c\uff0c\u4f46\u6709\u4e00\u4e9b\u5173\u952e\u533a\u522b\u3002\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\uff0c\u4e0d\u80fd\u4fee\u6539\u5176\u5185\u5bb9\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u5141\u8bb8\u6dfb\u52a0\u3001\u5220\u9664\u6216\u66f4\u6539\u5143\u7d20\u3002\u4f7f\u7528\u5143\u7ec4\u7684\u4e00\u4e2a\u4e3b\u8981\u539f\u56e0\u662f\u5b83\u4eec\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u4fdd\u62a4\uff0c\u786e\u4fdd\u6570\u636e\u4e0d\u4f1a\u88ab\u610f\u5916\u4fee\u6539\u3002\u6b64\u5916\uff0c\u5143\u7ec4\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6bd4\u5217\u8868\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u5185\u5b58\u4f7f\u7528\u66f4\u5c11\uff0c\u64cd\u4f5c\u901f\u5ea6\u66f4\u5feb\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5220\u9664\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5176\u4e2d\u7684\u5143\u7d20\u3002\u5982\u679c\u9700\u8981\u53bb\u6389\u5143\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u5220\u9664\u5143\u7d20\u540e\u518d\u5c06\u5176\u8f6c\u6362\u56de\u5143\u7ec4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>my_list = list(my_tuple)<\/code> \u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528 <code>my_list.remove(element)<\/code> \u5220\u9664\u6307\u5b9a\u5143\u7d20\u3002\u5b8c\u6210\u540e\uff0c\u901a\u8fc7 <code>new_tuple = tuple(my_list)<\/code> \u5c06\u5217\u8868\u8f6c\u6362\u56de\u5143\u7ec4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u6837\u7684\u64cd\u4f5c\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5143\u7ec4\uff0c\u800c\u4e0d\u4f1a\u4fee\u6539\u539f\u59cb\u5143\u7ec4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684\u5143\u7ec4\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5c0f\u62ec\u53f7()\u6216\u8005tuple()\u51fd\u6570\u8fdb\u884c\u521b\u5efa\u3001\u5143\u7ec4\u7684\u5143\u7d20\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u53ef\u4fee\u6539\u3001\u5220\u9664\u5143 [&hellip;]","protected":false},"author":3,"featured_media":1177829,"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\/1177824"}],"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=1177824"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177824\/revisions"}],"predecessor-version":[{"id":1177831,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177824\/revisions\/1177831"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1177829"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1177824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1177824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1177824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}