{"id":2361,"date":"2015-07-12T14:54:40","date_gmt":"2015-07-12T06:54:40","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2361"},"modified":"2015-07-12T14:54:40","modified_gmt":"2015-07-12T06:54:40","slug":"collectpython%e4%bb%a3%e7%a0%81%e7%9a%84%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2361.html","title":{"rendered":"[collect]Python\u4ee3\u7801\u7684\u6027\u80fd\u4f18\u5316\u6280\u5de7"},"content":{"rendered":"<p>\u4e4b\u524d\u5728\u770b\u300a<a href=\"http:\/\/ixyzero.com\/blog\/archives\/2209.html\" target=\"_blank\">Python\u9ad8\u624b\u4e4b\u8def<\/a>\u300b\u90a3\u672c\u4e66\u7684\u7b2c10\u7ae0(\u6027\u80fd\u4e0e\u4f18\u5316)\u65f6\uff0c\u505a\u4e86\u4e00\u4e9b\u8bfb\u4e66\u7b14\u8bb0(\u5176\u5b9e\u5c31\u662f\u6458\u6284)\uff0c\u540e\u6765\u53c8\u770b\u5230\u4e86\u4e00\u7bc7\u6587\u7ae0\uff1a<a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-cn-python-optim\/\" target=\"_blank\">Python \u4ee3\u7801\u6027\u80fd\u4f18\u5316\u6280\u5de7<\/a>\uff0c\u4e24\u8005\u6709\u5f02\u66f2\u540c\u5de5\u4e4b\u5999\uff0c\u518d\u52a0\u4e0a\u81ea\u5df1\u4e4b\u524d\u5199Python\u811a\u672c\u65f6\u7684\u4e00\u4e9b\u7ecf\u9a8c\u5c0f\u7ed3\uff0c\u5c06\u5185\u5bb9\u6574\u7406\u5982\u4e0b\uff1a<\/p>\n<p><strong>10.1\u00a0<\/strong><strong>\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<p>\u5982\u679c\u4f7f\u7528\u6b63\u786e\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5927\u591a\u6570\u8ba1\u7b97\u673a\u95ee\u9898\u90fd\u80fd\u4ee5\u4e00\u79cd\u4f18\u96c5\u800c\u7b80\u5355\u7684\u65b9\u5f0f\u89e3\u51b3\uff0c\u800cPython\u5c31\u6070\u6070\u63d0\u4f9b\u4e86\u5f88\u591a\u53ef\u4f9b\u9009\u62e9\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>\u901a\u5e38\uff0c\u6709\u4e00\u79cd\u8bf1\u60d1\u662f\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u8fd9\u5fc5\u7136\u662f\u5f92\u52b3\u65e0\u529f\u3001\u6ce8\u5b9a\u5931\u8d25\u7684\u60f3\u6cd5\u3002\u56e0\u4e3aPython\u603b\u662f\u80fd\u591f\u63d0\u4f9b\u66f4\u597d\u7684\u6570\u636e\u7ed3\u6784\u548c\u4ee3\u7801\uff0c\u8981\u5b66\u4f1a\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u719f\u6089\u57fa\u672c\u7684<\/strong><strong>Python<\/strong><strong>\u6570\u636e\u7ed3\u6784\u548c\u5b83\u6240\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u51ed\u6b64\u89e3\u51b3\u5927\u591a\u6570\u95ee\u9898<\/strong><\/span>\u3002<\/p>\n<p>dict\/set\/collections<\/p>\n<p><strong>10.2\u00a0<\/strong><strong>\u6027\u80fd\u5206\u6790<\/strong><\/p>\n<ul>\n<li>cProfile \u6027\u80fd\u5206\u6790<\/li>\n<li>dis \u6a21\u5757\u662fPython\u5b57\u8282\u7801\u7684\u53cd\u7f16\u8bd1\u5668\uff0c\u7528\u8d77\u6765\u5f88\u7b80\u5355\uff1adis.dis()\u51fd\u6570\u4f1a\u53cd\u7f16\u8bd1\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u7684\u51fd\u6570\uff0c\u5e76\u6253\u5370\u51fa\u8fd9\u4e2a\u51fd\u6570\u8fd0\u884c\u7684\u5b57\u8282\u7801\u6307\u4ee4\u7684\u6e05\u5355\u3002<\/li>\n<\/ul>\n<p><strong>10.3\u00a0<\/strong><strong>\u6709\u5e8f\u5217\u8868\u548c\u4e8c\u5206\u67e5\u627e<\/strong><\/p>\n<p>\u4f7f\u7528\u5185\u7f6e\u7684bisect\u6a21\u5757\u5bf9\u6709\u5e8f\u5217\u8868\u8fdb\u884c\u4e8c\u5206\u67e5\u627e<\/p>\n<p><strong>10.4 namedtuple<\/strong><strong>\u548c<\/strong><strong>slots<\/strong><\/p>\n<p>\u5bf9\u6709\u65f6\u521b\u5efa\u53ea\u62e5\u6709\u56fa\u5b9a\u5c5e\u6027\u7684\u7b80\u5355\u5bf9\u8c61\u662f\u975e\u5e38\u6709\u7528\u7684\u3002<\/p>\n<p><strong>10.5 memoization<\/strong><\/p>\n<ul>\n<li>Memoization\u662f\u6307<strong>\u901a\u8fc7\u7f13\u5b58<span style=\"color: #ff0000;\">\u7eaf\u51fd\u6570<\/span>\u8fd4\u56de\u7ed3\u679c\u6765\u52a0\u901f\u51fd\u6570\u8c03\u7528<\/strong>\u7684\u4e00\u79cd\u6280\u672f\u3002\u4f46\u5f53\u4e14\u4ec5\u5f53\u51fd\u6570\u662f\u7eaf\u51fd\u6570\u65f6\u7ed3\u679c\u624d\u53ef\u4ee5\u88ab\u7f13\u5b58\u3002<\/li>\n<li>\u4ecePython3.3\u5f00\u59cb\uff0cfunctools\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2aLRU\u7f13\u5b58\u88c5\u9970\u5668\uff0c\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u7f13\u5b58\u547d\u4e2d\u3001\u7f3a\u5931\u7b49\u7684\u7edf\u8ba1\u3002<\/li>\n<\/ul>\n<p><strong>10.6 PyPy<\/strong><\/p>\n<p>PyPy\u662f\u7b26\u5408\u6807\u51c6\u7684\u4e00\u4e2aPython\u7684\u9ad8\u6548\u5b9e\u73b0\u3002<\/p>\n<p><strong>10.7\u00a0<\/strong><strong>\u901a\u8fc7\u7f13\u51b2\u533a\u534f\u8bae\u5b9e\u73b0\u96f6\u590d\u5236<\/strong><\/p>\n<ul>\n<li>\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u5927\u91cf\u7684\u590d\u5236\u3001\u5206\u7247\u548c\u4fee\u6539\u7b49\u64cd\u4f5c\u662f\u975e\u5e38\u4f4e\u6548\u7684\u3002<\/li>\n<li>\u63a8\u8350\u4f7f\u7528memory_profiler\u6a21\u5757\u9010\u884c\u67e5\u770b\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5\u3002<\/li>\n<li>memoryview\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p><strong>10.8 Victor Stinner<\/strong><strong>\u8bbf\u8c08<\/strong><\/p>\n<ul>\n<li>\n<h6>\u4f18\u5316Python\u4ee3\u7801\u7684\u4e00\u4e2a\u521d\u6b65\u7b56\u7565\u662f\u4ec0\u4e48\uff1f<\/h6>\n<\/li>\n<\/ul>\n<p>\u9488\u5bf9Python\u7684\u7b56\u7565\u5176\u5b9e\u548c\u5728\u5176\u4ed6\u7684\u8bed\u8a00\u4e2d\u4e00\u6837\u3002\u9996\u5148\u9700\u8981\u5b9a\u4e49\u826f\u597d\u7684\u7528\u4f8b\uff0c\u4ee5\u5f97\u5230\u4e00\u4e2a\u7a33\u5b9a\u53ef\u91cd\u73b0\u7684\u57fa\u51c6\u3002<strong>\u5728\u6ca1\u6709\u53ef\u9760\u57fa\u51c6\u7684\u60c5\u51b5\u4e0b\u5c1d\u8bd5\u4e0d\u540c\u7684\u4f18\u5316\u65b9\u6cd5\u5f88\u53ef\u80fd\u5bfc\u81f4\u65f6\u95f4\u7684\u6d6a\u8d39\u548c\u4e0d\u6210\u719f\u7684\u4f18\u5316\u3002<\/strong>\u65e0\u7528\u7684\u4f18\u5316\u53ef\u80fd\u4f7f\u4ee3\u7801\u66f4\u7cdf\uff0c\u66f4\u4e0d\u6613\u61c2\uff0c\u751a\u81f3\u66f4\u6162\u3002\u6709\u7528\u7684\u4f18\u5316\u5fc5\u987b\u81f3\u5c11\u8ba9\u7a0b\u5e8f\u52a0\u901f5%\u3002<\/p>\n<ul>\n<li>\n<h6>\u5173\u4e8ePython\u4ee3\u7801\u7684\u6027\u80fd\u5206\u6790\u548c\u4f18\u5316\u6709\u8bb8\u591a\u4e0d\u540c\u7684\u5de5\u5177\uff0c\u4f60\u6700\u559c\u6b22\u7684\u662f\u54ea\u4e2a\uff1f<\/h6>\n<\/li>\n<\/ul>\n<ol>\n<li>Python 3.3\u00a0\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b0\u7684 time.perf_counter() \u51fd\u6570\uff0c\u7528\u6765\u4e3a\u57fa\u51c6\u6d4b\u8bd5\u8861\u91cf\u5df2\u8017\u7528\u65f6\u95f4\u3002<\/li>\n<li>\u5bf9\u4e8e\u5fae\u57fa\u51c6\u6d4b\u8bd5\uff0c\u4f7f\u7528 timeit \u6a21\u5757\u662f\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/li>\n<li>\u4f18\u5316\u662f\u975e\u5e38\u82b1\u65f6\u95f4\u7684\uff0c\u6240\u4ee5\u6700\u597d\u80fd\u4e13\u6ce8\u4e8e\u90a3\u4e9b\u8017\u8d39\u6700\u591aCPU\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 cProfile \u6a21\u5757\u6765\u8fdb\u884c\u5206\u6790\u3002<\/li>\n<li>\u6d4b\u8bd5\u8fd0\u884c\u5e94\u8be5\u4e0d\u6b62\u4e00\u6b21\uff0c\u6700\u5c113\u6b21\uff0c\u4e00\u822c5\u6b21\u5c31\u591f\u4e86\u3002<\/li>\n<\/ol>\n<ul>\n<li>\n<h6>\u80fd\u591f\u6539\u8fdb\u6027\u80fd\u7684\u6700\u6709\u610f\u601d\u7684Python\u6280\u5de7\u662f\u4ec0\u4e48\uff1f<\/h6>\n<\/li>\n<\/ul>\n<ol>\n<li>\u5c3d\u53ef\u80fd\u91cd\u7528\u6807\u51c6\u5e93<\/li>\n<li><span style=\"color: #ff0000;\"><strong>\u5e94\u8be5\u6709\u4e00\u79cd\u2014\u2014\u6700\u597d\u53ea\u6709\u4e00\u79cd\u2014\u2014\u663e\u800c\u6613\u89c1\u7684\u65b9\u5f0f\u53bb\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd<\/strong><\/span><\/li>\n<\/ol>\n<ul>\n<li>\n<h6>\u5728\u54ea\u4e9b\u9886\u57df\u4e2dPython\u7684\u6027\u80fd\u5f88\u5dee\uff1f\u54ea\u4e9b\u9886\u57df\u4e2d\u5e94\u8be5\u5c0f\u5fc3\u4f7f\u7528\uff1f<\/h6>\n<\/li>\n<\/ul>\n<ol>\n<li>\u9996\u5148\u9700\u8981\u786e\u5b9a\u74f6\u9888\u5728\u54ea\uff0c\u4e0d\u662f\u6240\u6709\u7684\u95ee\u9898\u90fd\u7531Python\u5f15\u8d77<\/li>\n<li>multiprocessing<\/li>\n<li>\u7f16\u5199\u5f02\u6b65\u4ee3\u7801<\/li>\n<\/ol>\n<ul>\n<li>\n<h6>\u4f60\u89c1\u8fc7\u6700\u591a\u7684\u5bfc\u81f4\u6027\u80fd\u5dee\u7684\u201c\u9519\u8bef\u201d\u662f\u4ec0\u4e48\uff1f<\/h6>\n<\/li>\n<\/ul>\n<ol>\n<li>\u5728\u4e0d\u9700\u8981\u590d\u5236\u7684\u65f6\u5019\u9519\u8bef\u7684\u4f7f\u7528\u4e86copy.deepcopy()<\/li>\n<li>\u9519\u8bef\u7684\u4f7f\u7528\u4e0d\u9002\u5408\u7684\u6570\u636e\u7ed3\u6784\uff08<strong><span style=\"color: #ff0000;\">\u5e94\u8be5\u4e86\u89e3\u6bcf\u4e2a\u64cd\u4f5c(add\/get\/delete)\u5728\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u4e0a\u7684\u590d\u6742\u6027\u548c\u5f71\u54cd<\/span><\/strong>\uff09<\/li>\n<\/ol>\n<p>==<\/p>\n<p>\u4ee3\u7801\u4f18\u5316\u80fd\u591f\u8ba9\u7a0b\u5e8f\u8fd0\u884c\u66f4\u5feb\uff0c\u5b83\u662f\u5728\u4e0d\u6539\u53d8\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u7684\u60c5\u51b5\u4e0b\u4f7f\u5f97\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u66f4\u9ad8\uff0c\u6839\u636e 80\/20 \u539f\u5219\uff0c\u5b9e\u73b0\u7a0b\u5e8f\u7684\u91cd\u6784\u3001\u4f18\u5316\u3001\u6269\u5c55\u4ee5\u53ca\u6587\u6863\u76f8\u5173\u7684\u4e8b\u60c5\u901a\u5e38\u9700\u8981\u6d88\u8017 80% \u7684\u5de5\u4f5c\u91cf\u3002\u4f18\u5316\u901a\u5e38\u5305\u542b\u4e24\u65b9\u9762\u7684\u5185\u5bb9\uff1a\u51cf\u5c0f\u4ee3\u7801\u7684\u4f53\u79ef\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002<\/p>\n<p><strong>\u6539\u8fdb\u7b97\u6cd5\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<p>\u4e00\u4e2a\u826f\u597d\u7684\u7b97\u6cd5\u80fd\u591f\u5bf9\u6027\u80fd\u8d77\u5230\u5173\u952e\u4f5c\u7528\uff0c\u56e0\u6b64\u6027\u80fd\u6539\u8fdb\u7684\u9996\u8981\u70b9\u662f\u5bf9\u7b97\u6cd5\u7684\u6539\u8fdb\u3002\u5728\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6392\u5e8f\u4e0a\u4f9d\u6b21\u662f\uff1a<\/p>\n<p>O(1) -&gt; O(lg n) -&gt; O(n lg n) -&gt; O(n^2) -&gt; O(n^3) -&gt; O(n^k) -&gt; O(k^n) -&gt; O(n!)<\/p>\n<p>\u56e0\u6b64\u5982\u679c\u80fd\u591f\u5728\u65f6\u95f4\u590d\u6742\u5ea6\u4e0a\u5bf9\u7b97\u6cd5\u8fdb\u884c\u4e00\u5b9a\u7684\u6539\u8fdb\uff0c\u5bf9\u6027\u80fd\u7684\u63d0\u9ad8\u4e0d\u8a00\u800c\u55bb\u3002\u4f46\u5bf9\u5177\u4f53\u7b97\u6cd5\u7684\u6539\u8fdb\u4e0d\u5c5e\u4e8e\u672c\u6587\u8ba8\u8bba\u7684\u8303\u56f4\uff0c\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u53c2\u8003\u8fd9\u65b9\u9762\u8d44\u6599\u3002\u4e0b\u9762\u7684\u5185\u5bb9\u5c06\u96c6\u4e2d\u8ba8\u8bba\u6570\u636e\u7ed3\u6784\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u5b57\u5178 (dictionary) <\/strong><strong>\u4e0e\u5217\u8868 (list)<\/strong><\/p>\n<p>Python \u5b57\u5178\u4e2d\u4f7f\u7528\u4e86 hash table\uff0c\u56e0\u6b64\u67e5\u627e\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u4e3a O(1)\uff0c\u800c list \u5b9e\u9645\u662f\u4e2a\u6570\u7ec4\uff0c\u5728 list \u4e2d\uff0c\u67e5\u627e\u9700\u8981\u904d\u5386\u6574\u4e2a list\uff0c\u5176\u590d\u6742\u5ea6\u4e3a O(n)\uff0c\u56e0\u6b64\u5bf9\u6210\u5458\u7684\u67e5\u627e\u8bbf\u95ee\u7b49\u64cd\u4f5c\u5b57\u5178\u8981\u6bd4 list \u66f4\u5feb\u3002\u56e0\u6b64<span style=\"color: #ff0000;\"><strong>\u5728\u9700\u8981\u591a\u6570\u636e\u6210\u5458\u8fdb\u884c\u9891\u7e41\u7684\u67e5\u627e\u6216\u8005\u8bbf\u95ee\u7684\u65f6\u5019\uff0c\u4f7f\u7528 dict \u800c\u4e0d\u662f list \u662f\u4e00\u4e2a\u8f83\u597d\u7684\u9009\u62e9<\/strong><\/span>\u3002<\/p>\n<p><strong>\u96c6\u5408 (set) <\/strong><strong>\u4e0e\u5217\u8868 (list)<\/strong><\/p>\n<p>set \u7684 union\uff0c intersection\uff0cdifference \u64cd\u4f5c\u8981\u6bd4 list \u7684\u8fed\u4ee3\u8981\u5feb\u3002\u56e0\u6b64<strong><span style=\"color: #ff0000;\">\u5982\u679c\u6d89\u53ca\u5230\u6c42 list \u4ea4\u96c6\uff0c\u5e76\u96c6\u6216\u8005\u5dee\u7684\u95ee\u9898\u53ef\u4ee5\u8f6c\u6362\u4e3a set \u6765\u64cd\u4f5c<\/span><\/strong>\u3002<\/p>\n<p><strong>\u5bf9\u5faa\u73af\u7684\u4f18\u5316<\/strong><\/p>\n<p>\u5bf9\u5faa\u73af\u7684\u4f18\u5316\u6240\u9075\u5faa\u7684\u539f\u5219\u662f\u5c3d\u91cf\u51cf\u5c11\u5faa\u73af\u8fc7\u7a0b\u4e2d\u7684\u8ba1\u7b97\u91cf\uff0c\u6709\u591a\u91cd\u5faa\u73af\u7684\u5c3d\u91cf\u5c06\u5185\u5c42\u7684\u8ba1\u7b97\u63d0\u5230\u4e0a\u4e00\u5c42\u3002<\/p>\n<p><strong>\u5145\u5206\u5229\u7528 Lazy if-evaluation <\/strong><strong>\u7684\u7279\u6027<\/strong><\/p>\n<p>python \u4e2d\u6761\u4ef6\u8868\u8fbe\u5f0f\u662f lazy evaluation \u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u5982\u679c\u5b58\u5728\u6761\u4ef6\u8868\u8fbe\u5f0f if x and y\uff0c\u5728 x \u4e3a false \u7684\u60c5\u51b5\u4e0b y \u8868\u8fbe\u5f0f\u7684\u503c\u5c06\u4e0d\u518d\u8ba1\u7b97\u3002\u56e0\u6b64\u53ef\u4ee5\u5229\u7528\u8be5\u7279\u6027\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3002<\/p>\n<p><strong>\u5b57\u7b26\u4e32\u7684\u4f18\u5316<\/strong><\/p>\n<p><strong><span style=\"color: #ff0000;\">python \u4e2d\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\u662f\u4e0d\u53ef\u6539\u53d8\u7684<\/span><\/strong>\uff0c\u56e0\u6b64\u5bf9\u4efb\u4f55\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u5982\u62fc\u63a5\uff0c\u4fee\u6539\u7b49\u90fd\u5c06\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u57fa\u4e8e\u539f\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u8fd9\u79cd\u6301\u7eed\u7684 copy \u4f1a\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u5f71\u54cd python \u7684\u6027\u80fd\u3002\u5bf9\u5b57\u7b26\u4e32\u7684\u4f18\u5316\u4e5f\u662f\u6539\u5584\u6027\u80fd\u7684\u4e00\u4e2a\u91cd\u8981\u7684\u65b9\u9762\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u6587\u672c\u8f83\u591a\u7684\u60c5\u51b5\u4e0b\u3002\u5b57\u7b26\u4e32\u7684\u4f18\u5316\u4e3b\u8981\u96c6\u4e2d\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<ol>\n<li>\u5728\u5b57\u7b26\u4e32\u8fde\u63a5\u7684\u4f7f\u7528\u5c3d\u91cf\u4f7f\u7528 join() \u800c\u4e0d\u662f + \u3002\u56e0\u6b64\u5728\u5b57\u7b26\u7684\u64cd\u4f5c\u4e0a join \u6bd4 + \u8981\u5feb\uff0c\u56e0\u6b64\u8981\u5c3d\u91cf\u4f7f\u7528 join \u800c\u4e0d\u662f + \u3002<\/li>\n<li>\u5f53\u5bf9\u5b57\u7b26\u4e32\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u8005\u5185\u7f6e\u51fd\u6570\u6765\u5904\u7406\u7684\u65f6\u5019\uff0c\u9009\u62e9\u5185\u7f6e\u51fd\u6570\u3002\u5982 str.isalpha()\uff0cstr.isdigit()\uff0cstr.startswith((&#8216;x&#8217;, &#8216;yz&#8217;))\uff0cstr.endswith((&#8216;x&#8217;, &#8216;yz&#8217;))<\/li>\n<li>\u5bf9\u5b57\u7b26\u8fdb\u884c\u683c\u5f0f\u5316\u6bd4\u76f4\u63a5\u4e32\u8054\u8bfb\u53d6\u8981\u5feb\uff0c\u56e0\u6b64\u8981\u4f7f\u7528\uff1a<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true\">out = \"&lt;html&gt;%s%s%s%s&lt;\/html&gt;\" % (head, prologue, query, tail)<\/pre>\n<p style=\"padding-left: 30px;\">\u800c\u907f\u514d\uff1a<\/p>\n<pre class=\"lang:default decode:true\">out = \"&lt;html&gt;\" + head + prologue + query + tail + \"&lt;\/html&gt;\"<\/pre>\n<p><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff08list comprehension<\/strong><strong>\uff09\u548c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\uff08generator expression<\/strong><strong>\uff09<\/strong><\/p>\n<p>\u5217\u8868\u89e3\u6790\u8981\u6bd4\u5728\u5faa\u73af\u4e2d\u91cd\u65b0\u6784\u5efa\u4e00\u4e2a\u65b0\u7684 list \u66f4\u4e3a\u9ad8\u6548\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u6765\u63d0\u9ad8\u8fd0\u884c\u7684\u6548\u7387\u3002<\/p>\n<p><strong>\u5176\u4ed6\u4f18\u5316\u6280\u5de7<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u9700\u8981\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u4f7f\u7528 a,b=b,a \u800c\u4e0d\u662f\u501f\u52a9\u4e2d\u95f4\u53d8\u91cf t=a;a=b;b=t\uff1b<\/li>\n<li>\u5728\u5faa\u73af\u7684\u65f6\u5019\u4f7f\u7528 xrange \u800c\u4e0d\u662f range\uff1b\u4f7f\u7528 xrange \u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u7cfb\u7edf\u5185\u5b58\uff0c\u56e0\u4e3a xrange() \u5728\u5e8f\u5217\u4e2d\u6bcf\u6b21\u8c03\u7528\u53ea\u4ea7\u751f\u4e00\u4e2a\u6574\u6570\u5143\u7d20\u3002\u800c range() \u5c06\u76f4\u63a5\u8fd4\u56de\u5b8c\u6574\u7684\u5143\u7d20\u5217\u8868\uff0c\u7528\u4e8e\u5faa\u73af\u65f6\u4f1a\u6709\u4e0d\u5fc5\u8981\u7684\u5f00\u9500\u3002\u5728 python3 \u4e2d xrange \u4e0d\u518d\u5b58\u5728\uff0c\u91cc\u9762 range \u63d0\u4f9b\u4e00\u4e2a\u53ef\u4ee5\u904d\u5386\u4efb\u610f\u957f\u5ea6\u7684\u8303\u56f4\u7684 iterator\u3002<\/li>\n<li>\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\uff0c\u907f\u514d&#8221;global&#8221;\u5173\u952e\u5b57\u3002<strong><span style=\"color: #ff0000;\">python \u8bbf\u95ee\u5c40\u90e8\u53d8\u91cf\u4f1a\u6bd4\u5168\u5c40\u53d8\u91cf\u8981\u5feb\u5f97\u591a<\/span><\/strong>\uff0c\u56e0\u6b64\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u63d0\u5347\u6027\u80fd\u3002<\/li>\n<li>if done is not None \u6bd4\u8bed\u53e5 if done != None \u66f4\u5feb\uff0c\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u9a8c\u8bc1\uff1b<\/li>\n<li>\u5728\u8017\u65f6\u8f83\u591a\u7684\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u628a\u51fd\u6570\u7684\u8c03\u7528\u6539\u4e3a\u5185\u8054\u7684\u65b9\u5f0f\uff1b<\/li>\n<li>\u4f7f\u7528<strong><span style=\"color: #ff0000;\">\u7ea7\u8054\u6bd4\u8f83<\/span><\/strong> &#8220;x &lt; y &lt; z&#8221; \u800c\u4e0d\u662f &#8220;x &lt; y and y &lt; z&#8221;\uff1b<\/li>\n<li>while 1 \u8981\u6bd4 while True \u66f4\u5feb\uff08\u5f53\u7136\u540e\u8005\u7684\u53ef\u8bfb\u6027\u66f4\u597d\uff09\uff1b<\/li>\n<li>build in \u51fd\u6570\u901a\u5e38\u8f83\u5feb\uff0cadd(a,b) \u8981\u4f18\u4e8e a+b\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9a\u4f4d\u7a0b\u5e8f\u6027\u80fd\u74f6\u9888<\/strong><\/p>\n<p>\u5bf9\u4ee3\u7801\u4f18\u5316\u7684\u524d\u63d0\u662f\u9700\u8981\u4e86\u89e3\u6027\u80fd\u74f6\u9888\u5728\u4ec0\u4e48\u5730\u65b9\uff0c\u7a0b\u5e8f\u8fd0\u884c\u7684\u4e3b\u8981\u65f6\u95f4\u662f\u6d88\u8017\u5728\u54ea\u91cc\uff0c\u5bf9\u4e8e\u6bd4\u8f83\u590d\u6742\u7684\u4ee3\u7801\u53ef\u4ee5\u501f\u52a9\u4e00\u4e9b\u5de5\u5177\u6765\u5b9a\u4f4d\uff0cpython \u5185\u7f6e\u4e86\u4e30\u5bcc\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u5982 profile,cProfile \u4e0e hotshot \u7b49\u3002\u5176\u4e2d Profiler \u662f python \u81ea\u5e26\u7684\u4e00\u7ec4\u7a0b\u5e8f\uff0c\u80fd\u591f\u63cf\u8ff0\u7a0b\u5e8f\u8fd0\u884c\u65f6\u5019\u7684\u6027\u80fd\uff0c\u5e76\u63d0\u4f9b\u5404\u79cd\u7edf\u8ba1\u5e2e\u52a9\u7528\u6237\u5b9a\u4f4d\u7a0b\u5e8f\u7684\u6027\u80fd\u74f6\u9888\u3002Python \u6807\u51c6\u6a21\u5757\u63d0\u4f9b\u4e09\u79cd profilers:cProfile, profile \u4ee5\u53ca hotshot\u3002<\/p>\n<p>\u5bf9\u4e8e\u5927\u578b\u5e94\u7528\u7a0b\u5e8f\uff0c\u5982\u679c\u80fd\u591f\u5c06\u6027\u80fd\u5206\u6790\u7684\u7ed3\u679c\u4ee5\u56fe\u5f62\u7684\u65b9\u5f0f\u5448\u73b0\uff0c\u5c06\u4f1a\u975e\u5e38\u5b9e\u7528\u548c\u76f4\u89c2\uff0c\u5e38\u89c1\u7684\u53ef\u89c6\u5316\u5de5\u5177\u6709 Gprof2Dot\uff0cvisualpytune\uff0cKCacheGrind \u7b49\uff0c\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u67e5\u9605\u76f8\u5173\u5b98\u7f51\uff0c\u672c\u6587\u4e0d\u505a\u8be6\u7ec6\u8ba8\u8bba\u3002<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/haypo-notes.readthedocs.org\/en\/latest\/python.html\" target=\"_blank\">http:\/\/haypo-notes.readthedocs.org\/en\/latest\/python.html<\/a><\/li>\n<li><a href=\"https:\/\/jakevdp.github.io\/blog\/2012\/08\/08\/memoryview-benchmarks\/\" target=\"_blank\">https:\/\/jakevdp.github.io\/blog\/2012\/08\/08\/memoryview-benchmarks\/<\/a><\/li>\n<li><a href=\"http:\/\/helpful.knobs-dials.com\/index.php\/Python_usage_notes_-_struct,_buffer,_array,_bytes,_memoryview\" target=\"_blank\">http:\/\/helpful.knobs-dials.com\/index.php\/Python_usage_notes_-_struct,_buffer,_array,_bytes,_memoryview<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/18655648\/what-exactly-is-the-point-of-memoryview-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/18655648\/what-exactly-is-the-point-of-memoryview-in-python<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/c-api\/buffer.html\" target=\"_blank\">https:\/\/docs.python.org\/2\/c-api\/buffer.html<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/stdtypes.html#memoryview-type\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/stdtypes.html#memoryview-type<\/a><\/li>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-cn-python-optim\/\" target=\"_blank\">Python \u4ee3\u7801\u6027\u80fd\u4f18\u5316\u6280\u5de7<\/a><\/li>\n<\/ul>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u5728\u770b\u300aPython\u9ad8\u624b\u4e4b\u8def\u300b\u90a3\u672c\u4e66\u7684\u7b2c10\u7ae0(\u6027\u80fd\u4e0e\u4f18\u5316)\u65f6\uff0c\u505a\u4e86\u4e00\u4e9b\u8bfb\u4e66\u7b14\u8bb0(\u5176\u5b9e\u5c31\u662f\u6458\u6284)\uff0c\u540e\u6765\u53c8\u770b\u5230 [&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,12],"tags":[42,8],"class_list":["post-2361","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-efficiency","tag-python"],"views":5499,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2361","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=2361"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2361\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}