{"id":4108,"date":"2018-10-09T20:35:31","date_gmt":"2018-10-09T12:35:31","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4108"},"modified":"2018-10-09T20:35:31","modified_gmt":"2018-10-09T12:35:31","slug":"python%e7%9a%84%e4%b8%80%e4%ba%9b%e5%b0%8f%e7%9f%a5%e8%af%86%e7%82%b9_12","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4108.html","title":{"rendered":"Python\u7684\u4e00\u4e9b\u5c0f\u77e5\u8bc6\u70b9_12"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u603b\u7ed3\u4e00\u4e0b\u6700\u8fd1\u5728\u7f16\u7801\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u4e00\u4e9bPython\u77e5\u8bc6\u70b9\uff0c\u65b9\u4fbf\u4ee5\u540e\u5feb\u901f\u53c2\u8003\u3001\u5b66\u4e60\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/13694034\/is-a-python-list-guaranteed-to-have-its-elements-stay-in-the-order-they-are-inse\" target=\"_blank\" rel=\"noopener\">Python\u4e2dlist\u7684\u5143\u7d20\u90fd\u662f\u56fa\u5b9a\u987a\u5e8f\u7684\u4e48\uff1f<\/a><\/li>\n<\/ul>\n<p>Yes, the order of elements in a python list is persistent.<br \/>\n\u6839\u636e\u63d2\u5165\u987a\u5e8f\u51b3\u5b9a\u5143\u7d20\u7684\u5b9e\u9645\u4f4d\u7f6e\u3002<\/p>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/1518522\/find-the-most-common-element-in-a-list\" target=\"_blank\" rel=\"noopener\">Python\u4e2d\u5982\u4f55\u67e5\u627elist\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u5143\u7d20\uff1f<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\"># \u671f\u671b\u8fbe\u5230\u7684\u6548\u679c\r\n&gt;&gt;&gt; most_common(['duck', 'duck', 'goose'])\r\n'duck'\r\n&gt;&gt;&gt; most_common(['goose', 'duck', 'duck', 'goose'])\r\n'goose'\r\n\r\n\r\n# \u65b9\u6cd5\u4e00\r\nfrom itertools import groupby as g\r\ndef most_common_oneliner(L):\r\n    return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]\r\n\r\n# \u65b9\u6cd5\u4e8c\r\ndef most_common(lst):\r\n    return max(set(lst), key=lst.count)\r\n\r\n#\u65b9\u6cd5\u4e09\r\nfrom collections import Counter\r\ndef most_common(lst):\r\n    data = Counter(lst)\r\n    return max(lst, key=data.get)<\/pre>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/10797819\/finding-the-mode-of-a-list\">Python\u4e2d\u5982\u4f55\u786e\u5b9a\u4e00\u4e2alist\u7684\u6a21\u5f0f<\/a>\uff08\u540c\u4e0a\uff09<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \"># \u5373\uff0c\u7ed9\u5b9a\u4e00\u4e2alist\uff0c\u8c03\u7528\u67d0\u4e2a\u51fd\u6570\u8fd4\u56de\u8be5list\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u5143\u7d20\u3002\r\nmax(set(list_in), key=list_in.count)\r\n\r\n# Python 2.7\u53ca\u4ee5\u4e0a\u7248\u672c\r\nfrom collections import Counter\r\ndata = Counter(your_list_in_here)\r\ndata.most_common()   # Returns all unique items and their counts\r\ndata.most_common(1)  # Returns the highest occurring item\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/13694034\/is-a-python-list-guaranteed-to-have-its-elements-stay-in-the-order-they-are-inse\">https:\/\/stackoverflow.com\/questions\/13694034\/is-a-python-list-guaranteed-to-have-its-elements-stay-in-the-order-they-are-inse<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/1518522\/find-the-most-common-element-in-a-list\">https:\/\/stackoverflow.com\/questions\/1518522\/find-the-most-common-element-in-a-list<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/10797819\/finding-the-mode-of-a-list\">https:\/\/stackoverflow.com\/questions\/10797819\/finding-the-mode-of-a-list<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u603b\u7ed3\u4e00\u4e0b\u6700\u8fd1\u5728\u7f16\u7801\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u4e00\u4e9bPython\u77e5\u8bc6\u70b9\uff0c\u65b9\u4fbf\u4ee5\u540e\u5feb\u901f\u53c2\u8003\u3001\u5b66\u4e60\u3002 \u6b63\u6587\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[161,8],"class_list":["post-4108","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-list","tag-python"],"views":2903,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4108","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4108"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4108\/revisions"}],"predecessor-version":[{"id":4109,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4108\/revisions\/4109"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}