{"id":2670,"date":"2016-05-14T15:05:55","date_gmt":"2016-05-14T07:05:55","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2670"},"modified":"2016-05-14T15:05:55","modified_gmt":"2016-05-14T07:05:55","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96-listordereddict-%e7%9a%84-%e7%ac%ac%e4%b8%80%e4%b8%aa%e6%9c%80%e5%90%8e%e4%b8%80%e4%b8%aa-%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2670.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u83b7\u53d6 list\/OrderedDict \u7684 \u7b2c\u4e00\u4e2a\/\u6700\u540e\u4e00\u4e2a \u5143\u7d20"},"content":{"rendered":"<p>=Start=<\/p>\n<h6>\u7f18\u7531\uff1a<\/h6>\n<p>\u6700\u8fd1\u5728\u5de5\u4f5c\u4e2d\u6709\u63a5\u89e6\u5230\u7528Python\u7684xmltodict\u6a21\u5757\u8fdb\u884cXML\u7684\u89e3\u6790\uff0c\u7528\u8be5\u6a21\u5757\u8fdb\u884cXML\u89e3\u6790\u7684\u65f6\u5019\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u4e2aOrderDict\uff0c\u5373\u2014\u2014\u6709\u5e8f\u5b57\u5178\uff0c\u867d\u7136\u5728\u7a0b\u5e8f\u4e2d\u6ca1\u6709\u53d6OrderDict\u7684\u9996\u5c3e\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u4f46\u51fa\u4e8e\u597d\u5947\u548c\u5b66\u4e60\u7684\u5fc3\u6001\uff0c\u5b66\u4e60\u4e86\u4e00\u4e0b\uff0c\u8bb0\u5f55\u5728\u6b64\uff0c\u65b9\u4fbf\u53c2\u8003\u3002<\/p>\n<h5>\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6>Python\u4e2d\u5982\u4f55\u83b7\u53d6 list \u7684 \u7b2c\u4e00\u4e2a\/\u6700\u540e\u4e00\u4e2a \u5143\u7d20<\/h6>\n<pre class=\"lang:default decode:true \">some_list[0]  #\u5217\u8868\u7684 \u7b2c\u4e00\u4e2a \u5143\u7d20\nsome_list[-1] #\u5217\u8868\u7684 \u6700\u540e\u4e00\u4e2a \u5143\u7d20<\/pre>\n<h6>Python\u4e2d\u5982\u4f55\u83b7\u53d6 OrderDict \u7684 \u7b2c\u4e00\u4e2a\/\u6700\u540e\u4e00\u4e2a \u5143\u7d20<\/h6>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPython\u4e2d\u7684dict\u662f\u65e0\u5e8f\u7684\uff0c\u5982\u679c\u9700\u8981\u83b7\u53d6 \u7b2c\u4e00\u4e2a\/\u6700\u540e\u4e00\u4e2a \u5143\u7d20\uff0c\u5f97\u4f7f\u7528 collections.OrderedDict \u624d\u884c\uff1a<\/p>\n<pre class=\"lang:default decode:true\">from collections import OrderedDict\nod = OrderedDict(zip('bar','foo'))\nprint od  # OrderedDict([('b', 'f'), ('a', 'o'), ('r', 'o')])\n\n#\u65b9\u6cd5\u4e00\nod.keys()[-1]    # \u4ec5\u9002\u7528\u4e8e Python 2.x\nod.values()[-1]\nod.items()[-1]\n\nlist(od.items())[-1] # \u517c\u5bb9 Python 3.x\n\n#\u65b9\u6cd5\u4e8c\nod.popitem() # also removes the last item(\u4fee\u6539\u4e86OrderDict\u672c\u8eab\u7684\u5185\u5bb9)\n\n#\u65b9\u6cd5\u4e09\uff08\u6700\u4f73\u65b9\u6cd5\uff09\nnext(reversed(od)) # get the last key\nnext(reversed(od.items())) # get the last item\n\nnext(iter(od)) # get the first key\nnext(iter(od.items())) # get the first item<\/pre>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/16125229\/last-key-in-python-dictionary\">http:\/\/stackoverflow.com\/questions\/16125229\/last-key-in-python-dictionary<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/19030179\/how-to-access-the-first-and-the-last-elements-in-a-dictionary-python\">http:\/\/stackoverflow.com\/questions\/19030179\/how-to-access-the-first-and-the-last-elements-in-a-dictionary-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/30250715\/how-do-you-get-the-first-3-elements-in-python-ordereddict\">http:\/\/stackoverflow.com\/questions\/30250715\/how-do-you-get-the-first-3-elements-in-python-ordereddict<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/21062781\/shortest-way-to-get-first-item-of-ordereddict-in-python-3\">http:\/\/stackoverflow.com\/questions\/21062781\/shortest-way-to-get-first-item-of-ordereddict-in-python-3<\/a> #nice<\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/9917178\/last-element-in-ordereddict\">http:\/\/stackoverflow.com\/questions\/9917178\/last-element-in-ordereddict<\/a> #\u6027\u80fd\u6d4b\u8bd5<\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/3097866\/python-access-to-first-element-in-dictionary\">http:\/\/stackoverflow.com\/questions\/3097866\/python-access-to-first-element-in-dictionary<\/a> #\u65b9\u6cd5\u4e0d\u9519<\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/10058140\/accessing-items-in-a-ordereddict\">http:\/\/stackoverflow.com\/questions\/10058140\/accessing-items-in-a-ordereddict<\/a> #\u6709\u8be6\u7ec6\u8bf4\u660e<\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/10503666\/get-the-first-100-elements-of-ordereddict\">http:\/\/stackoverflow.com\/questions\/10503666\/get-the-first-100-elements-of-ordereddict<\/a><\/li>\n<\/ul>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6700\u8fd1\u5728\u5de5\u4f5c\u4e2d\u6709\u63a5\u89e6\u5230\u7528Python\u7684xmltodict\u6a21\u5757\u8fdb\u884cXML\u7684\u89e3\u6790\uff0c\u7528\u8be5\u6a21\u5757\u8fdb [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[602,161,603,8],"class_list":["post-2670","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-collections","tag-list","tag-ordereddict","tag-python"],"views":8873,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2670","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=2670"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2670\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}