{"id":2414,"date":"2015-07-25T23:53:25","date_gmt":"2015-07-25T15:53:25","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2414"},"modified":"2015-07-25T23:53:25","modified_gmt":"2015-07-25T15:53:25","slug":"%e5%9c%a8%e5%88%9b%e5%bb%balisttuple%e6%97%b6%e5%a4%9a%e5%86%99%e4%b8%80%e4%b8%aa%e9%80%97%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2414.html","title":{"rendered":"\u5728\u521b\u5efalist\/tuple\u65f6\u591a\u5199\u4e00\u4e2a\u9017\u53f7"},"content":{"rendered":"<h6>\u7f18\u7531\uff1a<\/h6>\n<p>\u5728\u770b\u4e00\u4e9bPython\u4ee3\u7801\u7684\u65f6\u5019\u4f1a\u53d1\u73b0\u6709\u4eba\u5728\u521b\u5efalist\/tuple\u7684\u65f6\u5019\u4f1a\u5728\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\u9762\u6dfb\u52a0\u4e00\u4e2a&#8217;\u9017\u53f7&#8217;\uff0c\u800c\u4e14\u4e5f\u4e0d\u62a5\u9519\uff0c\u5bf9\u6709\u4e00\u70b9\u5f3a\u8feb\u75c7\u7684\u6211\u6765\u8bf4\u8fd9\u4e0d\u53ef\u5fcd\u53d7\uff0c\u9700\u8981\u5220\u6389\u3002\u4e0d\u8fc7\u540e\u6765\u60f3\u60f3\u65e2\u7136\u522b\u4eba\u8fd9\u4e48\u5199\u5e94\u8be5\u8fd8\u662f\u6709\u4e00\u5b9a\u9053\u7406\u7684\uff0c\u4e8e\u662f\uff0c\u5c31\u6709\u4e86\u4e0b\u6587\u3002<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>python create a list with one more comma<\/li>\n<li>python create a tuple with one more comma<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/11597901\/why-does-python-allow-a-trailing-comma-in-list\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/11597901\/why-does-python-allow-a-trailing-comma-in-list<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/tutorial\/datastructures.html\" target=\"_blank\">https:\/\/docs.python.org\/2\/tutorial\/datastructures.html<\/a><\/li>\n<li><a href=\"https:\/\/wiki.python.org\/moin\/TupleSyntax\" target=\"_blank\">https:\/\/wiki.python.org\/moin\/TupleSyntax<\/a><\/li>\n<li><a href=\"http:\/\/www.dotnetperls.com\/tuple-python\" target=\"_blank\">http:\/\/www.dotnetperls.com\/tuple-python<\/a><\/li>\n<li><a href=\"http:\/\/www.pythonlearn.com\/html-009\/book011.html\" target=\"_blank\">http:\/\/www.pythonlearn.com\/html-009\/book011.html<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<p>\u9996\u5148\uff0c\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2alist\/tuple\u65f6\uff0c\u5728\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\u9762\u6dfb\u52a0\u4e00\u4e2a&#8217;\u9017\u53f7&#8217;\u662f\u6ca1\u95ee\u9898\u7684\uff1b<\/p>\n<p>\u5176\u6b21\uff0c\u8fd9\u6837\u505a\u7684\u597d\u5904\u5728\u4e8e\uff1a\u5728\u7f16\u8f91\u201c\u591a\u884c\u5143\u7d20\u7684list\u201d\u65f6\u66f4\u4e0d\u5bb9\u6613\u72af\u9519(\u5fd8\u8bb0\u6dfb\u52a0\/\u5220\u9664&#8217;\u9017\u53f7&#8217;)\uff1b\u4ee5\u53ca\u5728diff\u4ee3\u7801\u65f6\u66f4\u597d\u7684\u89c2\u611f\u3002<\/p>\n<p>=<\/p>\n<p>The main advantages are that it makes multi-line lists <span style=\"color: #ff0000;\"><strong>easier to edit<\/strong><\/span> and that it <span style=\"color: #ff0000;\"><strong>reduces clutter in diffs<\/strong><\/span>.<\/p>\n<p>=<\/p>\n<p>It&#8217;s a common syntactical convention to allow trailing commas in an array, languages like C and Java allow it, and Python seems to have adopted this convention for its list data structure. It&#8217;s particularly useful when generating code for populating a list: just generate a sequence of elements and commas, no need to consider the last one as an special case that doesn&#8217;t have a comma at the end.<\/p>\n<p>==<\/p>\n<p>Python newbies often have some confusion about how to make one-element tuples.\u00a0<a href=\"https:\/\/wiki.python.org\/moin\/VenkataSubramanian\" target=\"_blank\">VenkataSubramanian<\/a>\u00a0provided a nice summary of Python&#8217;s tuple syntax\u00a0<a href=\"https:\/\/wiki.python.org\/moin\/TupleSyntax#A1\" target=\"_blank\">[1]<\/a>.<\/p>\n<p>Zero Element Tuples<\/p>\n<p>In Python, zero-element tuples look like:<\/p>\n<p>()<\/p>\n<p>In this form, unlike the other tuple forms, parentheses are the essential elements, not commas.<\/p>\n<p>One Element Tuples<\/p>\n<p>One-element tuples look like:<\/p>\n<p>1,<\/p>\n<p>The essential element here is the trailing comma. As for any expression, parentheses are optional, so you may also write one-element tuples like<\/p>\n<p>(1,)<\/p>\n<p>but <strong><span style=\"color: #ff0000;\">it is the comma, not the parentheses, that define the tuple<\/span><\/strong>.<\/p>\n<p>Multiple Element Tuples<\/p>\n<p>In Python, multiple-element tuples look like:<\/p>\n<p>1,2,3<\/p>\n<p><strong><span style=\"color: #ff0000;\">The essential elements are the commas between each element of the tuple.<\/span><\/strong> Multiple-element tuples may be written with a trailing comma, e.g.<\/p>\n<p>1,2,3,<\/p>\n<p>but <span style=\"color: #0000ff;\"><strong>the trailing comma is completely optional<\/strong><\/span>. Just like all other expressions in Python, multiple-element tuples may be enclosed in parentheses, e.g.<\/p>\n<p>(1,2,3)<\/p>\n<p>or<\/p>\n<p>(1,2,3,)<\/p>\n<p>but again, it is the commas, not the parentheses, that define the tuple.<\/p>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f18\u7531\uff1a \u5728\u770b\u4e00\u4e9bPython\u4ee3\u7801\u7684\u65f6\u5019\u4f1a\u53d1\u73b0\u6709\u4eba\u5728\u521b\u5efalist\/tuple\u7684\u65f6\u5019\u4f1a\u5728\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\u9762\u6dfb\u52a0\u4e00\u4e2a&#038; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[161,8,257],"class_list":["post-2414","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-list","tag-python","tag-tuple"],"views":2906,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2414"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2414\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}