{"id":824,"date":"2014-08-03T02:53:59","date_gmt":"2014-08-03T02:53:59","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=824"},"modified":"2014-08-03T02:53:59","modified_gmt":"2014-08-03T02:53:59","slug":"%e4%b8%80%e4%ba%9b%e5%a5%bd%e7%8e%a9%e7%9a%84python%e7%89%87%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/824.html","title":{"rendered":"\u4e00\u4e9b\u597d\u73a9\u7684Python\u7247\u6bb5"},"content":{"rendered":"<div>\n<h5>Python\u7279\u6b8a\u8bed\u6cd5\uff1afilter\u3001map\u3001reduce\u3001lambda<\/h5>\n<p><span style=\"color: #000000;\"><strong>filter(function, sequence)<\/strong><\/span>\uff1a<span style=\"color: #ff0000;\">\u5bf9sequence\u4e2d\u7684item\u4f9d\u6b21\u6267\u884cfunction(item)\uff0c\u5c06\u6267\u884c\u7ed3\u679c\u4e3aTrue\u7684item\u7ec4\u6210\u4e00\u4e2aList\/String\/Tuple\uff08\u53d6\u51b3\u4e8esequence\u7684\u7c7b\u578b\uff09\u8fd4\u56de<\/span>\uff1a<br \/>\n&gt;&gt;&gt; def f(x): return x % 2 != 0 and x % 3 != 0<br \/>\n&gt;&gt;&gt; filter(f, range(2, 25))<br \/>\n[5, 7, 11, 13, 17, 19, 23]<br \/>\n&gt;&gt;&gt; def f(x): return x != &#8216;a&#8217;<br \/>\n&gt;&gt;&gt; filter(f, &#8220;abcdef&#8221;)<br \/>\n&#8216;bcdef&#8217;<\/p>\n<p><span style=\"color: #000000;\"><strong>map(function, sequence)<\/strong><\/span>\uff1a<span style=\"color: #ff0000;\">\u5bf9sequence\u4e2d\u7684item\u4f9d\u6b21\u6267\u884cfunction(item)\uff0c\u89c1\u6267\u884c\u7ed3\u679c\u7ec4\u6210\u4e00\u4e2aList\u8fd4\u56de<\/span>\uff1a<br \/>\n&gt;&gt;&gt; def cube(x): return x*x*x<br \/>\n&gt;&gt;&gt; map(cube, range(1, 11))<br \/>\n[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]<br \/>\n&gt;&gt;&gt; def cube(x) : return x + x<br \/>\n&#8230;<br \/>\n&gt;&gt;&gt; map(cube , &#8220;abcde&#8221;)<br \/>\n[&#8216;aa&#8217;, &#8216;bb&#8217;, &#8216;cc&#8217;, &#8216;dd&#8217;, &#8216;ee&#8217;]<br \/>\n\u53e6\u5916map\u4e5f\u652f\u6301\u591a\u4e2asequence\uff0c\u8fd9\u5c31\u8981\u6c42function\u4e5f\u652f\u6301\u76f8\u5e94\u6570\u91cf\u7684\u53c2\u6570\u8f93\u5165\uff1a<br \/>\n&gt;&gt;&gt; def add(x, y): return x+y<br \/>\n&gt;&gt;&gt; map(add, range(8), range(8))<br \/>\n[0, 2, 4, 6, 8, 10, 12, 14]<\/p>\n<p><span style=\"color: #000000;\"><strong>reduce(function, sequence, starting_value)<\/strong><\/span>\uff1a<span style=\"color: #ff0000;\">\u5bf9sequence\u4e2d\u7684item\u987a\u5e8f\u8fed\u4ee3\u8c03\u7528function<\/span>\uff0c\u5982\u679c\u6709starting_value\uff0c\u8fd8\u53ef\u4ee5\u4f5c\u4e3a\u521d\u59cb\u503c\u8c03\u7528\uff0c\u4f8b\u5982\u53ef\u4ee5\u7528\u6765\u5bf9List\u6c42\u548c\uff1a<br \/>\n&gt;&gt;&gt; def add(x,y): return x + y<br \/>\n&gt;&gt;&gt; reduce(add, range(1, 11))<br \/>\n55 \uff08\u6ce8\uff1a1+2+3+4+5+6+7+8+9+10\uff09<br \/>\n&gt;&gt;&gt; reduce(add, range(1, 11), 20)<br \/>\n75 \uff08\u6ce8\uff1a1+2+3+4+5+6+7+8+9+10+20\uff09<\/p>\n<p><span style=\"color: #000000;\"><strong>lambda<\/strong><\/span>\uff1a\u8fd9\u662fPython\u652f\u6301\u4e00\u79cd\u6709\u8da3\u7684\u8bed\u6cd5\uff0c<span style=\"color: #ff0000;\">\u5b83\u5141\u8bb8\u4f60\u5feb\u901f\u5b9a\u4e49\u5355\u884c\u7684\u6700\u5c0f\u51fd\u6570\uff0c\u7c7b\u4f3c\u4e0eC\u8bed\u8a00\u4e2d\u7684\u5b8f\uff0c\u8fd9\u4e9b\u53eb\u505alambda\u7684\u51fd\u6570\uff0c\u662f\u4eceLISP\u501f\u7528\u6765\u7684\uff0c\u53ef\u4ee5\u7528\u5728\u4efb\u4f55\u9700\u8981\u51fd\u6570\u7684\u5730\u65b9<\/span>\uff1a<br \/>\n&gt;&gt;&gt; g = lambda x: x * 2<br \/>\n&gt;&gt;&gt; g(3)<br \/>\n6<br \/>\n&gt;&gt;&gt; (lambda x: x * 2)(3)<br \/>\n6<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/hi.baidu.com\/sethc5\/item\/463772dad68b4081260ae78b\" target=\"_blank\">Python\u7279\u6b8a\u8bed\u6cd5\uff1afilter\u3001map\u3001reduce\u3001lambda<\/a><\/li>\n<li><a href=\"https:\/\/bradmontgomery.net\/blog\/2013\/04\/01\/pythons-zip-map-and-lambda\/\" target=\"_blank\">Python&#8217;s 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\/\" target=\"_blank\">How to use Python&#8217;s enumerate and zip to iterate over two lists and their indices.<\/a><\/li>\n<\/ul>\n<h5>python\u81ea\u52a8\u7ed9\u6570\u5b57\u524d\u9762\u88650\u7684\u65b9\u6cd5<\/h5>\n<\/div>\n<div>\n<p><span style=\"font-size: medium;\">python\u4e2d\u6709\u4e00\u4e2a<strong><span style=\"color: #ff0000;\">zfill\u65b9\u6cd5<\/span><\/strong>\u7528\u6765\u7ed9\u5b57\u7b26\u4e32\u524d\u9762\u88650\uff0c\u975e\u5e38\u6709\u7528<\/span><\/p>\n<pre class=\"lang:default decode:true \">n = \"123\"\ns = n.zfill(5)\nassert s == \"00123\"<\/pre>\n<p>zfill()\u4e5f\u53ef\u4ee5\u7ed9\u8d1f\u6570\u88650<\/p>\n<pre class=\"lang:default decode:true \">n = \"-123\"\ns = n.zfill(5)\nassert s == \"-0123\"<\/pre>\n<p>\u5bf9\u4e8e\u7eaf\u6570\u5b57\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u683c\u5f0f\u5316\u7684\u65b9\u5f0f\u6765\u88650<\/p>\n<pre class=\"lang:default decode:true\">n = 123\ns = \"%05d\" % n\nassert s == \"00123\"<\/pre>\n<h6>\u5b9e\u9645\u6d4b\u8bd5\u6548\u679c\u5982\u4e0b\uff1a<\/h6>\n<\/div>\n<div>&gt;&gt;&gt; n=&#8221;123&#8243;<br \/>\n&gt;&gt;&gt; n<br \/>\n&#8216;123&#8217;<br \/>\n&gt;&gt;&gt; s = n.zfill(5)<br \/>\n&gt;&gt;&gt; s<br \/>\n&#8216;00123&#8217;<br \/>\n&gt;&gt;&gt;<br \/>\n&gt;&gt;&gt; p=&#8221;-123&#8243;<br \/>\n&gt;&gt;&gt; p<br \/>\n&#8216;-123&#8217;<br \/>\n&gt;&gt;&gt; q = p.zfill(5)<br \/>\n&gt;&gt;&gt; q<br \/>\n&#8216;-0123&#8217;<br \/>\n&gt;&gt;&gt;<br \/>\n&gt;&gt;&gt; x=123<br \/>\n&gt;&gt;&gt; y = &#8220;%05d&#8221; % x<br \/>\n&gt;&gt;&gt; y<br \/>\n&#8216;00123&#8217;<\/p>\n<hr \/>\n<\/div>\n<div>\n<h5>Python\u4e2d\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f<\/h5>\n<\/div>\n<div>\n<h6>1\u3001sort()\u51fd\u6570<\/h6>\n<p>sort()\u51fd\u6570\u4f7f\u7528\u56fa\u5b9a\u7684\u6392\u5e8f\u7b97\u6cd5\u5bf9\u5217\u8868\u6392\u5e8f\u3002<strong><span style=\"color: #ff0000; font-size: medium;\">sort()\u51fd\u6570\u5bf9\u5217\u8868\u6392\u5e8f\u65f6\u6539\u53d8\u4e86\u539f\u6765\u7684\u5217\u8868\uff0c\u4ece\u800c\u8ba9\u5176\u4e2d\u7684\u5143\u7d20\u80fd\u6309\u4e00\u5b9a\u7684\u987a\u5e8f\u6392\u5217\uff0c\u800c\u4e0d\u662f\u7b80\u5355\u7684\u8fd4\u56de\u4e00\u4e2a\u5df2\u6392\u5e8f\u7684\u5217\u8868\u526f\u672c\u3002<\/span><\/strong><\/p>\n<p><span style=\"color: #ff0000; font-size: medium;\">\u6ce8\u610fsort()\u51fd\u6570\u6539\u53d8\u539f\u6765\u7684\u5217\u8868\uff0c\u51fd\u6570\u8fd4\u56de\u503c\u662f\u7a7a\u503c\u5373None\u3002<\/span>\u56e0\u6b64\uff0c\u5982\u679c\u9700\u8981\u4e00\u4e2a\u5df2\u6392\u597d\u5e8f\u7684\u5217\u8868\u526f\u672c\uff0c\u540c\u65f6\u53c8\u8981\u4fdd\u7559\u539f\u6709\u5217\u8868\u4e0d\u53d8\u7684\u65f6\u5019\uff0c\u5c31\u4e0d\u80fd\u76f4\u63a5\u7b80\u5355\u7684\u4f7f\u7528sort()\u51fd\u6570\u3002\u4e3a\u4e86\u5b9e\u73b0\u4e0a\u8ff0\u529f\u80fd\u4f7f\u7528sort()\u7684\u65b9\u6cd5\u662f\uff1a\u5148\u83b7\u53d6\u5217\u8868X\u7684\u526f\u672cY\uff0c\u7136\u540e\u518d\u5bf9Y\u8fdb\u884c\u6392\u5e8f\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">x=[4,6,2,1,7,9,4]\ny=x[:]\ny.sort()\nprint x\nprint y<\/pre>\n<p>\u7ed3\u679c\uff1a<br \/>\n[4, 6, 2, 1, 7, 9, 4]<br \/>\n[1, 2, 4, 4, 6, 7, 9]<\/p>\n<p><span style=\"color: #ff0000;\"><strong><span style=\"font-size: medium;\">\u8bf4\u660e\uff1a<\/span><\/strong>\u8c03\u7528x[:]\u5f97\u5230\u7684\u662f\u5305\u542b\u4e86x\u6240\u6709\u5143\u7d20\u7684\u5206\u7247\uff0c\u8fd9\u662f\u4e00\u79cd\u5f88\u6709\u6548\u7387\u7684\u590d\u5236\u6574\u4e2a\u5217\u8868\u7684\u65b9\u6cd5\u3002\u901a\u8fc7y=x\u7b80\u5355\u7684\u5c06x\u590d\u5236\u7ed9y\u662f\u6ca1\u6709\u7528\u7684\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u5c31\u8ba9x\u548cy\u90fd\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u5217\u8868\u4e86\u3002<\/span><\/p>\n<h6>2\u3001sorted()\u51fd\u6570<\/h6>\n<p>\u53e6\u5916\u4e00\u79cd<span style=\"color: #ff0000;\"><strong>\u83b7\u53d6\u5df2\u6392\u5e8f\u7684\u5217\u8868\u526f\u672c<\/strong><\/span>\u7684\u65b9\u6cd5\u662f\u4f7f\u7528sorted()\u51fd\u6570\u3002\u6ce8\u610f\uff0csorted()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u4efb\u4f55\u53ef\u8fed\u4ee3\u7684\u5bf9\u8c61\u3002<\/p>\n<pre class=\"lang:default decode:true\">x=[4,6,2,1,7,9,4]\ny=sorted(x)\nprint x\nprint y\n#\u7ed3\u679c\uff1a\n[4,6,2,1,7,9,4]\n[1,2,4,4,6,7,9]<\/pre>\n<\/div>\n<div><span style=\"color: #000000; font-size: medium;\">\u5b9e\u9645\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff1a<\/span><\/div>\n<div>\n<pre class=\"lang:default decode:true  \">&gt;&gt;&gt; x=[4,6,2,1,7,9,4]\n&gt;&gt;&gt; y=sorted(x)\n&gt;&gt;&gt; print x\n[4, 6, 2, 1, 7, 9, 4]\n&gt;&gt;&gt; print y\n[1, 2, 4, 4, 6, 7, 9]\n&gt;&gt;&gt;\n&gt;&gt;&gt; x=[4,6,2,1,7,9,4]\n&gt;&gt;&gt; y=x[:]\n&gt;&gt;&gt; print y\n[4, 6, 2, 1, 7, 9, 4]\n&gt;&gt;&gt; y.sort()\n&gt;&gt;&gt; print x\n[4, 6, 2, 1, 7, 9, 4]\n&gt;&gt;&gt; print y\n[1, 2, 4, 4, 6, 7, 9]<\/pre>\n<\/div>\n<h6># <span style=\"color: #ff0000;\">\u751f\u6210100\u4e2a0\u5230100\u7684\u968f\u673a\u6570\uff0c\u5e76\u6311\u51fa\u5176\u4e2d\u5927\u4e8e40\u5c0f\u4e8e60\u7684<\/span><\/h6>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt; from random import random\n&gt;&gt;&gt; [y for y in [randint(x-x, 100) for x in range(100)] if 40&lt;y&lt;60]\n[42, 47, 49, 50, 52, 58, 41, 46, 49, 50, 51, 51, 55, 42, 52, 48, 53, 56]\n&gt;&gt;&gt; [y for y in [randint(x-x, 100) for x in range(100)] if 40&lt;y&lt;60]\n[56, 52, 45, 52, 42, 54, 42, 50, 55, 48, 57, 53, 44, 46, 42, 53, 48, 44, 50, 49]<\/pre>\n<hr \/>\n<h5>\u8c03\u7528WindowsAPI\u9501\u5b9a\u8ba1\u7b97\u673a\uff1b\u8bfb\u53d6zip\u538b\u7f29\u6587\u4ef6<\/h5>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n#-*- coding:cp936 -*-\n'''\u8c03\u7528WindowAPI\u9501\u5b9a\u8ba1\u7b97\u673a'''\nimport ctypes\ndll = ctypes.WinDLL('user32.dll')\ndll.LockWorkStation()\n\n\nimport os, sys\n\n#\u4e2d\u6587\u5b57\u7b26\u96c6\ndef encodeChinese(msg):\n    type = sys.getfilesystemencoding()\n    return msg.decode('UTF-8').encode(type)\n\n#\u68c0\u6d4b\u6587\u4ef6\u540e\u7f00,\u8fd4\u56de\u6587\u4ef6\u540e\u7f00\ndef check_fileMode(fileName):\n    fPostfix = os.path.splitext(fileName)[1]\n    return fPostfix\n\n#\u6253\u5f00\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6\ndef open_imgFile(fileName):\n    im = Image.open(fileName)\n    im.load()\n    return im\n\nimport zipfile\nz = zipfile.ZipFile(\"20140801.zip\", \"r\")\n#\u6253\u5370zip\u6587\u4ef6\u4e2d\u7684\u6587\u4ef6\u5217\u8868\nfor filename in z.namelist():\n    print 'File:', filename\n\n#\u8bfb\u53d6zip\u6587\u4ef6\u4e2d\u7684\u7b2c\u4e00\u4e2a\u6587\u4ef6\nfirst_file_name = z.namelist()[0]\ncontent = z.read(first_file_name)\t#\u8bfb\u53d6zip\u538b\u7f29\u6587\u4ef6\u4e2d\u6307\u5b9a\u6587\u4ef6\u7684\u5185\u5bb9\nprint first_file_name\nprint content\n\n\ndef Fibonacci_s(n):\n    Fb = []\n    a, b = 0, 1\n    while b&lt;n:\n        Fb.append(b)\n        a, b = b, a+b\n    return Fb\nprint Fibonacci_s(1000)\t#\u6253\u53701000\u4ee5\u5185\u7684\u6590\u6ce2\u7eb3\u6308\u6570\u5217<\/pre>\n<h5>\u7528Python\u753b\u5fc3\u5f62\u56fe\u50cf\u76844\u79cd\u516c\u5f0f\/\u65b9\u6cd5<\/h5>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n# -*- coding: utf-8 -*-\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n'''\n'17*x^2 - 16*|x|*y + 17*y^2 = 225'\n'''\nX = np.arange(-5.0, 5.0, 0.1)\nY = np.arange(-5.0, 5.0, 0.1)\nx, y = np.meshgrid(X, Y)\nf = 17 * x ** 2 - 16 * np.abs(x) * y + 17 * y ** 2 - 225\nfig = plt.figure()\ncs = plt.contour(x, y, f, 0, colors = 'r')\nplt.show()\n\n\n'''\n'(x^2+y^2+y)^2 = x^2 + y^2'\n'''\nX = np.arange(-2.0, 2.0, 0.05)\nY = np.arange(-2.0, 2.0, 0.05)\nx, y = np.meshgrid(X, Y)\nf = (x ** 2 + y ** 2 + y) ** 2 - x ** 2 - y ** 2\nfig = plt.figure()\ncs = plt.contour(x, y, f, 0, colors = 'r')\nplt.show()\n\n\n'''\n'8*x^2 - 9*|x|*y + 8*y^2 = 17'\n'''\nX = np.arange(-2.5, 2.5, 0.05)\nY = np.arange(-2.5, 2.5, 0.05)\nx, y = np.meshgrid(X, Y)\nf = 8 * x ** 2 - 9 * np.abs(x) * y + 8 * y ** 2 - 17\nfig = plt.figure()\ncs = plt.contour(x, y, f, 0, colors = 'r')\nplt.show()\n\n\n'''\n'(x^2 + y^2 - 1)^3 - x^2*y^3 = 0'\n'''\nimport math\nX = np.arange(-2.0, 2.0, 0.05)\nY = np.arange(-2.0, 2.0, 0.05)\nx, y = np.meshgrid(X, Y)\nf = (x ** 2 + y ** 2 - 1) ** 2 * (x ** 2 + y ** 2 - 1)- x ** 2 *  y ** 2 * y\nfig = plt.figure()\ncs = plt.contour(x, y, f, 0, colors = 'r')\nplt.show()<\/pre>\n<h6>\u7528Python\u7edf\u8ba1\u7279\u5b9a\u76ee\u5f55\u4e0b\u6307\u5b9a\u6587\u4ef6\u7c7b\u578b\u7684\u603b\u884c\u6570<\/h6>\n<p>\u7a0b\u5e8f\u5f88\u7b80\u5355\uff0c\u4e3b\u8981\u662f\u5148\u7528\u201cos.walk(dir)\u201d\u83b7\u53d6\u7279\u5b9a\u76ee\u5f55\u4e0b\u6240\u6709\u7684\u6587\u4ef6\uff0c\u518d\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\uff0c\u6700\u540e\u7528\u201cenumerate\u201d\u7edf\u8ba1\u6587\u4ef6\u884c\u6570\uff0c\u4ee5\u6b64\u5faa\u73af\u7d2f\u52a0\u51fa\u603b\u884c\u6570\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\n#coding=utf-8\nimport os\n\ndir = \"E:\/test\"\t#\u8981\u7edf\u8ba1\u7684\u6307\u5b9a\u76ee\u5f55\nfile_types = ('.php', '.py')\t#\u8981\u7edf\u8ba1\u7684\u6587\u4ef6\u7c7b\u578b\n\ndef linecount(filepath):\n\tcount = -1\t#\u6700\u540e\u4e00\u822c\u6709\u7a7a\u884c\n\tfor count, line in enumerate(open(filepath, 'rU')):\n\t\tpass\n\tcount += 1\n\treturn count\n\ndef scan_dir(dir):\n\tnum = 0\n\tfor roots, dirs, files in os.walk(dir):\n\t\tfor file in files:\n\t\t\tname, type = os.path.splitext(file)\n\t\t\tif type in file_types:\n\t\t\t\tfilepath = os.path.join(roots, file)\n\t\t\t\tnum += linecount(filepath)\n\tprint num\n\nif __name__ == '__main__':\n    scan_dir(dir)<\/pre>\n<p>\u53c2\u8003\u94fe\u63a5\uff1a<br \/>\n<a href=\"http:\/\/dhq.me\/python-count-file-line\" target=\"_blank\">http:\/\/dhq.me\/python-count-file-line<\/a><\/p>\n<h5>Python\u7528\u7b5b\u6cd5\u751f\u6210\u7d20\u6570<\/h5>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n# coding=utf-8\nfrom time import time\nLIMIT=10**7\nt0=time()\n\ndef primes(n):\n\tp = [1] * (n\/2)\n\tfor i in xrange(3, int(n**0.5)+1, 2):\n\t\tif p[i\/2]:\n\t\t\tp[i*i\/2::i] = [0] * ((n-i*i-1)\/(2*i)+1)\n\treturn [2] + [2*i+1 for i in xrange(1,n\/2) if p[i]]\n\ndef allPrime(maxNum):\n\taList = range(0, maxNum)\n\tdef _do(aPrime):\n\t\tfor i in xrange(aPrime, maxNum, aPrime):\n\t\t\taList[i] = 0\n\t\treturn aPrime\n\treturn map( _do, (i for i in xrange(2, maxNum) if aList[i]) )\n\nprint len( primes(LIMIT) )\nprint (time()-t0)\n\nt1=time()\nprint len( allPrime(LIMIT) )\nprint (time()-t1)<\/pre>\n<h5>\u6253\u5f00\u6d4f\u89c8\u5668\u8bbf\u95eeURL<\/h5>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n# coding=utf-8\nimport webbrowser as web\nimport re\nimport urllib\nimport time\nimport os\ndef spider(url, urlpattern):\n\turls=getURLs(url, urlpattern)\n\tfor url in urls:\n\t\tvisitURL(url)\ndef visitURL(url):\n\tprint(url)\n\tweb.open(url, 1, False)\n\ttime.sleep(5)\ndef getURLs(url, urlpattern):\n\turls=[]\n\tresponse=urllib.urlopen(url)\n\thtml=response.read()\n\tpattern=re.compile(urlpattern)\n\turls=pattern.findall(html)\n\turls=list(set(urls))\n\treturn urls\n\nif __name__==\"__main__\":\n\turls={\n\t\t\"http:\/\/ixyzero.com\/blog\/\":r'http:\/\/ixyzero.com\/blog\/archives\/d{1,3}.html'\n\t\t}\n\tfor i in range(1, 10):\n\t\tfor url, urlpattern in urls.items():\n\t\t\tprint url, urlpattern\n\t\t\tspider(url, urlpattern)\n\t\tprint(\"Blogs has been refreshed for \", i, \" times\")\n\t\tos.system(\"taskkill \/F \/IM chrome.exe\")\t#\u8fd9\u91cc\u5047\u8bbe\u7cfb\u7edf\u7684\u6d4f\u89c8\u5668\u9ed8\u8ba4\u4e3aChrome\uff0c\u4f46\u5b9e\u9645\u60c5\u51b5\u53ef\u80fd\u4e0d\u662f<\/pre>\n<h5>\u5012\u8ba1\u65f6\u529f\u80fd\u793a\u4f8b<\/h5>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n#coding=utf-8\nimport time, sys\ncount = 0\nwhile (count &lt; 10):\n\tncount = 10 - count\n\tsys.stdout.write(\"r%2d\" % ncount)\n\tsys.stdout.flush()\n\ttime.sleep(1)\n\tcount += 1<\/pre>\n<h5>\u7528qrcode\u751f\u6210\u4e8c\u7ef4\u7801<\/h5>\n<pre class=\"lang:default decode:true \">import qrcode\nqr = qrcode.QRCode(\n\tversion=2,\n\terror_correction=qrcode.constants.ERROR_CORRECT_L,\n\tbox_size=10,\n\tborder=1\n)\nqr.add_data(\"http:\/\/ixyzero.com\/\")\nqr.make(fit=True)\nimg = qr.make_image()\nimg.save(\"crazyofme_qrcode.png\")<\/pre>\n<h5>Python\u83b7\u53d6\u5f53\u524d\u8ba1\u7b97\u673a\u7684cpu\u6570\u91cf<\/h5>\n<pre class=\"lang:default decode:true\">from multiprocessing import cpu_count\nprint(cpu_count())<\/pre>\n<h5>Python\u5220\u9664\u4e00\u4e2a\u975e\u7a7a\u6587\u4ef6\u5939<\/h5>\n<pre class=\"lang:default decode:true \">import shutil\nshutil.rmtree('c:\\test')\nprint 'ok'<\/pre>\n<h5>Python list \u5224\u7a7a<\/h5>\n<p><strong>\u5224\u65ad\u4e00\u4e2a list \u662f\u5426\u4e3a\u7a7a&#8211;\u4f20\u7edf\u7684\u65b9\u5f0f\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true \">if len(mylist):\n    # Do something with my list\nelse:\n    # The list is empty<\/pre>\n<p><strong>\u7531\u4e8e\u4e00\u4e2a\u7a7a list \u672c\u8eab\u7b49\u540c\u4e8e False\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\uff1a<\/strong><\/p>\n<pre class=\"lang:default decode:true\">if mylist:\n    # Do something with my list\nelse:\n    # The list is empty<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>\u53c2\u8003<\/strong><\/span>\uff1a<a href=\"http:\/\/blog.csdn.net\/yelbosh\/article\/details\/7558981\" target=\"_blank\">http:\/\/blog.csdn.net\/yelbosh\/article\/details\/7558981<\/a><\/p>\n<h5>Python\u4e00\u53e5\u4ee3\u7801\u5b9e\u73b0\u5168\u6392\u5217<\/h5>\n<pre class=\"lang:default decode:true \">from itertools import permutations\nprint(list(permutations([1, 2, 3])))<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7279\u6b8a\u8bed\u6cd5\uff1afilter\u3001map\u3001reduce\u3001lambda filter(function, se [&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,6],"tags":[8],"class_list":["post-824","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-other","tag-python"],"views":2212,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/824","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=824"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/824\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}