{"id":1461,"date":"2014-10-12T02:32:22","date_gmt":"2014-10-12T02:32:22","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1461"},"modified":"2014-10-12T02:32:22","modified_gmt":"2014-10-12T02:32:22","slug":"python%e7%9f%a5%e8%af%86%e7%a7%af%e7%b4%af_1","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1461.html","title":{"rendered":"Python\u77e5\u8bc6\u79ef\u7d2f_1"},"content":{"rendered":"<p>\u6700\u8fd1\u7528Python\u5904\u7406\u6570\u636e\u4ec0\u4e48\u7684\u8f83\u591a\uff0c\u671f\u95f4\u78b0\u5230\u4e86\u4e0d\u5c11\u5751\uff0c\u89c9\u5f97\u6709\u5fc5\u8981\u6574\u7406\u603b\u7ed3\u4e00\u4e0b\uff0c\u5148\u5b9a\u4e2a\u6846\u67b6\uff0c\u5185\u5bb9\u6162\u6162\u586b\u8865\uff1a<\/p>\n<h5>1.\u8bb0\u5f55Python\u811a\u672c\u7684\u6267\u884c\u65f6\u95f4\uff1b<\/h5>\n<p>\u7ecf\u5e38\u5728\u4e00\u4e9b\u6570\u636e\u5904\u7406\u8f83\u4e3a\u96c6\u4e2d\uff0c\u539f\u59cb\u6570\u636e\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\u9700\u8981\u5bf9\u6267\u884c\u7684Python\u811a\u672c\u8ba1\u7b97\/\u7edf\u8ba1\u5176\u6267\u884c\u65f6\u95f4\uff0c\u4ee5\u65b9\u4fbf\u9884\u4f30\u6574\u4e2a\u9879\u76ee\u8981\u82b1\u8d39\u7684\u65f6\u95f4\uff0c\u4e4b\u524d\u6ca1\u600e\u4e48\u78b0\u5230\u8fd9\u79cd\u9700\u6c42\uff0c\u8fd9\u51e0\u5929\u78b0\u5230\u4e86\u4e4b\u540e\uff0c\u5c31\u4e0a\u7f51\u53d6\u641c\u4e86\u4e00\u4e0b\uff0c\u627e\u5230\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528<\/strong><strong>Python<\/strong><strong>\u7684<\/strong><strong>time<\/strong><strong>\u6a21\u5757<\/strong><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">import time\nstart_time = time.time()\nmain()\nprint(\"--- %s seconds ---\" % time.time() - start_time)<\/pre>\n<ul>\n<li><strong>\u5bf9\u4e8e\u8f83\u5c0f\u7684\u4ee3\u7801\u7247\u6bb5\u53ef\u4ee5\u4f7f\u7528<\/strong><strong>Python<\/strong><strong>\u7684<\/strong><strong>timeit<\/strong><strong>\u6a21\u5757<\/strong><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt; import timeit\n#\u6267\u884c\u547d\u4ee4\n&gt;&gt;&gt; t2 = timeit.Timer('x=range(1000)')\n#\u663e\u793a\u65f6\u95f4\n&gt;&gt;&gt; t2.timeit()\n10.620039563513103\n\n#\u6267\u884c\u547d\u4ee4\n&gt;&gt;&gt; t1 = timeit.Timer('sum(x)', 'x = (i for i in range(1000))')\n#\u663e\u793a\u65f6\u95f4\n&gt;&gt;&gt; t1.timeit()\n0.1881566039438201<\/pre>\n<p><span style=\"color: #0000ff;\"><strong>\u5728IPython\u4e0b\u7684\u4f7f\u7528\u65b9\u6cd5\u4e3a<\/strong><\/span>\uff1a<\/p>\n<pre class=\"lang:default decode:true\">In [4]: %timeit y=map(lambda x:x**10, range(32))\n10000 loops, best of 3: 51.9 us per loop<\/pre>\n<ul>\n<li><strong>\u4f7f\u7528<\/strong><strong>IPython<\/strong><strong>\u7684<\/strong><strong>%run<\/strong><strong>\u9b54\u672f\u547d\u4ee4<\/strong><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">In [5]: %run -t py_deal_with_results.py<\/pre>\n<blockquote><p>%run has special flags for timing the execution of your scripts (-t), or for running them under the control of either Python\u2019s pdb debugger (-d) or profiler (-p).<\/p><\/blockquote>\n<h5>2.Python\u4e2d\u5e38\u89c1\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1b<\/h5>\n<h6>list\u64cd\u4f5c<\/h6>\n<p class=\"line874\">\u672c\u8d28\u4e0a\uff0c\u4e00\u4e2alist\u5217\u8868\u5c31\u662f\u7528array\u6570\u7ec4\u6765\u5448\u73b0\u7684\uff0c\u5b83\u7684\u6700\u5927\u82b1\u9500\u5728\u4e8e\u79fb\u52a8\u4e0d\u65ad\u589e\u957f\u7684\u5206\u914d\u7a7a\u95f4\u5927\u5c0f\uff0c\u56e0\u4e3a<strong><span style=\"color: #ff0000;\">\u5bf9\u4e8elist\u5217\u8868\u6765\u8bf4\u6240\u6709\u7684\u64cd\u4f5c\u90fd\u9700\u8981\u79fb\u52a8\u5143\u7d20\uff0c\u7279\u522b\u662f\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c<\/span><\/strong>\uff0c<strong>\u5982\u679c\u4f60\u9700\u8981\u5728\u4e24\u7aef\u63d2\u5165\/\u5220\u9664\u5143\u7d20\u65f6\uff0c\u8003\u8651\u4f7f\u7528collections.deque\u66ff\u4ee3list<\/strong><span id=\"line-18\" class=\"anchor\"><\/span><\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<p class=\"line891\"><strong>Operation<\/strong><\/p>\n<\/td>\n<td>\n<p class=\"line891\"><strong>Average Case<\/strong><\/p>\n<\/td>\n<td>\n<p class=\"line891\"><strong><a class=\"http\" href=\"http:\/\/en.wikipedia.org\/wiki\/Amortized_analysis\" target=\"_blank\">Amortized Worst Case<\/a><\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Copy<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Append[1]<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Insert<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Get Item<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Set Item<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Delete Item<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Iteration<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Get Slice<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(k)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(k)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Del Slice<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Set Slice<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(k+n)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(k+n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Extend[1]<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(k)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(k)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line891\"><a class=\"http\" href=\"http:\/\/svn.python.org\/projects\/python\/trunk\/Objects\/listsort.txt\">Sort<\/a><\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n log n)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n log n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Multiply<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(nk)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(nk)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">x in s<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">min(s), max(s)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Get Length<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h6 id=\"set\">set\u64cd\u4f5c<\/h6>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<p class=\"line891\"><strong>Operation<\/strong><\/p>\n<\/td>\n<td>\n<p class=\"line891\"><strong>Average case<\/strong><\/p>\n<\/td>\n<td>\n<p class=\"line891\"><strong>Worst Case<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">x in s<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Union s|t<\/p>\n<\/td>\n<td>\n<p class=\"line891\"><a class=\"nonexistent\" href=\"https:\/\/wiki.python.org\/moin\/TimeComplexity_%28SetCode%29\">O(len(s)+len(t))<\/a><\/p>\n<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Intersection s&amp;t<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(min(len(s), len(t))<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(len(s) * len(t))<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Difference s-t<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(len(s))<\/p>\n<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">s.difference_update(t)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(len(t))<\/p>\n<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Symmetric Difference s^t<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(len(s))<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(len(s) * len(t))<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">s.symmetric_difference_update(t)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(len(t))<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(len(t) * len(s))<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>set\u7684\u5b9e\u73b0\u548cdict\u5b57\u5178\u7684\u5b9e\u73b0\u975e\u5e38\u76f8\u4f3c\u3002<\/p>\n<h6 id=\"dict\">dict\u64cd\u4f5c<\/h6>\n<p class=\"line874\">\u5728\u6b64\u5047\u8bbePython\u4f7f\u7528\u7684Hash\u51fd\u6570\u80fd\u591f\u4f7f\u78b0\u649e\u53d1\u751f\u7684\u51e0\u7387\u975e\u5e38\u5c0f\uff0c\u6d4b\u8bd5\u7528\u4f8b\u4e5f\u975e\u5e38\u5e73\u5747\u3002<span id=\"line-91\" class=\"anchor\"><\/span><\/p>\n<p class=\"line874\">\u5728Python\u4e2d\u6709\u4e00\u79cd\u5feb\u901f\u6784\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\u2014\u2014{}\uff0c\u5b83\u5e76\u4e0d\u5f71\u54cd\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\uff0c\u4f46\u662f\u5b83\u80fd\u591f\u663e\u8457\u7684\u5f71\u54cd\u5e38\u6570\u56e0\u5b50\uff0c\u5982\u4f55\u5feb\u901f\u7684\u5b8c\u6210\u4e00\u4e2a\u5178\u578b\u7684\u7a0b\u5e8f\u3002<span id=\"line-95\" class=\"anchor\"><\/span><\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<p class=\"line891\"><strong>Operation<\/strong><\/p>\n<\/td>\n<td>\n<p class=\"line891\"><strong>Average Case<\/strong><\/p>\n<\/td>\n<td>\n<p class=\"line891\"><strong>Amortized Worst Case<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Copy[2]<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Get Item<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Set Item[1]<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Delete Item<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(1)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/p>\n<p class=\"line862\">Iteration[2]<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<td>\n<p class=\"line862\">O(n)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ece\u4e0a\u9762\u51e0\u5f20\u8868\u53ef\u4ee5\u770b\u51fa\uff0c\u5728list\u4e2d\u8981\u5c3d\u91cf\u907f\u514d\u4f7f\u7528insert()\/delete()\u7b49\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u5e38\u9ad8\u7684\u64cd\u4f5c\uff0c\u56e0\u4e3a\u5b83\u4eec\u6d89\u53ca\u5230\u5143\u7d20\u7684\u79fb\u52a8\uff0c\u4f1a\u8017\u8d39\u65f6\u95f4\uff1b\u5728\u6d89\u53ca\u5230\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u5b58\u5728{\u67e5\u627e}\u7684\u60c5\u51b5\uff0c\u6700\u597d\u4f7f\u7528dict\/set\u6765\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ea\u6709O(1)\u3002<\/p>\n<p>\u5728Python\u4e2d\u4f7f\u7528list\u548cdict\u4f5c\u4e3a\u67e5\u627e\u8868\u7684\u65f6\u95f4\u3001\u7a7a\u95f4\u6548\u7387\u5bf9\u6bd4\uff1a<a href=\"http:\/\/stackoverflow.com\/questions\/513882\/python-list-vs-dict-for-look-up-table\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/513882\/python-list-vs-dict-for-look-up-table<\/a><\/p>\n<\/div>\n<h5>3.Python\u7684\u5217\u8868\u3001\u96c6\u5408\u64cd\u4f5c\u5c0f\u7ed3\uff1b<\/h5>\n<p>\u5728Python\u4e2d\uff0c\u5411List\u6dfb\u52a0\u5143\u7d20\uff0c\u6709\u5982\u4e0b4\u79cd\u65b9\u6cd5\uff08append(),extend(),insert(), +\u52a0\u53f7\uff09\uff1a<\/p>\n<ol>\n<li>append() \u8ffd\u52a0\u5355\u4e2a\u5143\u7d20\u5230List\u7684\u5c3e\u90e8\uff0c\u53ea\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u53c2\u6570\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u88ab\u8ffd\u52a0\u7684\u5143\u7d20\u5728List\u4e2d\u4fdd\u6301\u7740\u539f\u7ed3\u6784\u7c7b\u578b\u3002\u6b64\u5143\u7d20\u5982\u679c\u662f\u4e00\u4e2alist\uff0c\u90a3\u4e48\u8fd9\u4e2alist\u5c06\u4f5c\u4e3a\u4e00\u4e2a\u6574\u4f53\u8fdb\u884c\u8ffd\u52a0\uff0c<strong><span style=\"color: #ff0000;\">\u6ce8\u610fappend()\u548cextend()\u7684\u533a\u522b<\/span><\/strong>\u3002<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt; list1=['a','b']\n&gt;&gt;&gt; list1.append('c')\n&gt;&gt;&gt; list1\n['a', 'b', 'c']<\/pre>\n<ol start=\"2\">\n<li>extend() \u5c06\u4e00\u4e2a\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u5206\u522b\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u53ea\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff1bextend()\u76f8\u5f53\u4e8e\u662f\u5c06list B \u8fde\u63a5\u5230list A\u4e0a\u3002<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt; list1\n['a', 'b', 'c']\n&gt;&gt;&gt; list1.extend('d')\n&gt;&gt;&gt; list1\n['a', 'b', 'c', 'd']<\/pre>\n<ol start=\"3\">\n<li>insert() \u5c06\u4e00\u4e2a\u5143\u7d20\u63d2\u5165\u5230\u5217\u8868\u4e2d\uff0c\u4f46\u5176\u53c2\u6570\u6709\u4e24\u4e2a\uff08\u5982insert(1,\u201dg\u201d)\uff09\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7d22\u5f15\u70b9\uff0c\u5373\u63d2\u5165\u7684\u4f4d\u7f6e\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u63d2\u5165\u7684\u5143\u7d20\u3002<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt; list1\n['a', 'b', 'c', 'd']\n&gt;&gt;&gt; list1.insert(1,'x')\n&gt;&gt;&gt; list1\n['a', 'x', 'b', 'c', 'd']<\/pre>\n<ol start=\"4\">\n<li>+ \u52a0\u53f7\uff0c\u5c06\u4e24\u4e2alist\u76f8\u52a0\uff0c\u4f1a\u8fd4\u56de\u5230\u4e00\u4e2a\u65b0\u7684list\u5bf9\u8c61\uff0c\u6ce8\u610f\u4e0e\u524d\u4e09\u79cd\u7684\u533a\u522b\u3002<strong><span style=\"color: #ff0000;\">\u524d\u9762\u4e09\u79cd\u65b9\u6cd5\uff08append, extend, insert\uff09\u53ef\u5bf9\u5217\u8868\u589e\u52a0\u5143\u7d20\u7684\u64cd\u4f5c\uff0c\u4ed6\u4eec\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u662f\u76f4\u63a5\u4fee\u6539\u4e86\u539f\u6570\u636e\u5bf9\u8c61\u3002<\/span><\/strong>\u6ce8\u610f\uff1a\u5c06\u4e24\u4e2alist\u76f8\u52a0\uff0c\u9700\u8981\u521b\u5efa\u65b0\u7684list\u5bf9\u8c61\uff0c\u4ece\u800c\u9700\u8981\u6d88\u8017\u989d\u5916\u7684\u5185\u5b58\uff0c\u7279\u522b\u662f\u5f53list\u8f83\u5927\u65f6\uff0c\u5c3d\u91cf\u4e0d\u8981\u4f7f\u7528\u201c+\u201d\u6765\u6dfb\u52a0list\uff0c\u800c\u5e94\u8be5\u5c3d\u53ef\u80fd\u4f7f\u7528List\u7684append()\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true \">&gt;&gt;&gt; list1\n['a', 'x', 'b', 'c', 'd']\n&gt;&gt;&gt; list2=['y','z']\n&gt;&gt;&gt; list3=list1+list2\n&gt;&gt;&gt; list3\n['a', 'x', 'b', 'c', 'd', 'y', 'z']<\/pre>\n<p>append()\u65b9\u6cd5\u548c\u201c+\u201d\u65b9\u6cd5\u7684\u6027\u80fd\u5bf9\u6bd4\uff1a<\/p>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/725782\/in-python-what-is-the-difference-between-append-and\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/725782\/in-python-what-is-the-difference-between-append-and<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/5663213\/python-append-performance\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/5663213\/python-append-performance<\/a><\/li>\n<\/ul>\n<hr \/>\n<p>\u96c6\u5408\u64cd\u4f5c\uff08intersection()\/difference()\uff09\uff1a<\/p>\n<pre class=\"lang:default decode:true\">print list(set(set1).difference(set(set2))) # in set1 not in set2\nprint list(set(set1).intersection(set(set2))) # in both set1 and set2<\/pre>\n<h5>4.Python\u4e2d\u7684\u5b57\u5178\u64cd\u4f5c\u53ca\u76f8\u5173\u8fd0\u7528\uff1b<\/h5>\n<pre class=\"lang:default decode:true\">def fileToDict(fileName):\n    return {k.strip():v.strip() for k, v in (l.split() for l in open(fileName))}\nipAll = fileToDict('ip_with_counts.txt')\nprint len(dic)\n\ndic = {}\nfor line in open('ip_with_counts.txt'):\n    dic[line.split()[1].strip()] = line.split()[0].strip()\n\nipAll = [line for line in open('ip_with_counts.txt')]\nprint len(ipAll)\n\ntop = [line.strip() for line in open('top.txt')]\nfor ip in top:\n    if ip.split()[1].strip() in dic:\n        print ip +\"t\"+ dic.get(ip.split()[1].strip())\n    else:\n        print ip +\"t\"+\"0\"<\/pre>\n<p>\u56e0\u4e3adict\u7528\u5230\u4e86Hash\u7b97\u6cd5\uff0c\u6240\u4ee5\u5728dict\u4e2d\u67e5\u627e\u5143\u7d20\/\u5224\u65ad\u662f\u5426\u5b58\u5728\uff0c\u901f\u5ea6\u5f88\u5feb\uff08\u6b64\u65f6\u8981\u5c3d\u91cf\u907f\u514d\u4f7f\u7528list\uff09\uff1b\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e862\u79cd\u65b9\u6cd5\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u81f3\u5b57\u5178\uff0c\u7b2c\u4e00\u79cd\u662f\u6bd4\u8f83Pythonic\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u65e0\u6cd5\u5c06\u6240\u6709\u7684\u5185\u5bb9\u8bfb\u53d6\uff0c\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u867d\u7136\u6bd4\u8f83\u539f\u59cb\uff0c\u4f46\u5b9e\u9645\u4f7f\u7528\u6548\u679c\u662f\u6700\u597d\u7684\uff0c\u8c28\u8bb0\u2014\u2014<span style=\"color: #ff0000;\"><strong>\u80fd\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\u5c31\u662f\u597d\u65b9\u6cd5<\/strong><\/span>\uff0c\u8c01\u7ba1\u5b83\u662f\u4e0d\u662f\u597d\u770b\uff1f<\/p>\n<p>\u5728Python\u4e2d\uff0cdict\u5b57\u5178\u6211\u7528\u7684\u6700\u591a\u7684\u5730\u65b9\u5c31\u662f\u53bb\u91cd&amp;\u8ba1\u6570\uff1a<\/p>\n<pre class=\"lang:default decode:true\">def get_counts(sequence):\n    counts = {}\n    for x in sequence:\n        if x in counts:\n            counts[x] += 1\n        else:\n            counts[x] = 1\n    return counts\n####\n\nfrom collections import defaultdict\ndef get_counts2(sequence):\n    counts = defaultdict(int)\n    for x in sequence:\n        counts[x] += 1\n    return counts\n####\n\ndef top_counts(count_dict, n=10):\n    value_key_pairs = [(count, tz) for tz,count in count_dict.items()]\n    value_key_pairs.sort()\n    return value_key_pairs[-n:]\n####\n\nfrom collections import Counter\ncounts = Counter(time_zones)\ncounts.most_common(10)<\/pre>\n<h5>5.Python\u6587\u4ef6\u8bfb\u53d6\u7684\u4e00\u4e9b\u4f8b\u5b50\uff1b<\/h5>\n<pre class=\"lang:default decode:true\">rawData = [line.strip() for line in open('ipList.txt')]\n\ntop1000 = [line.strip() for line in open('topRate.txt').readlines()[:1000]]\n\nfor line in sys.stdin:\n    line = line.strip()\n\nfor line in open('fileName.txt'):\n    line = line.strip()<\/pre>\n<h5>6.Python\u7684\u5b57\u7b26\u4e32\u52a0\u89e3\u5bc6\u64cd\u4f5c\uff08hashlib\u3001base64\uff09\uff1b<\/h5>\n<pre class=\"lang:default decode:true \">import hashlib\n\na = \"a test string\"\nprint 'md5 = %s' % (hashlib.md5(a).hexdigest(),)\nprint 'sha1 = %s' % (hashlib.sha1(a).hexdigest(),)\nprint 'sha224 = %s' % (hashlib.sha224(a).hexdigest(),)\nprint 'sha256 = %s' % (hashlib.sha256(a).hexdigest(),)\nprint 'sha384 = %s' % (hashlib.sha384(a).hexdigest(),)\nprint 'sha512 = %s' % (hashlib.sha512(a).hexdigest(),)<\/pre>\n<pre class=\"lang:default decode:true\">import base64\ntest = 'a test str'\n\n#\u8f6c\u6210bytes string\nbytesString = test.encode(encoding=\"utf-8\")\nprint(bytesString)\n\n#base64 \u7f16\u7801\nencodestr = base64.b64encode(bytesString)\nprint(encodestr)\nprint(encodestr.decode())\n\n#\u89e3\u7801\ndecodestr = base64.b64decode(encodestr)\nprint(decodestr.decode())<\/pre>\n<h5>7.\u7528Python\u64cd\u4f5cMySQL\u6570\u636e\u5e93\uff1b<\/h5>\n<pre class=\"lang:default decode:true\">import MySQLdb\nconn = MySQLdb.connect(host=\"127.0.0.1\",user=\"root\",passwd=\"pass111\",db=\"test\",charset=\"utf8\")\n\ncur = conn.cursor()\n\ncur.execute(\"insert into users (username,password,email) values (%s,%s,%s)\",(\"python\",\"123456\",\"python@gmail.com\"))\nconn.commit()\n\ncur.executemany(\"insert into users (username,password,email) values (%s,%s,%s)\",((\"google\",\"111222\",\"g@gmail.com\"),(\"facebook\",\"222333\",\"f@face.book\"),(\"github\",\"333444\",\"git@hub.com\"),(\"docker\",\"444555\",\"doc@ker.com\")))\nconn.commit()<\/pre>\n<p>\u5728Ubuntu\u4e0a\u5b89\u88c5MySQLdb\uff1a<\/p>\n<p><a href=\"http:\/\/zhoujianghai.iteye.com\/blog\/1520666\" target=\"_blank\">http:\/\/zhoujianghai.iteye.com\/blog\/1520666<\/a><\/p>\n<h5>8.\u7528Python\u751f\u6210\u9a8c\u8bc1\u7801\uff1b<\/h5>\n<p>\u53c2\u8003\uff1a<a href=\"http:\/\/www.zouyesheng.com\/captcha.html\" target=\"_blank\">\u5728Python\u4e2d\u7528PIL\u505a\u9a8c\u8bc1\u7801<\/a><\/p>\n<p>&nbsp;<\/p>\n<h6>\u4e00\u4e9b\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/wiki.python.org\/moin\/TimeComplexity\" target=\"_blank\">TimeComplexity &#8211; Python Wiki<\/a><\/li>\n<li><a href=\"%20https:\/\/wiki.python.org\/moin\/PythonSpeed\/PerformanceTips\" target=\"_blank\">PythonSpeed\/PerformanceTips &#8211; Python Wiki<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1557571\/how-to-get-time-of-a-python-program-execution\">http:\/\/stackoverflow.com\/questions\/1557571\/how-to-get-time-of-a-python-program-execution<\/a><\/li>\n<li><a href=\"http:\/\/ipython.org\/ipython-doc\/stable\/interactive\/tutorial.html#running-and-editing\">http:\/\/ipython.org\/ipython-doc\/stable\/interactive\/tutorial.html#running-and-editing<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/16118621\/first-common-element-from-two-lists\">http:\/\/stackoverflow.com\/questions\/16118621\/first-common-element-from-two-lists<\/a><\/li>\n<li><a href=\"https:\/\/bradmontgomery.net\/blog\/2013\/04\/01\/pythons-zip-map-and-lambda\/\">https:\/\/bradmontgomery.net\/blog\/2013\/04\/01\/pythons-zip-map-and-lambda\/<\/a><\/li>\n<li><a href=\"http:\/\/www.saltycrane.com\/blog\/2008\/04\/how-to-use-pythons-enumerate-and-zip-to\/\">http:\/\/www.saltycrane.com\/blog\/2008\/04\/how-to-use-pythons-enumerate-and-zip-to\/<\/a><\/li>\n<li><a href=\"http:\/\/segmentfault.com\/blog\/qiwsir\/1190000000709735\">http:\/\/segmentfault.com\/blog\/qiwsir\/1190000000709735<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u7528Python\u5904\u7406\u6570\u636e\u4ec0\u4e48\u7684\u8f83\u591a\uff0c\u671f\u95f4\u78b0\u5230\u4e86\u4e0d\u5c11\u5751\uff0c\u89c9\u5f97\u6709\u5fc5\u8981\u6574\u7406\u603b\u7ed3\u4e00\u4e0b\uff0c\u5148\u5b9a\u4e2a\u6846\u67b6\uff0c\u5185\u5bb9\u6162\u6162\u586b\u8865\uff1a 1 [&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":[369,370,8,371],"class_list":["post-1461","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-base64","tag-mysqldb","tag-python","tag-timecomplexity"],"views":3542,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1461","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=1461"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1461\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}