{"id":689,"date":"2014-07-22T01:08:40","date_gmt":"2014-07-22T01:08:40","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=689"},"modified":"2014-07-22T01:08:40","modified_gmt":"2014-07-22T01:08:40","slug":"python%e4%b8%ad%e7%9a%84%e5%88%97%e8%a1%a8list%ef%bc%8c%e5%85%83%e7%bb%84tuple%ef%bc%8c%e9%9b%86%e5%90%88set%ef%bc%8c%e5%ad%97%e5%85%b8dict","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/689.html","title":{"rendered":"Python\u4e2d\u7684\u5217\u8868(list)\uff0c\u5143\u7ec4(tuple)\uff0c\u96c6\u5408(set)\uff0c\u5b57\u5178(dict)"},"content":{"rendered":"<div>\n<h5 class=\"title content-title\" style=\"color: #454545;\">Python\u4e2d\u5217\u8868(list)\u3001\u5b57\u5178(dict)\u3001\u96c6\u5408(set) \u6570\u636e\u6392\u5e8f<\/h5>\n<\/div>\n<div>\n<p style=\"color: #454545;\">\u53c2\u8003\u94fe\u63a5\uff1a<a href=\"http:\/\/ciniao.me\/article.php?id=8\" target=\"_blank\">http:\/\/ciniao.me\/article.php?id=8<\/a><\/p>\n<p><span style=\"color: #000000; font-size: medium;\"><span style=\"color: #000000; font-size: medium;\"><strong style=\"color: #454545;\">\u5bf9\u5b57\u7b26\u4e32\u5217\u8868\u8fdb\u884c\u5c31\u5730\u6392\u5e8f<\/strong><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">list_str\u00a0=\u00a0[&#8216;blue&#8217;,&#8217;allen&#8217;,&#8217;sophia&#8217;,&#8217;keen&#8217;]<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">print\u00a0list_str<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">list_str.sort()<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">print\u00a0list_str<\/span><br style=\"color: #454545;\" \/><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">#\u6267\u884c\u7ed3\u679c\uff1a<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">[&#8216;blue&#8217;,\u00a0&#8216;allen&#8217;,\u00a0&#8216;sophia&#8217;,\u00a0&#8216;keen&#8217;]<\/span><br style=\"color: #454545;\" \/><\/span><\/span><\/p>\n<p style=\"color: #454545;\">[&#8216;allen&#8217;,\u00a0&#8216;blue&#8217;,\u00a0&#8216;keen&#8217;,\u00a0&#8216;sophia&#8217;]<\/p>\n<p style=\"color: #454545;\">====<\/p>\n<p><span style=\"color: #000000; font-size: medium;\"><span style=\"color: #000000; font-size: medium;\"><strong style=\"color: #454545;\">\u5bf9\u6574\u578b\u6570\u8fdb\u884c\u6392\u5e8f<\/strong><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">list_int\u00a0=\u00a0[34,23,2,2333,45]<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">print\u00a0list_int<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">list_int.sort()<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">print\u00a0list_int<\/span><br style=\"color: #454545;\" \/><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">#\u6267\u884c\u7ed3\u679c\uff1a<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">[34,\u00a023,\u00a02,\u00a02333,\u00a045]<\/span><br style=\"color: #454545;\" \/><\/span><\/span><\/p>\n<p style=\"color: #454545;\">[2,\u00a023,\u00a034,\u00a045,\u00a02333]<\/p>\n<p style=\"color: #454545;\">====<\/p>\n<p><span style=\"color: #000000; font-size: medium;\"><span style=\"color: #000000; font-size: medium;\"><strong style=\"color: #454545;\">\u5bf9\u5b57\u5178\u6570\u636e\u8fdb\u884c\u6392\u5e8f(\u5bf9\u5b57\u5178\u8fdb\u884c\u6392\u5e8f\u9700\u8981\u4f7f\u7528<span style=\"color: #ff0000;\">sorted()\u65b9\u6cd5<\/span>)<\/strong><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">dict_str\u00a0=\u00a0{&#8216;blue&#8217;:&#8217;5555@sina.com&#8217;,<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;allen&#8217;:&#8217;222@163.com&#8217;,<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;sophia&#8217;:&#8217;4444@gmail.com&#8217;,<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#8216;ceen&#8217;:&#8217;blue@263.net&#8217;}<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">print\u00a0dict_str<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\"><span style=\"color: #ff0000;\">#\u00a0<strong>\u6309\u7167key\u8fdb\u884c\u6392\u5e8f<\/strong><\/span><\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">print\u00a0<span style=\"color: #ff0000;\"><strong>sorted<\/strong><\/span>(dict_str.items(),\u00a0key=lambda\u00a0d:\u00a0d[0])\u00a0\u00a0\u00a0<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\"><span style=\"color: #ff0000;\">#\u00a0<strong>\u6309\u7167value\u8fdb\u884c\u6392\u5e8f<\/strong><\/span><\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">print\u00a0<strong><span style=\"color: #ff0000;\">sorted<\/span><\/strong>(dict_str.items(),\u00a0key=lambda\u00a0d:\u00a0d[1])<\/span><br style=\"color: #454545;\" \/><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">#\u6267\u884c\u7ed3\u679c\uff1a<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">{&#8216;blue&#8217;:\u00a0&#8216;5555@sina.com&#8217;,\u00a0&#8216;allen&#8217;:\u00a0&#8216;222@163.com&#8217;,\u00a0&#8216;sophia&#8217;:\u00a0&#8216;4444@gmail.com&#8217;,\u00a0&#8216;ceen&#8217;:\u00a0&#8216;blue@263.net&#8217;}<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #000000; font-size: medium;\"><span style=\"color: #000000; font-size: medium;\"><span style=\"color: #454545;\">[(&#8216;allen&#8217;,\u00a0&#8216;222@163.com&#8217;),\u00a0(&#8216;blue&#8217;,\u00a0&#8216;5555@sina.com&#8217;),\u00a0(&#8216;ceen&#8217;,\u00a0&#8216;blue@263.net&#8217;),(&#8216;sophia&#8217;,\u00a0&#8216;4444@gmail.com&#8217;)]<\/span><br style=\"color: #454545;\" \/><\/span><\/span><\/p>\n<p style=\"color: #454545;\">[(&#8216;allen&#8217;,\u00a0&#8216;222@163.com&#8217;),\u00a0(&#8216;sophia&#8217;,\u00a0&#8216;4444@gmail.com&#8217;),\u00a0(&#8216;blue&#8217;,\u00a0&#8216;5555@sina.com&#8217;),\u00a0(&#8216;ceen&#8217;,\u00a0&#8216;blue@263.net&#8217;)]<\/p>\n<p style=\"color: #454545;\">====<\/p>\n<p style=\"color: #454545;\"><strong><span style=\"color: #ff0000; font-size: medium;\">\u5bf9\u96c6\u5408\u6392\u5e8f<\/span>\uff1a<\/strong><\/p>\n<p><span style=\"color: #000000; font-size: medium;\"><span style=\"color: #000000; font-size: medium;\"><span style=\"color: #454545;\">data = set([&#8216;booklet&#8217;, &#8216;4 sheets&#8217;, &#8217;48 sheets&#8217;, &#8217;12 sheets&#8217;])<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">r = <span style=\"color: #ff0000;\">sorted<\/span>(data, key=<span style=\"color: #ff0000;\">lambda<\/span> item: (int(item.partition(&#8216; &#8216;)[0]) <\/span><span style=\"color: #454545;\">if item[0].isdigit() else float(&#8216;inf&#8217;), item))<\/span><br style=\"color: #454545;\" \/><span style=\"color: #454545;\">print &#8216;,n&#8217;.join(r)<\/span><br style=\"color: #454545;\" \/><\/span><\/span><\/p>\n<p style=\"color: #454545;\">4 sheets,<br \/>\n12 sheets,<br \/>\n48 sheets,<br \/>\nbooklet<\/p>\n<p style=\"color: #454545;\"><strong>\u539f\u6587\u53c2\u89c1<\/strong>\uff1a<a href=\"http:\/\/stackoverflow.com\/questions\/2669059\/how-to-sort-alpha-numeric-set-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/2669059\/how-to-sort-alpha-numeric-set-in-python<\/a><\/p>\n<p style=\"color: #454545;\">=========================================================<\/p>\n<h6 style=\"color: #454545;\">\u6765\u4e2a\u590d\u6742\u70b9\u7684\u4f8b\u5b50\uff1a<\/h6>\n<p style=\"color: #454545;\">\u5bf9list\u8fdb\u884c\u6392\u5e8f\uff0clist\u7684\u5143\u7d20\u662f\u4e00\u4e2a\u542b\u6709\u4e09\u4e2a\u6570\u5b57\u7684list\uff0c\u6392\u5e8f\u89c4\u5219\u5982\u4e0b\uff1a<br \/>\n\u7b2c\u4e00\u4f4d\u5347\u6392\u5e8f<br \/>\n\u82e5\u7b2c\u4e00\u4f4d\u76f8\u540c\uff0c\u7b2c\u4e8c\u4f4d\u964d\u6392\u5e8f<br \/>\n\u82e5\u7b2c\u4e8c\u4f4d\u4e5f\u76f8\u540c\uff0c\u7b2c\u4e09\u4f4d\u5347\u6392\u5e8f<br \/>\n\u63d0\u793a\uff1a\u53ef\u4ee5\u7528sort\u65b9\u6cd5<br \/>\n\u4f8b\uff1a<br \/>\n\u6392\u5e8f\u524d<br \/>\nalist=[[1,2,3],[2,3,1],[1,3,2],[1,3,4],[1,2,1]]<br \/>\n\u6392\u5e8f\u540e<br \/>\nAlist=[[1,3,2],[1,3,4],[1,2,1],[1,2,3],[2,3,1]]<\/p>\n<h6 style=\"color: #454545;\">\u4ee3\u7801\u5982\u4e0b\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\n#\u00a0-*-\u00a0coding:\u00a0utf-8\u00a0-*-\nalist=[[1,2,3],[2,3,1],[1,3,2],[1,3,4],[1,2,1]]\nAlist\u00a0=\u00a0sorted(alist)\nprint\u00a0Alist\nb\u00a0=\u00a0sorted(Alist,cmp\u00a0=\u00a0lambda\u00a0x,\u00a0y:\u00a0-cmp(x[1],\u00a0y[1]))\nprint\u00a0b\nc\u00a0=\u00a0sorted(b,\u00a0key\u00a0=\u00a0lambda\u00a0z:\u00a0z[0])\nprint\u00a0c<\/pre>\n<\/div>\n<h6>\u539f\u6587\u5730\u5740\uff1a<\/h6>\n<p><a href=\"http:\/\/hi.baidu.com\/leejun_2005\/item\/8a6b4319b58d9e476826bb66\" target=\"_blank\">python\u4e2d\u5217\u8868(list)\u3001\u5b57\u5178(dict)\u3001\u96c6\u5408(set) \u6570\u636e\u6392\u5e8f<\/a><\/p>\n<div>\n<hr \/>\n<\/div>\n<div>\n<h5 class=\"ContentTitle\" style=\"color: #ff6600;\"><strong>Python\u4e24\u4e2a list \u83b7\u53d6\u4ea4\u96c6\uff0c\u5e76\u96c6\uff0c\u5dee\u96c6\u7684\u65b9\u6cd5<\/strong><\/h5>\n<\/div>\n<div><span style=\"color: #000000; font-size: medium;\">\u6709\u65f6\u5019\uff0c\u4e3a\u4e86\u9700\u6c42\uff0c\u9700\u8981\u7edf\u8ba1\u4e24\u4e2a list \u4e4b\u95f4\u7684\u4ea4\u96c6\uff0c\u5e76\u96c6\uff0c\u5dee\u96c6\u3002\u67e5\u8be2\u4e86\u4e00\u4e9b\u8d44\u6599\uff0c\u73b0\u5728\u603b\u7ed3\u5728\u4e0b\u9762\uff1a<\/span><\/div>\n<h5>1.\u83b7\u53d6\u4e24\u4e2a list \u7684\u4ea4\u96c6<\/h5>\n<h6>#\u65b9\u6cd5\u4e00<\/h6>\n<div><span style=\"color: #000000; font-size: medium;\">a=[2,3,4,5]<br \/>\nb=[2,5,8]<br \/>\ntmp = [val for val in a if val in b]\u00a0\u00a0\u00a0\u00a0#\u751f\u6210\u5217\u8868<br \/>\nprint tmp<br \/>\n#[2, 5]<\/span><\/div>\n<h6><span style=\"color: #000000; font-size: medium;\">#\u65b9\u6cd5\u4e8c<\/span><\/h6>\n<div><span style=\"color: #000000; font-size: medium;\">print list(set(a).<strong><span style=\"color: #ff0000;\">intersection<\/span><\/strong>(set(b)))<\/span><\/div>\n<h5><span style=\"color: #000000; font-size: medium;\">2.\u83b7\u53d6\u4e24\u4e2a list \u7684\u5e76\u96c6<\/span><\/h5>\n<div><span style=\"color: #000000; font-size: medium;\">print list(set(a).<strong><span style=\"color: #ff0000;\">union<\/span><\/strong>(set(b)))<\/span><\/div>\n<h5><span style=\"color: #000000; font-size: medium;\">3.\u83b7\u53d6\u4e24\u4e2a list \u7684\u5dee\u96c6<\/span><\/h5>\n<div><span style=\"color: #000000; font-size: medium;\">print list(set(b).<strong><span style=\"color: #ff0000;\">difference<\/span><\/strong>(set(a))) # b\u4e2d\u6709\u800ca\u4e2d\u6ca1\u6709\u7684<\/span><\/div>\n<div><span style=\"color: #000000; font-size: medium;\"><br \/>\n\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u5c31\u80fd\u5904\u7406 python list \u7684\u4ea4\u96c6\uff0c\u5e76\u96c6\uff0c\u5dee\u96c6\u4e86\u3002<\/span><\/div>\n<div>\n<hr \/>\n<\/div>\n<h5>Python\u5bf9list\u53bb\u91cd\u7684\u5404\u79cd\u65b9\u6cd5<\/h5>\n<div>\u4eca\u5929\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u5728\u5404\u79cd\u641c\u7d22\u4e4b\u540e\uff0c\u77e5\u9053\u4e86\u00a0<tt class=\"docutils literal\" style=\"color: #028dfa;\">itertools.groupby<\/tt>\u00a0\u8fd9\u4e2a\u51fd\u6570\u3002\u95ee\u9898\u662f\u5bf9\u4e00\u4e2alist\u4e2d\u7684\u65b0\u95fbid\u8fdb\u884c\u53bb\u91cd\uff0c<span style=\"color: #ff0000;\"><strong>\u53bb\u91cd\u4e4b\u540e\u8981\u4fdd\u8bc1\u987a\u5e8f\u4e0d\u53d8<\/strong><\/span>\u3002<\/div>\n<h6>\u76f4\u89c2\u65b9\u6cd5<\/h6>\n<div>\n<div id=\"id1\" class=\"section\" style=\"color: #333333;\">\n<p>\u6700\u7b80\u5355\u7684\u601d\u8def\u5c31\u662f\uff1a<\/p>\n<pre class=\"lang:default decode:true \">ids = [1,2,3,3,4,2,3,4,5,6,1]\nnews_ids = []\nfor id in ids:\n    if id not in news_ids:\n        news_ids.append(id)\n\nprint news_ids<\/pre>\n<p>\u8fd9\u6837\u4e5f\u53ef\u884c\uff0c\u4f46\u662f\u770b\u8d77\u6765\u4e0d\u591fPythonic\u3002<\/p>\n<h6>\u7528set<\/h6>\n<\/div>\n<div id=\"set\" class=\"section\" style=\"color: #333333;\">\n<p>\u53e6\u5916\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\u5c31\u662f\u7528set\uff1a<\/p>\n<pre class=\"lang:default decode:true\">ids = [1,4,3,3,4,2,3,4,5,6,1]\nids = list(set(ids))<\/pre>\n<p>\u8fd9\u6837\u7684\u7ed3\u679c\u662f<strong><span style=\"color: #ff0000;\">\u6ca1\u6709\u4fdd\u6301\u539f\u6765\u7684\u987a\u5e8f<\/span><\/strong>\u3002<\/p>\n<h6>\u6309\u7167\u7d22\u5f15\u518d\u6b21\u6392\u5e8f\uff08\u4e3a\u4e86\u4fdd\u6301\u539f\u6765\u7684\u987a\u5e8f\uff09<\/h6>\n<\/div>\n<div id=\"id2\" class=\"section\" style=\"color: #333333;\">\n<p>\u6700\u540e\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u89e3\u51b3\uff1a<\/p>\n<pre class=\"lang:default decode:true\">ids = [1,4,3,3,4,2,3,4,5,6,1]\nnews_ids = list(set(ids))\nnews_ids.sort(ids.index)<\/pre>\n<\/div>\n<h6 id=\"itertools-grouby\" class=\"section\" style=\"color: #333333;\">\u4f7f\u7528itertools.grouby<\/h6>\n<div class=\"section\" style=\"color: #333333;\">\n<p>\u6587\u7ae0\u4e00\u5f00\u59cb\u5c31\u63d0\u5230itertools.grouby, \u5982\u679c\u4e0d\u8003\u8651\u5217\u8868\u987a\u5e8f\u7684\u8bdd\u53ef\u7528\u8fd9\u4e2a\uff1a<\/p>\n<pre class=\"lang:default decode:true\">ids = [1,4,3,3,4,2,3,4,5,6,1]\nids.sort()\nit = itertools.groupby(ids)\n\nfor k, g in it:\n    print k<\/pre>\n<p>\u5173\u4e8eitertools.groupby\u7684\u539f\u7406\u53ef\u4ee5\u770b\u8fd9\u91cc\uff1a<a class=\"reference external\" style=\"color: #428bca;\" href=\"http:\/\/docs.python.org\/2\/library\/itertools.html#itertools.groupby\" target=\"_blank\">http:\/\/docs.python.org\/2\/library\/itertools.html#itertools.groupby<\/a><\/p>\n<h6><em>\u7f51\u53cb\u8865\u5145\uff1a\u7528reduce<\/em><\/h6>\n<\/div>\n<div id=\"reduce\" class=\"section\" style=\"color: #333333;\">\n<p>\u7f51\u53cbreatlk\u7559\u8a00\u7ed9\u4e86\u53e6\u5916\u7684\u89e3\u51b3\u65b9\u6848\u3002\u6211\u8865\u5145\u5e76\u89e3\u91ca\u5230\u8fd9\u91cc\uff1a<\/p>\n<pre class=\"lang:default decode:true \">&gt;&gt; ids = [1,4,3,3,4,2,3,4,5,6,1]\n&gt;&gt; func = lambda x,y:x if y in x else x + [y]\n&gt;&gt; reduce(func, [[], ] + ids)\n\nOutput: [1, 4, 3, 2, 5, 6]<\/pre>\n<p>\u4e0a\u9762\u662f\u6211\u5728ipython\u4e2d\u8fd0\u884c\u7684\u4ee3\u7801\uff0c\u5176\u4e2d\u7684\u00a0<tt class=\"docutils literal\" style=\"color: #028dfa;\">lambda x,y:x if y in x else x + [y]<\/tt>\u00a0\u7b49\u4ef7\u4e8e\u00a0<tt class=\"docutils literal\" style=\"color: #028dfa;\">lambda x,y: y in x and x or\u00a0<span class=\"pre\">x+[y]<\/span><\/tt>\u00a0\u3002<\/p>\n<p>\u601d\u8def\u5176\u5b9e\u5c31\u662f\u5148\u628aids\u53d8\u4e3a[[], 1,4,3,&#8230;&#8230;] ,\u7136\u540e\u5728\u5229\u7528reduce\u7684\u7279\u6027\u3002reduce\u89e3\u91ca\u53c2\u770b\u8fd9\u91cc\uff1a<a class=\"reference external\" style=\"color: #428bca;\" href=\"http:\/\/docs.python.org\/2\/library\/functions.html#reduce\" target=\"_blank\">http:\/\/docs.python.org\/2\/library\/functions.html#reduce<\/a><\/p>\n<p>=================<\/p>\n<h5>\u5b57\u5178(dict)<\/h5>\n<p>dict \u7528 {} \u5305\u56f4<br \/>\ndict.keys(),dict.values(),dict.items()<br \/>\nhash(obj)\u8fd4\u56deobj\u7684\u54c8\u5e0c\u503c\uff0c\u5982\u679c\u8fd4\u56de\u8868\u793a\u53ef\u4ee5\u4f5c\u4e3adict\u7684key<br \/>\ndel \u6216 dict.pop\u53ef\u4ee5\u5220\u9664\u4e00\u4e2aitem,clear\u6e05\u9664\u6240\u6709\u7684\u5185\u5bb9<br \/>\n<span style=\"color: #ff0000;\"><strong>sorted<\/strong><\/span>(dict)\u53ef\u4ee5\u628adict\u6392\u5e8f<br \/>\n<span style=\"color: #ff0000;\">dict.get()\u53ef\u4ee5\u67e5\u627e\u6ca1\u5b58\u5728\u7684key<\/span>\uff0cdict.[]\u4e0d\u53ef\u4ee5<br \/>\ndict.setdefault() \u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u542b\u6709\u67d0\u952e\u3002\u5982\u679c\u5b57\u5178\u4e2d\u8fd9\u4e2a\u952e\u5b58\u5728\uff0c\u4f60\u53ef\u4ee5\u53d6\u5230\u5b83\u7684\u503c\u3002 \u5982\u679c\u6240\u627e\u7684\u952e\u5728\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\uff0c\u4f60\u53ef\u4ee5\u7ed9\u8fd9\u4e2a\u952e\u8d4b\u9ed8\u8ba4\u503c\u5e76\u8fd4\u56de\u6b64\u503c\u3002<br \/>\n{}.fromkeys()\u521b\u5efa\u4e00\u4e2adict\uff0c\u4f8b\u5982: {}.fromkeys((&#8216;love&#8217;, &#8216;honor&#8217;), True) =&gt;{&#8216;love&#8217;: True, &#8216;honor&#8217;: True}<br \/>\n\u4e0d\u5141\u8bb8\u4e00\u4e2a\u952e\u5bf9\u5e94\u591a\u4e2a\u503c<br \/>\n\u952e\u503c\u5fc5\u987b\u662f\u54c8\u5e0c\u7684\uff0c\u7528hash()\u6d4b\u8bd5<br \/>\n\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5982\u679c\u5b9e\u73b0_hash()_\u65b9\u6cd5\u53ef\u4ee5\u4f5c\u4e3a\u952e\u503c\u4f7f\u7528<\/p>\n<h5>\u96c6\u5408(set)<\/h5>\n<div>\u96c6\u5408\u662f\u4e00\u4e2a\u6570\u5b66\u6982\u5ff5\uff0c\u7528set()\u521b\u5efa<br \/>\nset.add(), set.update(), set.remove()\uff0c\u6dfb\u52a0\u66f4\u65b0\u5220\u9664\uff0c-= \u53ef\u4ee5\u505aset\u51cf\u6cd5<br \/>\nset.discard \u548c set.remove\u4e0d\u540c\u5728\u4e8e\u5982\u679c\u5220\u9664\u7684\u5143\u7d20\u4e0d\u5728\u96c6\u5408\u5185\uff0cdiscard\u4e0d\u62a5\u9519\uff0cremove \u62a5\u9519<br \/>\n&lt; &lt;= \u8868\u793a \u5b50\u96c6\uff0c&gt; &gt;=\u8868\u793a\u8d85\u96c6<br \/>\n| \u8868\u793a\u8054\u5408 &amp; \u8868\u793a\u4ea4\u96c6 &#8211; \u8868\u793a\u5dee\u96c6 ^ \u5dee\u5206\u96c6<\/p>\n<h5>\u5217\u8868(list)<\/h5>\n<div>\u5217\u8868\u662f\u5e8f\u5217\u5bf9\u8c61\uff0c\u53ef\u5305\u542b\u4efb\u610f\u7684Python\u6570\u636e\u4fe1\u606f\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5217\u8868\u3001\u5143\u7ec4\u7b49\u3002\u5217\u8868\u7684\u6570\u636e\u662f\u53ef\u53d8\u7684\uff0c\u6211\u4eec\u53ef\u901a\u8fc7\u5bf9\u8c61\u65b9\u6cd5\u5bf9\u5217\u8868\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u589e\u52a0\u3001\u4fee\u6539\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u3002\u53ef\u4ee5\u901a\u8fc7list(seq)\u51fd\u6570\u628a\u4e00\u4e2a\u5e8f\u5217\u7c7b\u578b\u8f6c\u6362\u6210\u4e00\u4e2a\u5217\u8868\u3002<br \/>\nappend(x) \u5728\u5217\u8868\u5c3e\u90e8\u8ffd\u52a0\u5355\u4e2a\u5bf9\u8c61x\u3002\u4f7f\u7528\u591a\u4e2a\u53c2\u6570\u4f1a\u5f15\u8d77\u5f02\u5e38\u3002<br \/>\ncount(x) \u8fd4\u56de\u5bf9\u8c61x\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<br \/>\nextend(L) \u5c06\u5217\u8868L\u4e2d\u7684\u8868\u9879\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u8fd4\u56deNone\u3002<br \/>\nIndex(x) \u8fd4\u56de\u5217\u8868\u4e2d\u5339\u914d\u5bf9\u8c61x\u7684\u7b2c\u4e00\u4e2a\u5217\u8868\u9879\u7684\u7d22\u5f15\u3002\u65e0\u5339\u914d\u5143\u7d20\u65f6\u4ea7\u751f\u5f02\u5e38\u3002<br \/>\ninsert(i,x) \u5728\u7d22\u5f15\u4e3ai\u7684\u5143\u7d20\u524d\u63d2\u5165\u5bf9\u8c61x\u3002\u5982list.insert(0,x)\u5728\u7b2c\u4e00\u9879\u524d\u63d2\u5165\u5bf9\u8c61\u3002\u8fd4\u56deNone\u3002<br \/>\npop(x) \u5220\u9664\u5217\u8868\u4e2d\u7d22\u5f15\u4e3ax\u7684\u8868\u9879\uff0c\u5e76\u8fd4\u56de\u8be5\u8868\u9879\u7684\u503c\u3002\u82e5\u672a\u6307\u5b9a\u7d22\u5f15\uff0cpop\u8fd4\u56de\u5217\u8868\u6700\u540e\u4e00\u9879\u3002<br \/>\nremove(x) \u5220\u9664\u5217\u8868\u4e2d\u5339\u914d\u5bf9\u8c61x\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5339\u914d\u5143\u7d20\u65f6\u4ea7\u751f\u5f02\u5e38\u3002\u8fd4\u56deNone\u3002<br \/>\nreverse() \u98a0\u5012\u5217\u8868\u5143\u7d20\u7684\u987a\u5e8f\u3002<br \/>\nsort() \u5bf9\u5217\u8868\u6392\u5e8f\uff0c\u8fd4\u56denone\u3002bisect\u6a21\u5757\u53ef\u7528\u4e8e\u6392\u5e8f\u5217\u8868\u9879\u7684\u6dfb\u52a0\u548c\u5220\u9664\u3002<\/div>\n<h5>\u5143\u7ec4(tuple)<\/h5>\n<div>tuple=(1,)\uff0c\u8fd9\u662f\u5355\u4e2a\u5143\u7d20\u7684\u5143\u7ec4\u8868\u793a\uff0c\u9700\u52a0\u989d\u5916\u7684\u9017\u53f7\u3002<br \/>\ntuple=1\uff0c2\uff0c3\uff0c4\uff0c\u8fd9\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u5728\u4e0d\u4f7f\u7528\u5706\u62ec\u53f7\u800c\u4e0d\u4f1a\u5bfc\u81f4\u6df7\u6dc6\u65f6\uff0cPython\u5141\u8bb8\u4e0d\u4f7f\u7528\u5706\u62ec\u53f7\u7684\u5143\u7ec4\u3002<\/div>\n<div>\u548c\u5217\u8868\u4e00\u6837\uff0c\u53ef\u5bf9\u5143\u7ec4\u8fdb\u884c\u7d22\u5f15\u3001\u5206\u7247\u3001\u8fde\u63a5\u548c\u91cd\u590d\u3002\u4e5f\u53ef\u7528len()\u6c42\u5143\u7ec4\u957f\u5ea6\u3002<br \/>\n\u5143\u7ec4\u7684\u7d22\u5f15\u7528tuple[i]\u7684\u5f62\u5f0f\uff0c\u800c\u4e0d\u662ftuple(i)\u3002<br \/>\n\u548c\u5217\u8868\u7c7b\u4f3c\uff0c\u4f7f\u7528tuple(seq)\u53ef\u628a\u5176\u5b83\u5e8f\u5217\u7c7b\u578b\u8f6c\u6362\u6210\u5143\u7ec4\u3002<\/div>\n<div><\/div>\n<div>\u8bf4\u660e\uff1a\u8fd9\u7bc7\u6587\u7ae0\u662f\u4ece\u7f51\u4e0a\u641c\u96c6\u5230\u7684\u51e0\u7bc7\u6587\u7ae0\u5408\u6210\u800c\u6210\uff0c\u4e2d\u95f4\u7684\u90a3\u90e8\u5206\u539f\u6587\u5730\u5740\u5fd8\u4e86\u51fa\u5904\u4e86\uff0c\u6709\u673a\u4f1a\u518d\u8865\u4e0a\u3002<\/p>\n<hr \/>\n<\/div>\n<div><\/div>\n<div>\u53ef\u4ee5\u53c2\u89c1\u540e\u6765\u5728\u770b\u4e66\u7684\u8fc7\u7a0b\u4e2d\u4e86\u89e3\u5230\u7684\u4e00\u4e9b\u77e5\u8bc6\u7b14\u8bb0\uff1a<a href=\"http:\/\/ixyzero.com\/blog\/archives\/1284.html\" target=\"_blank\">http:\/\/ixyzero.com\/blog\/archives\/1284.html<\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u5217\u8868(list)\u3001\u5b57\u5178(dict)\u3001\u96c6\u5408(set) \u6570\u636e\u6392\u5e8f \u53c2\u8003\u94fe\u63a5\uff1ahttp:\/\/cinia [&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],"tags":[220,161,8,256,257],"class_list":["post-689","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","tag-dict","tag-list","tag-python","tag-set","tag-tuple"],"views":9217,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/689","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=689"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/689\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}