{"id":994319,"date":"2024-12-27T08:54:22","date_gmt":"2024-12-27T00:54:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994319.html"},"modified":"2024-12-27T08:54:26","modified_gmt":"2024-12-27T00:54:26","slug":"python%e5%a6%82%e4%bd%95%e5%88%97%e8%a1%a8%e8%bd%ac%e4%b8%ba%e5%85%83%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994319.html","title":{"rendered":"python\u5982\u4f55\u5217\u8868\u8f6c\u4e3a\u5143\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071344\/8f623d90-edc9-4340-b6c3-0ec5b880b346.webp\" alt=\"python\u5982\u4f55\u5217\u8868\u8f6c\u4e3a\u5143\u7ec4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528tuple()\u51fd\u6570\u3001\u4f7f\u7528\u661f\u53f7\u8fd0\u7b97\u7b26*\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002<\/strong> \u4f7f\u7528tuple()\u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u76f4\u63a5\u3002\u53ea\u9700\u8981\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9tuple()\u51fd\u6570\u5373\u53ef\u3002\u661f\u53f7\u8fd0\u7b97\u7b26\u53ef\u4ee5\u7528\u4e8e\u5728\u4e0d\u663e\u5f0f\u521b\u5efa\u65b0\u5bf9\u8c61\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u8f6c\u6362\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u6301\u5bf9\u8c61\u4e0d\u53ef\u53d8\u7684\u573a\u666f\u3002\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5219\u9002\u5408\u5728\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u67d0\u79cd\u53d8\u6362\u6216\u8fc7\u6ee4\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528tuple()\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684tuple()\u51fd\u6570\u3002\u6b64\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\uff0c\u800c\u4e14\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u662f\u8db3\u591f\u9ad8\u6548\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4]<\/p>\n<p>my_tuple = tuple(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>my_list<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5217\u8868\uff0c\u800c<code>tuple()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5143\u7ec4<code>my_tuple<\/code>\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u76f4\u63a5\u4e14\u9ad8\u6548\u7684\uff0c\u56e0\u4e3atuple()\u51fd\u6570\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7ecf\u8fc7\u4e86\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u661f\u53f7\u8fd0\u7b97\u7b26*<\/p>\n<\/p>\n<p><p>\u661f\u53f7\u8fd0\u7b97\u7b26\u7528\u4e8e\u89e3\u5305\u5e8f\u5217\uff0c\u53ef\u4ee5\u7528\u4e8e\u5c06\u5217\u8868\u8f6c\u5316\u4e3a\u5143\u7ec4\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5c06\u5217\u8868\u7684\u5143\u7d20\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4]<\/p>\n<p>my_tuple = (*my_list,)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u661f\u53f7\u8fd0\u7b97\u7b26*\u7528\u4e8e\u5c06\u5217\u8868<code>my_list<\/code>\u7684\u5143\u7d20\u89e3\u5305\uff0c\u7136\u540e\u518d\u5c06\u5176\u653e\u5165\u4e00\u4e2a\u65b0\u7684\u5143\u7ec4\u4e2d\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u80fd\u8fbe\u5230\u76ee\u7684\uff0c\u4f46\u76f8\u6bd4\u76f4\u63a5\u4f7f\u7528tuple()\u51fd\u6570\uff0c\u7a0d\u663e\u590d\u6742\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\uff0c\u4f8b\u5982\u9700\u8981\u7ed3\u5408\u591a\u4e2a\u5e8f\u5217\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5143\u7ec4\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u5bf9\u5217\u8868\u8fdb\u884c\u67d0\u79cd\u53d8\u6362\u6216\u8fc7\u6ee4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4]<\/p>\n<p>my_tuple = tuple(x * 2 for x in my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e0d\u4ec5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u4e86\u5143\u7ec4\uff0c\u8fd8\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u4e86\u500d\u6570\u8fd0\u7b97\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u5bf9\u5217\u8868\u5143\u7d20\u8fdb\u884c\u67d0\u79cd\u5904\u7406\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u8f6c\u6362\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u7684\u5e94\u7528\u573a\u666f\u4e3b\u8981\u96c6\u4e2d\u5728\u9700\u8981\u6570\u636e\u4e0d\u53ef\u53d8\u7684\u5730\u65b9\u3002\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u800c\u5217\u8868\u662f\u53ef\u53d8\u7684\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5143\u7ec4\u53ef\u4ee5\u9632\u6b62\u6570\u636e\u88ab\u610f\u5916\u4fee\u6539\u3002\u6b64\u5916\uff0c\u5143\u7ec4\u53ef\u4ee5\u7528\u4f5c\u5b57\u5178\u7684\u952e\uff0c\u800c\u5217\u8868\u4e0d\u53ef\u4ee5\uff0c\u56e0\u4e3a\u5217\u8868\u662f\u53ef\u53d8\u7684\u3002<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5c06\u5217\u8868\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u4f20\u9012\u7684\u60c5\u51b5\uff0c\u4f7f\u7528\u5143\u7ec4\u53ef\u4ee5\u786e\u4fdd\u51fd\u6570\u5185\u90e8\u4e0d\u4f1a\u4fee\u6539\u4f20\u5165\u7684\u6570\u636e\u3002\u8fd9\u79cd\u4e0d\u53ef\u53d8\u6027\u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\u5c24\u5176\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u907f\u514d\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6027\u80fd\u8003\u8651<\/p>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u65b9\u9762\uff0c\u5143\u7ec4\u901a\u5e38\u6bd4\u5217\u8868\u66f4\u8f7b\u91cf\uff0c\u56e0\u4e3a\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0cPython\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u5185\u5b58\u4f18\u5316\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u6027\u80fd\u5dee\u5f02\u901a\u5e38\u53ea\u6709\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u5408\u65f6\u624d\u4f1a\u663e\u73b0\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u9009\u62e9\u4f7f\u7528\u5217\u8868\u8fd8\u662f\u5143\u7ec4\u5e94\u8be5\u57fa\u4e8e\u6570\u636e\u7684\u6027\u8d28\u548c\u9700\u6c42\uff0c\u800c\u4e0d\u662f\u6027\u80fd\u3002\u53ea\u6709\u5728\u7279\u5b9a\u7684\u6027\u80fd\u74f6\u9888\u60c5\u51b5\u4e0b\uff0c\u624d\u9700\u8981\u4ed4\u7ec6\u5206\u6790\u4e24\u8005\u7684\u6027\u80fd\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5176\u4ed6\u9ad8\u7ea7\u7528\u6cd5<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5d4c\u5957\u7ed3\u6784\u7684\u8f6c\u6362\uff1a<\/strong> \u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u5d4c\u5957\u5217\u8868\uff0c\u53ef\u4ee5\u9012\u5f52\u5730\u5c06\u6bcf\u4e2a\u5d4c\u5957\u5217\u8868\u4e5f\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_nested_list_to_tuple(nested_list):<\/p>\n<p>    return tuple(convert_nested_list_to_tuple(x) if isinstance(x, list) else x for x in nested_list)<\/p>\n<p>my_list = [1, [2, 3], 4]<\/p>\n<p>my_tuple = convert_nested_list_to_tuple(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u8f6c\u6362\uff1a<\/strong> \u6709\u65f6\u9700\u8981\u5728\u591a\u79cd\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u8f6c\u6362\uff0c\u4f8b\u5982\u4ece\u5b57\u5178\u7684\u503c\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u5b57\u5178\u7684\u65b9\u6cd5\u548ctuple()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: [1, 2], &#39;b&#39;: [3, 4]}<\/p>\n<p>my_tuple_dict = {k: tuple(v) for k, v in my_dict.items()}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001\u5e38\u89c1\u8bef\u533a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bef\u89e3\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\uff1a<\/strong> \u867d\u7136\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4f46\u5982\u679c\u5143\u7ec4\u5305\u542b\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5217\u8868\uff09\uff0c\u8fd9\u4e9b\u53ef\u53d8\u5bf9\u8c61\u7684\u5185\u5bb9\u4ecd\u7136\u53ef\u4ee5\u88ab\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_tuple = (1, [2, 3])<\/p>\n<p>my_tuple[1][0] = 9  # \u8fd9\u662f\u5141\u8bb8\u7684<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76f4\u63a5\u4f7f\u7528list()\u51fd\u6570\uff1a<\/strong> \u6709\u4e9b\u4eba\u53ef\u80fd\u4f1a\u8bef\u7528list()\u800c\u4e0d\u662ftuple()\u8fdb\u884c\u8f6c\u6362\uff0c\u8fd9\u5c06\u5bfc\u81f4\u751f\u6210\u5217\u8868\u800c\u975e\u5143\u7ec4\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5728Python\u4e2d\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002\u9009\u62e9\u9002\u5408\u7684\u65b9\u5f0f\u5e94\u5f53\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>tuple()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002\u53ea\u9700\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>tuple()<\/code>\u51fd\u6570\u5373\u53ef\u3002\u4f8b\u5982\uff0c<code>my_list = [1, 2, 3]<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>my_tuple = tuple(my_list)<\/code>\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u83b7\u5f97<code>my_tuple<\/code>\u4e3a <code>(1, 2, 3)<\/code> \u7684\u5143\u7ec4\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684\u5143\u7ec4\u4e0e\u539f\u5217\u8868\u76f8\u6bd4\u6709\u54ea\u4e9b\u4e0d\u540c\uff1f<\/strong><br \/>\u5143\u7ec4\u4e0e\u5217\u8868\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u53ef\u53d8\u6027\u3002\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u968f\u610f\u4fee\u6539\uff0c\u800c\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\u3002\u4e00\u65e6\u521b\u5efa\uff0c\u5143\u7ec4\u7684\u5185\u5bb9\u5c31\u65e0\u6cd5\u66f4\u6539\uff0c\u8fd9\u4f7f\u5f97\u5143\u7ec4\u5728\u9700\u8981\u786e\u4fdd\u6570\u636e\u4e0d\u88ab\u4fee\u6539\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u5143\u7ec4\u800c\u4e0d\u662f\u5217\u8868\uff1f<\/strong><br \/>\u5143\u7ec4\u9002\u7528\u4e8e\u9700\u8981\u5b58\u50a8\u56fa\u5b9a\u96c6\u5408\u7684\u6570\u636e\uff0c\u7279\u522b\u662f\u5728\u4e0d\u5e0c\u671b\u6570\u636e\u88ab\u610f\u5916\u4fee\u6539\u7684\u60c5\u51b5\u4e0b\u3002\u6bd4\u5982\uff0c\u5f53\u4f60\u9700\u8981\u5c06\u4e00\u7ec4\u76f8\u5173\u7684\u6570\u636e\u4f5c\u4e3a\u4e00\u4e2a\u5355\u4e00\u7684\u503c\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\uff0c\u5143\u7ec4\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u6b64\u5916\uff0c\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\u4f7f\u5176\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff08\u5982\u7528\u4f5c\u5b57\u5178\u7684\u952e\uff09\u5177\u6709\u4f18\u52bf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528tuple()\u51fd\u6570\u3001\u4f7f\u7528\u661f\u53f7\u8fd0\u7b97\u7b26*\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7b49\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5143\u7ec4\u3002 \u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":994327,"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\/994319"}],"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=994319"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994319\/revisions"}],"predecessor-version":[{"id":994329,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994319\/revisions\/994329"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994327"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}