{"id":1088,"date":"2014-08-19T08:40:21","date_gmt":"2014-08-19T08:40:21","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1088"},"modified":"2014-08-19T08:40:21","modified_gmt":"2014-08-19T08:40:21","slug":"%e4%b8%80%e9%81%93%e6%80%9d%e8%80%83%e9%a2%98_0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1088.html","title":{"rendered":"\u4e00\u9053\u601d\u8003\u9898_0"},"content":{"rendered":"<p>\u7a81\u7136\u4e4b\u95f4\u89c9\u5f97\u4e0d\u77e5\u9053\u5199\u4e9b\u4ec0\u4e48\u5185\u5bb9\u597d\uff08\u867d\u7136\u6211\u4e4b\u524d\u79ef\u7d2f\u4e86\u4e0d\u5c11\u7684\u8bfe\u5916\u4e66\u7684\u8bfb\u4e66\u7b14\u8bb0\/\u8bb0\u5f55\uff0c\u4f46\u4e0d\u662f\u548c\u4ee3\u7801\/\u5b89\u5168\u76f8\u5173\u7684\u5185\u5bb9\u611f\u89c9\u4e5f\u4e0d\u592a\u597d\u76f4\u63a5\u5c06\u53d1\u6587\u7ae0\u5c06\u4e66\u7684\u76ee\u5f55\u7b49\u5185\u5bb9\u653e\u5728blog\u4e0a{\u5982\u679c\u4e2a\u4eba\u60f3\u6cd5\u8f83\u591a\u3001\u8f83\u4e3a\u79c1\u4eba\u6027\u8d28\u7684\u8bdd\u5e72\u8106\u8bbe\u7f6e\u4e2a\u5bc6\u7801or\u76f4\u63a5\u7f6e\u4f4d\u79c1\u5bc6\uff0c\u4e2a\u81ea\u5df1\u770b\u5c31\u884c}\uff0c\u603b\u89c9\u5f97\u6587\u7ae0\u5982\u679c\u80fd\u6709\u4e9b\u4ee3\u7801\u5728\u91cc\u9762\uff0c\u7136\u540e\u5177\u5907\u4e00\u5b9a\u7684\u542f\u53d1\u6027\u8d28\uff0c\u8bb0\u5f55\u5728\u9762\u5bf9\u95ee\u9898\u65f6\u7684\u601d\u8003\u8fc7\u7a0b\u3001\u89e3\u51b3\u65b9\u6cd5\u7684\u8f6c\u53d8\u7b49\u5185\u5bb9\u4f1a\u66f4\u6709\u542f\u53d1\u6027\uff09\uff0c\u7136\u540e\u6d4f\u89c8\u5230\u4e86\u4e00\u7bc7\u8bb2\u9762\u8bd5\u9898\u7684\u6587\u7ae0\uff0c\u81ea\u5df1\u6ca1\u6709\u76f4\u63a5\u770b\u89e3\u51b3\u65b9\u6848\uff0c\u5148\u601d\u8003\u4e86\u4e00\u4f1a\uff0c\u7136\u540e\u548c\u4f5c\u8005\u7684\u601d\u8def\u3001\u7b54\u6848\u8fdb\u884c\u4e86\u6bd4\u8f83\uff0c\u89c9\u5f97\u5f88\u4eab\u53d7\u8fd9\u4e2a\u8fc7\u7a0b\uff08\u800c\u4e14\u8fd9\u79cd\u5185\u5bb9\u5bf9\u4ee5\u540e\u53ef\u80fd\u7684\u9762\u8bd5\u4e5f\u4f1a\u6709\u4e00\u5b9a\u5e2e\u52a9\u7684\uff09\uff0c\u6240\u4ee5\u51b3\u5b9a\u4ee5\u540e\u5728\u6682\u65f6\u6ca1\u6709\u5176\u5b83\u5185\u5bb9\u60f3\u5199\u7684\u65f6\u5019\u5199\u4e00\u4e9b\u6bd4\u8f83\u597d\u73a9\u7684\u9762\u8bd5\u9898\uff0c\u505a\u505a\u8111\u529b\u4f53\u64cd\u3002\u4e0b\u9762\u5c31\u662f\u7b2c\u4e00\u9053\u9898\u76ee\uff1a<\/p>\n<blockquote><p>\u9898\u76ee\uff1a\u6709\u4e00\u4e2a\u957f\u5ea6\u662f101\u7684\u6570\u7ec4\uff0c\u5b58\u67091 ~ 100\u8fd9100\u4e2a\u6570\u5b57\uff0c\u5176\u4e2d\u6709\u4e00\u4e2a\u662f\u91cd\u590d\u7684\u3002\u8bf7\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u627e\u51fa\u8fd9\u4e2a\u91cd\u590d\u7684\u6570\u5b57\u3002<\/p><\/blockquote>\n<p>\u6211\u60f3\u4e86\u4e00\u9635\u4e4b\u540e\u89c9\u5f97\u65b9\u6cd5\u7684\u8bdd\u8c8c\u4f3c\u53ef\u4ee5\u91c7\u7528Python\u7684set\u7ed3\u6784\u53bb\u91cd\uff0c\u7136\u540e\u7528\u539f\u6709list\u51cf\u53bb\u53bb\u91cd\u4e4b\u540e\u7684list\u6765\u641e\u5b9a\uff08\u6ca1\u6709\u5199\u4ee3\u7801\uff0c\u53ea\u662f\u8003\u8651\u7684\u53ef\u884c\u7684\u601d\u8def\/\u65b9\u6848\uff0c\u4f46\u540e\u6765\u641c\u7d22\u4e86\u4e4b\u540e\u624d\u77e5\u9053<span style=\"color: rgb(255, 0, 0);\">Python\u7684list\u4e0d\u652f\u6301\u51cf\u6cd5\uff0c\u53ea\u6709set\u53ef\u4ee5\u505a\u51cf\u6cd5<\/span>\uff0c<strong>\u53c8\u56e0\u4e3a\u8fd9\u91cc\u7684\u4e24\u4e2aset\u662f\u76f8\u540c\u7684\uff0c\u601d\u8def\u5c31\u5361\u5728\u8fd9\u91cc\u4e86<\/strong>\uff09\uff0c\u4e0d\u8fc7\u540e\u6765\u8fd8\u662f\u7528\u53e6\u4e00\u79cd\u65b9\u6cd5\u89e3\u51b3\u4e86\uff0c\u601d\u8def\u7684\u8bdd\u53c2\u89c1\uff1a<a title=\"\u53bb\u91cd\u7684\u5404\u79cd\u65b9\u6cd5[\u4e0d\u5b9a\u671f\u66f4\u65b0]\" href=\"http:\/\/ixyzero.com\/blog\/archives\/100.html\" target=\"_blank\">\u53bb\u91cd\u7684\u5404\u79cd\u65b9\u6cd5[\u4e0d\u5b9a\u671f\u66f4\u65b0]<\/a>\u3002<\/p>\n<p>\u7136\u540e\u518d\u53bb\u770b\u4f5c\u8005\u7684\u601d\u8def\uff1a<span style=\"color: rgb(255, 0, 0);\">\u5148\u6392\u5e8f\uff0c\u7136\u540e\u987a\u5e8f\u67e5\u627e\u503c\u76f8\u540c\u7684\u5143\u7d20\u5373\u662f\u552f\u4e00\u91cd\u590d\u7684\u6570\u5b57<\/span>\u3002<\/p>\n<p>\u518d\u770b\u6587\u7ae0\u4e0b\u9762\u7684\u8bc4\u8bba\uff0c\u8fd8\u6709\u4e00\u4e2a\u66f4\u5177\u9488\u5bf9\u6027\uff0c\u540c\u65f6\u4e5f\u66f4\u5feb\u7684\u65b9\u6cd5\uff1a\u76f4\u63a5\u628a\u8fd9101\u4e2a\u6570\u5b57\u52a0\u8d77\u6765\uff0c\u7136\u540e\u51cf\u53bb5050\uff0c\u5c31\u662f\u90a3\u4e2a\u91cd\u590d\u7684\u6570\u5b57\u3002\u6bd4\u59821\uff0c2\uff0c2\uff0c3\uff0c\u8fd94\u4e2a\u6570\u5b57\uff0c\u52a0\u8d77\u6765\u662f8\uff0c\u7136\u540e\u51cf\u53bb1+2+3\uff0c\u6700\u540e\u7ed3\u679c\u662f2\uff0c\u90a3\u4e482\u5c31\u662f\u91cd\u590d\u7684\uff08\u56e0\u4e3a\u9898\u76ee\u5df2\u7ecf\u9650\u5b9a\u5f97\u5f88\u6e05\u695a\u4e86\uff0c\u8003\u7684\u5c31\u662f\u8bfb\u9898\u80fd\u529b\uff09\u3002<\/p>\n<p>\u5176\u4f59\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<h6>1.\u5229\u7528sort\u548cuniq\u8fdb\u884c\u5904\u7406<\/h6>\n<pre class=\"lang:default decode:true\">$ cat t\n5,2,1,9,6,2,8\n$ cut -d',' --output-delimiter=' ' -f1- &lt;t | tr ' ' 'n' | sort | uniq -c | awk '!\/ 1 \/ {print $2}'\n2<\/pre>\n<p>\u4e0a\u9762\u8fd9\u4e2a\u4e5f\u662f<span style=\"color: rgb(255, 0, 0);\">\u5148\u5229\u7528sort\u6392\u5e8f\uff0c\u7136\u540e\u7528uniq\u7684-c\u9009\u9879\u8ba1\u7b97\u51fa\u5404\u4e2a\u5143\u7d20\u51fa\u73b0\u6b21\u6570\uff0c\u6700\u540e\u7528awk\u6253\u5370\u4e0d\u662f1\u7684\u90a3\u4e2a\u5373\u53ef\u5f97\u5230\u7ed3\u679c<\/span>\u3002<\/p>\n<h6>2.\u5229\u7528collections\u6a21\u5757<\/h6>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt; arr1 = [5,2,1,9,6,2,8]\n&gt;&gt;&gt; import collections\n&gt;&gt;&gt; collections.Counter(arr1).most_common(1)[0][0]\n2<\/pre>\n<h6>3.\u5229\u7528\u5217\u8868\u81ea\u5e26\u7684count()\u51fd\u6570<\/h6>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt; arr1 = [5,2,1,9,6,2,8]\n&gt;&gt;&gt; [i for i in arr1 if arr1.count(i) == 2]\n[2, 2]<\/pre>\n<h6>4.\u539f\u4f5c\u8005\u7684\u65b9\u6cd5<\/h6>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\n#-*- coding:utf-8 -*-\narr1 = [5,2,1,9,6,2,8]\n\narr2 = {}.fromkeys(arr1).keys()\nprint arr2    #\u6548\u7387\u9ad8\uff0c\u4f46\u4e0d\u7b26\u5408\u9898\u76ee\u8981\u6c42\n\narr3 = list(set(arr1))\nprint arr3    #\u6548\u7387\u66f4\u9ad8\uff0c\u4f46\u4ecd\u7136\u4e0d\u7b26\u5408\u9898\u76ee\u8981\u6c42\n\narr4 = sorted(arr1)\ni = len(arr4) - 1\nfor x in range(i):\n    y = x + 1\n    if arr4[x] == arr4[y]:\n        print arr4[x]    #\u8fbe\u5230\u9898\u76ee\u8981\u6c42\u4e86<\/pre>\n<h6>5.\u6211\u7684\u89e3\u51b3\u65b9\u6cd5<\/h6>\n<pre class=\"lang:default decode:true\">arr1 = [5,2,1,9,6,2,8]\ndic = {}\ni = len(arr1)\nfor x in range(i):\n    if dic.get(arr1[x]) and dic[arr1[x]] == 1:\n        print arr1[x]\n    else:\n        dic[arr1[x]] = 1<\/pre>\n<h6>6.\u521a\u521a\u53d1\u73b0\u7684\u53e6\u5916\u4e00\u4e2a\u89e3\u51b3\u65b9\u6cd5<\/h6>\n<pre class=\"lang:default decode:true \">#\u521a\u521a\u53d1\u73b0\u7684\u53e6\u5916\u4e00\u4e2a\u7b80\u6d01\u7684\u65b9\u6848\uff08\u53ea\u6253\u5370\u51fa\u73b0\u4e00\u6b21\u7684\u5143\u7d20\u7ec4\u6210\u7684\u5217\u8868\uff09\n&gt;&gt;&gt; s=[11,22,11,44,22]\n&gt;&gt;&gt; [(s[i]) for i in range(len(s)) if s.count(s[i])&lt;2]\n[44]\t#\u53ea\u51fa\u73b01\u6b21\u7684\u5143\u7d20\u7ec4\u6210\u7684\u65b0list\n&gt;&gt;&gt; [(s[i],i) for i in range(len(s)) if s.count(s[i])&lt;2]\n[(44, 3)]\t#\u53ea\u51fa\u73b01\u6b21\u7684\u5143\u7d20\u53ca\u5176\u5bf9\u5e94\u7684\u4e0b\u6807\u4f4d\u7f6e<\/pre>\n<p>\u5982\u679c\u662f\u9898\u76ee\u4e2d\u7684\u8981\u6c42\u7684\u8bdd\u5c31\u662f\uff1a<\/p>\n<pre class=\"lang:default decode:true \">&gt;&gt;&gt; arr5=[arr1[i] for i in range(len(arr1)) if arr1.count(arr1[i])&gt;1]\n[2, 2]\n&gt;&gt;&gt; print list(set(arr5))\n[2]<\/pre>\n<hr \/>\n<p>\u5728\u5b9e\u9645\u5199\u8be5\u65b9\u6cd5\u7684\u8fc7\u7a0b\u4e2d\u78b0\u5230\u7684\u4e00\u4e9b\u95ee\u9898\uff08\u4e3b\u8981\u662f\u56e0\u4e3a\u5bf9Python\u7684list\u3001dict\u7684\u7ed3\u6784\u4e0d\u719f\u6089\u4ee5\u53ca\u5b9e\u9645\u9047\u5230\u7684\u60c5\u51b5\u8f83\u5c11\u5bfc\u81f4\u7684\uff09\u8bb0\u5f55\u5982\u4e0b\uff1a<\/p>\n<h6>1.KeyError\u9519\u8bef<\/h6>\n<pre class=\"lang:default decode:true \">&gt;&gt;&gt; arr1 = [5,2,1,9,6,2,8]\n&gt;&gt;&gt; dic = {}\n&gt;&gt;&gt; i = len(arr1) - 1\n&gt;&gt;&gt; for x in range(i):\n...     if dic[arr1[x]] != 1:\n...         dic[arr1[x]] = 1\n...     else:\n...         print arr1[x]\n...\nTraceback (most recent call last):\n  File \"&lt;stdin&gt;\", line 2, in &lt;module&gt;\nKeyError: 5<\/pre>\n<p>\u53bb\u7f51\u4e0a\u641c\u7d22\u95ee\u9898\u539f\u56e0\uff1a<\/p>\n<blockquote><p><strong>Python\u4e2ddict\u62a5KeyError\u7684\u95ee\u9898<\/strong><\/p>\n<p>\u5982\u679c\u4e0d\u77e5\u9053dict\u4e2d\u662f\u5426\u6709key\u7684\u503c\uff0c\u90a3\u4e48\u6700\u597d\u7528 dict.get(key) \u65b9\u6cd5\uff1b\u5982\u679c\u7528dict[key]\u8fd9\u4e2a\u65b9\u6cd5\u8bfb\u53d6\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u5c31\u4f1a\u62a5KeyError\u5f02\u5e38\uff08\u6240\u4ee5\u9700\u8981\u6839\u636e\u4e0d\u540c\u60c5\u51b5\u7528\u4e0d\u540c\u7684\u65b9\u5f0f\uff09<br \/>\ndict.get\u65b9\u6cd5\u4e3b\u8981\u662f\u63d0\u4f9b\u4e00\u4e2a\u53d6\u4e0d\u5230\u5bf9\u5e94key\u7684value\u5c31\u8fd4\u56de\u9ed8\u8ba4\u503c\u7684\u529f\u80fd\uff0c\u800cdict[key]\u5b9e\u9645\u4e0a\u662f\u8c03\u7528\u4e86__getitem__\u65b9\u6cd5\uff0c\u4f60\u4e5f\u53ef\u4ee5\u91cd\u5199\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p><\/blockquote>\n<h6>2.Python\u7684list\u7684\u51cf\u6cd5\u64cd\u4f5c<\/h6>\n<p>\u4e24\u4e2a list \u76f8\u51cf<br \/>\n\u5c31\u662f\u6bd4\u5982\uff1a<br \/>\na=[1,2,3,4,5]<br \/>\nb=[2,3,4]<br \/>\na-b\u9700\u8981\u5f97\u5230[1,5]<\/p>\n<p>\u4f46\u662f Python list \u4e0d\u652f\u6301\u51cf\u6cd5\uff0c\u6240\u4ee5\u53ea\u80fd\u4f7f\u7528Python\u7684set\u7ed3\u6784\u6295\u673a\u53d6\u5de7\u4e86~\u5475\u5475<br \/>\n\u6211\u4eec\u6765\u7528set()\u5982\u4e0b\uff1a<br \/>\nprint list (set(a)-set(b))<br \/>\n[1,5] #\u7ed3\u679c<\/p>\n<h6>3.\u5982\u4f55\u627e\u51faPython\u7684list\u4e2d\u91cd\u590d\u7684\u9879<\/h6>\n<p>&gt;&gt;&gt; s=[11,22,11,44,22]<br \/>\n&gt;&gt;&gt; [(s[i],i) for i in range(len(s)) if s.count(s[i])&gt;1]<br \/>\n[(11, 0), (22, 1), (11, 2), (22, 4)]<\/p>\n<p><strong>\u7528defaultdict\u6765\u8bd5\u8bd5\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true \">from collections import defaultdict\ns=[11,22,11,44,22,33]\nd = defaultdict(list)\nfor k,va in [(v,i) for i,v in enumerate(s)]:\n    d[k].append(va)\nd.items()<\/pre>\n<p><strong>\u8fd8\u6709\uff1a<\/strong><\/p>\n<p>&gt;&gt;&gt; from collections import Counter<br \/>\n&gt;&gt;&gt; Counter([11,22,11,44,22,33])<br \/>\nCounter({11: 2, 22: 2, 33: 1, 44: 1})<\/p>\n<p>&nbsp;<\/p>\n<h6>\u53c2\u8003\u5730\u5740\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/ixyzero.com\/blog\/archives\/534.html\" target=\"_blank\">Python\u5217\u8868[list]\u53bb\u91cd<\/a><\/li>\n<li><a href=\"http:\/\/ixyzero.com\/blog\/archives\/100.html\" target=\"_blank\">\u53bb\u91cd\u7684\u5404\u79cd\u65b9\u6cd5[\u4e0d\u5b9a\u671f\u66f4\u65b0]<\/a><\/li>\n<li><a href=\"http:\/\/bbs.chinaunix.net\/thread-1680208-1-1.html\" target=\"_blank\">\u5982\u4f55\u627e\u51fa python list \u4e2d\u6709\u91cd\u590d\u7684\u9879<\/a><\/li>\n<li><a href=\"http:\/\/standalone.iteye.com\/blog\/491959\" target=\"_blank\">python\u4e2dlist\u7c7b\u578b\u76f8\u51cf<\/a><\/li>\n<li><a href=\"http:\/\/www.cnblogs.com\/python-selenium\/p\/3909175.html\" target=\"_blank\">python \u4e2d\u5bf9list\u505a\u51cf\u6cd5\u64cd\u4f5c<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u7a81\u7136\u4e4b\u95f4\u89c9\u5f97\u4e0d\u77e5\u9053\u5199\u4e9b\u4ec0\u4e48\u5185\u5bb9\u597d\uff08\u867d\u7136\u6211\u4e4b\u524d\u79ef\u7d2f\u4e86\u4e0d\u5c11\u7684\u8bfe\u5916\u4e66\u7684\u8bfb\u4e66\u7b14\u8bb0\/\u8bb0\u5f55\uff0c\u4f46\u4e0d\u662f\u548c\u4ee3\u7801\/\u5b89\u5168\u76f8\u5173\u7684\u5185\u5bb9\u611f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[161,8],"class_list":["post-1088","post","type-post","status-publish","format-standard","hentry","category-programing","tag-list","tag-python"],"views":4813,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1088","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=1088"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1088\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}