{"id":3240,"date":"2025-06-03T16:27:40","date_gmt":"2025-06-04T08:30:39","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3240"},"modified":"2025-06-04T16:27:40","modified_gmt":"2025-06-04T08:30:39","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84tuple%ef%bc%9a%e7%b0%a1%e5%96%ae%e6%98%93%e7%94%a8%e7%9a%84%e8%b3%87%e6%96%99%e7%b5%90%e6%a7%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84tuple%ef%bc%9a%e7%b0%a1%e5%96%ae%e6%98%93%e7%94%a8%e7%9a%84%e8%b3%87%e6%96%99%e7%b5%90%e6%a7%8b\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 Tuple\uff1a\u7c21\u55ae\u6613\u7528\u7684\u8cc7\u6599\u7d50\u69cb\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, tuple, tuple \u51fd\u5f0f, Python \u8cc7\u6599\u7d50\u69cb, Python \u6559\u5b78\"><\/p>\n<h1>Python \u4e2d\u7684 tuple() \u51fd\u5f0f<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`tuple()` \u51fd\u5f0f\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5167\u5efa\u51fd\u5f0f\uff0c\u80fd\u5920\u5c07\u4efb\u4f55\u53ef\u8fed\u4ee3\u7684\u5e8f\u5217\u8f49\u63db\u70ba\u4e00\u500b\u5143\u7d44\uff08tuple\uff09\u3002\u5143\u7d44\u662f\u4e00\u7a2e\u4e0d\u53ef\u8b8a\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u9019\u610f\u5473\u8457\u4e00\u65e6\u5275\u5efa\uff0c\u5143\u7d44\u7684\u5167\u5bb9\u548c\u9577\u5ea6\u5c31\u7121\u6cd5\u66f4\u6539\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e `tuple()` \u51fd\u5f0f\u7684\u7528\u6cd5\u3001\u512a\u9ede\uff0c\u4ee5\u53ca\u5e38\u898b\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<h2>tuple() \u51fd\u5f0f\u7684\u8a9e\u6cd5<\/h2>\n<p>`tuple()` \u51fd\u5f0f\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python\">\ntuple(iterable)\n<\/pre>\n<p>\u5176\u4e2d\uff0c`iterable` \u53c3\u6578\u53ef\u4ee5\u662f\u4efb\u4f55\u53ef\u8fed\u4ee3\u7684\u5c0d\u8c61\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5b57\u4e32\u3001\u5b57\u5178\u7b49\u3002<\/p>\n<h2>Python tuple() \u51fd\u5f0f\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u7bc4\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `tuple()` \u51fd\u5f0f\u5c07\u5217\u8868\u548c\u5b57\u4e32\u8f49\u63db\u70ba\u5143\u7d44\uff1a<\/p>\n<p><strong>\u7bc4\u4f8b 1\uff1a\u5c07\u5217\u8868\u8f49\u63db\u70ba\u5143\u7d44<\/strong><\/p>\n<pre class=\"brush: python\">\n# \u5b9a\u7fa9\u4e00\u500b\u5217\u8868\nlist1 = [1, 2, 3, 4]\n\n# \u5c07\u5217\u8868\u8f49\u63db\u6210\u5143\u7d44\ntuple1 = tuple(list1)\n\n# \u986f\u793a\u5143\u7d44\nprint(tuple1)\n<\/pre>\n<p>\u57f7\u884c\u4e0a\u9762\u7684\u7a0b\u5f0f\uff0c\u8f38\u51fa\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python\">\n(1, 2, 3, 4)\n<\/pre>\n<p><strong>\u7bc4\u4f8b 2\uff1a\u5c07\u5b57\u4e32\u8f49\u63db\u70ba\u5143\u7d44<\/strong><\/p>\n<pre class=\"brush: python\">\n# \u5b9a\u7fa9\u4e00\u500b\u5b57\u4e32\nstr1 = \"Hello World\"\n\n# \u5c07\u5b57\u4e32\u8f49\u63db\u6210\u5143\u7d44\ntuple2 = tuple(str1)\n\n# \u986f\u793a\u5143\u7d44\nprint(tuple2)\n<\/pre>\n<p>\u57f7\u884c\u4e0a\u9762\u7684\u7a0b\u5f0f\uff0c\u8f38\u51fa\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python\">\n('H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd')\n<\/pre>\n<p>\u9019\u88e1\u53ef\u4ee5\u770b\u5230\uff0c`tuple()` \u51fd\u5f0f\u5c07\u5b57\u4e32\u4e2d\u7684\u6bcf\u500b\u5b57\u5143\u90fd\u8f49\u63db\u70ba\u4e00\u500b\u5143\u7d44\u5143\u7d20\u3002<\/p>\n<h2>Python tuple() \u51fd\u5f0f\u7684\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 `tuple()` \u51fd\u5f0f\u6709\u4ee5\u4e0b\u5e7e\u500b\u512a\u9ede\uff1a<br \/>\n1. **\u4e0d\u53ef\u8b8a\u6027**\uff1a\u5143\u7d44\u4e00\u65e6\u5275\u5efa\u5c31\u7121\u6cd5\u6539\u8b8a\uff0c\u9019\u4f7f\u5f97\u5b83\u66f4\u5b89\u5168\uff0c\u7279\u5225\u662f\u5728\u591a\u57f7\u884c\u7dd2\u74b0\u5883\u4e2d\u3002<br \/>\n2. **\u6027\u80fd\u512a\u52e2**\uff1a\u5143\u7d44\u76f8\u6bd4\u65bc\u5217\u8868\u4f54\u7528\u66f4\u5c11\u7684\u8a18\u61b6\u9ad4\uff0c\u4e14\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\u57f7\u884c\u901f\u5ea6\u66f4\u5feb\u3002<br \/>\n3. **\u53ef\u7528\u4f5c\u5b57\u5178\u9375**\uff1a\u5143\u7d44\u53ef\u4ee5\u4f5c\u70ba\u5b57\u5178\u7684\u9375\uff0c\u56e0\u70ba\u5b83\u662f\u4e0d\u53ef\u8b8a\u7684\uff0c\u800c\u5217\u8868\u5247\u4e0d\u53ef\u4ee5\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u5e38\u898b\u554f\u984c<\/h2>\n<p>\u7576\u4f7f\u7528 `tuple()` \u51fd\u5f0f\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u554f\u984c\uff0c\u4f8b\u5982\uff1a<br \/>\n&#8211; **TypeError**\uff1a\u5982\u679c\u50b3\u5165\u7684\u53c3\u6578\u4e0d\u662f\u53ef\u8fed\u4ee3\u5c0d\u8c61\uff0c\u5c07\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u78ba\u4fdd\u60a8\u50b3\u5165\u7684\u662f\u5217\u8868\u3001\u5b57\u4e32\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5c0d\u8c61\u3002<br \/>\n&#8211; **\u7a7a\u5143\u7d44**\uff1a\u8abf\u7528 `tuple()` \u51fd\u5f0f\u800c\u4e0d\u50b3\u5165\u4efb\u4f55\u53c3\u6578\uff0c\u5c07\u8fd4\u56de\u4e00\u500b\u7a7a\u5143\u7d44 `()`\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5143\u7d44\u5728 Python \u958b\u767c\u4e2d\u6709\u591a\u7a2e\u61c9\u7528\uff0c\u5305\u62ec\uff1a<br \/>\n&#8211; \u4f5c\u70ba\u51fd\u5f0f\u8fd4\u56de\u591a\u500b\u503c\u7684\u6709\u6548\u65b9\u5f0f\u3002<br \/>\n&#8211; \u5728\u8cc7\u6599\u5eab\u67e5\u8a62\u4e2d\uff0c\u4f7f\u7528\u5143\u7d44\u4f86\u5b58\u5132\u67e5\u8a62\u7d50\u679c\u3002<br \/>\n&#8211; \u4f5c\u70ba\u4e0d\u53ef\u8b8a\u7684\u8cc7\u6599\u7d50\u69cb\u5728\u51fd\u5f0f\u9593\u50b3\u905e\u8cc7\u6599\u3002<\/p>\n<p>\u60f3\u4e86\u89e3\u66f4\u591a Python \u6280\u8853\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003 [vocus.cc \u7684 Python \u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc) \u6216 [miner.tw \u7684 Python \u7bc4\u4f8b](https:\/\/miner.tw)\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86 Python \u4e2d\u7684 `tuple()` \u51fd\u5f0f\uff0c\u5b83\u80fd\u5920\u5c07\u4e00\u500b\u5e8f\u5217\u8f49\u63db\u6210\u4e00\u500b\u5143\u7d44\u3002\u6211\u5011\u63a2\u8a0e\u4e86\u5176\u8a9e\u6cd5\u3001\u7bc4\u4f8b\u3001\u512a\u9ede\u53ca\u5e38\u898b\u554f\u984c\uff0c\u70ba\u60a8\u63d0\u4f9b\u4e86\u4e00\u500b\u5b8c\u6574\u7684\u5b78\u7fd2\u8cc7\u6e90\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p><strong>Q1: tuple \u548c list \u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/strong><br \/>\nA1: tuple \u662f\u4e0d\u53ef\u8b8a\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u800c list \u662f\u53ef\u8b8a\u7684\u3002\u9019\u610f\u5473\u8457 tuple \u7684\u5167\u5bb9\u4e0d\u80fd\u66f4\u6539\uff0c\u800c list \u53ef\u4ee5\u96a8\u6642\u6dfb\u52a0\u6216\u522a\u9664\u5143\u7d20\u3002<\/p>\n<p><strong>Q2: \u5982\u4f55\u5c07 tuple \u8f49\u63db\u70ba list\uff1f<\/strong><br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `list()` \u51fd\u5f0f\u5c07 tuple \u8f49\u63db\u70ba list\uff0c\u4f8b\u5982\uff1a`list(tuple1)`\u3002<\/p>\n<p><strong>Q3: \u5728\u4ec0\u9ebc\u60c5\u6cc1\u4e0b\u4f7f\u7528 tuple \u800c\u4e0d\u662f list\uff1f<\/strong><br \/>\nA3: \u7576\u60a8\u9700\u8981\u4e00\u500b\u4e0d\u6703\u6539\u8b8a\u7684\u8cc7\u6599\u96c6\u5408\uff0c\u4e26\u4e14\u5e0c\u671b\u63d0\u9ad8\u6027\u80fd\u6642\uff0c\u4f7f\u7528 tuple \u662f\u4e00\u500b\u597d\u7684\u9078\u64c7\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2Python\u4e2d\u7684Tuple\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u8cc7\u6599\uff0c\u4e26\u66f4\u6709\u6548\u5730\u4f7f\u7528\u5b83\u5011\u3002\u672c\u6587\u5c07\u4ecb\u7d39Tuple\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u512a\u5316\u4f60\u7684\u7a0b\u5f0f\u78bc\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3240","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Qg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3240"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3240\/revisions"}],"predecessor-version":[{"id":12892,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3240\/revisions\/12892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}