{"id":2607,"date":"2015-12-27T10:17:18","date_gmt":"2015-12-27T02:17:18","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2607"},"modified":"2015-12-27T10:17:18","modified_gmt":"2015-12-27T02:17:18","slug":"%e5%81%9a%e4%b8%80%e4%ba%9b%e7%ae%80%e5%8d%95%e7%9a%84python%e6%b5%8b%e8%af%95%e9%a2%98","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2607.html","title":{"rendered":"\u505a\u4e00\u4e9b\u7b80\u5355\u7684Python\u6d4b\u8bd5\u9898"},"content":{"rendered":"<p>=Start=<\/p>\n<p>\u6628\u5929\u665a\u4e0a\u5728\u901b <a href=\"http:\/\/python.jobbole.com\/\" target=\"_blank\">\u4f2f\u4e50\u5728\u7ebf<\/a> \u7684\u65f6\u5019\u770b\u5230\u4e86\u4e00\u7bc7\u6587\u7ae0\u300c<a href=\"http:\/\/python.jobbole.com\/83641\/\" target=\"_blank\">\u4e00\u4e9b\u7b80\u5355\u7684Python\u6d4b\u8bd5\u9898<\/a>\u300d\uff0c\u95f2\u7740\u6ca1\u4e8b\u5c31\u505a\u4e86\u524d\u9762\u51e0\u9898(\u540e\u9762\u7684\u9898\u76ee\u505a\u4e0d\u51fa\u6765\u6211\u662f\u4e0d\u4f1a\u968f\u4fbf\u8bf4\u51fa\u53e3\u7684\u2026\u2026)\uff0c\u590d\u4e60\u4e86\u4e00\u4e9b\u77e5\u8bc6\uff0c\u89c9\u5f97\u8fd9\u79cd\u4efb\u52a1\u9a71\u52a8\u5f0f\u7684\u5b66\u4e60\u8981\u6bd4\u6ca1\u4e8b\u95f2\u901b\u7684\u76ee\u7684\u6027\u66f4\u5f3a\u3001\u4e13\u9879\u6548\u679c\u4e5f\u4f1a\u66f4\u597d(\u4f46\u524d\u63d0\u662f\u5df2\u7ecf\u6709\u8fc7\u7cfb\u7edf\u6027\u5b66\u4e60Python\u7684\u7ecf\u5386\uff0c\u5426\u5219\u5728\u78b0\u5230\u95ee\u9898\u65f6\u90fd\u4e0d\u77e5\u9053\u4ece\u54ea\u65b9\u9762\u5165\u624b)\uff0c\u4ee5\u540e\u53ef\u4ee5\u5728\u6ca1\u4e8b\u7684\u65f6\u5019\u90fd\u5b66\u591a\u7ec3\uff0c\u6c34\u5e73\u624d\u6709\u53ef\u80fd\u4e0d\u65ad\u63d0\u9ad8\u3002<\/p>\n<h5>\u9898\u76ee\u4e00\uff1a\u6253\u53701-1\u4ebf\u4e4b\u5185\u7684\u5076\u6570<\/h5>\n<p>\u521a\u62ff\u5230\u9898\u76ee\u65f6\u53ea\u662f\u60f3\u5230\u4e86\u7528xrange\u66ff\u6362range\u907f\u514d\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\uff0c\u6ca1\u60f3\u8fc7\u5176\u5b83\u7684\u95ee\u9898\uff0c\u4e0d\u77e5\u9053\u8fd9\u9898\u9664\u4e86\u8fd9\u4e2a\u8003\u70b9\u4e4b\u5916\u8fd8\u60f3\u8003\u4ec0\u4e48\u77e5\u8bc6\uff1f<\/p>\n<pre class=\"lang:default decode:true \">def get_even_numbers():\n\teven_numbers = []\n\tfor x in xrange(1, 100000000):\n\t\tif x % 2:\n\t\t\teven_numbers.append(x)\n\tprint ' '.join(str(item) for item in even_numbers)<\/pre>\n<p>\u6328\u4e2aprint\u611f\u89c9\u4e0d\u592a\u597d\uff0c\u6240\u4ee5\u5148\u5c06\u6570\u5b57\u653e\u5728\u4e86list\u4e2d\uff0c\u6700\u540e\u7528str.join()\u62fc\u63a5\u540e\u4e00\u6b21\u6027print\u51fa\u6765\uff08\u611f\u89c9\u5bf9\u4e8e\u8fd9\u4e48\u5927\u6570\u91cf\u7ea7\u7684\u60c5\u51b5\uff0c\u5199\u5165\u6587\u4ef6\u624d\u662f\u6700\u4f73\u7684\u65b9\u5f0f\uff09\u3002<\/p>\n<h5>\u9898\u76ee\u4e8c\uff1a\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6e05\u9664\u5b57\u7b26\u4e32\u4e2d\u7684[]\u548c\u5176\u4e2d\u7684\u5185\u5bb9<\/h5>\n<p>\u521a\u5f00\u59cb\u5dee\u70b9\u60f3\u9519\u65b9\u5411\u4e86\uff0c\u540e\u6765\u641c\u7d22\u4e86\u4e4b\u540e\u4e00\u4e0b\u5c31\u89e3\u51b3\u4e86\uff0c\u7528 re.sub \u5c06\u6307\u5b9a\u5b50\u4e32\u66ff\u6362\u4e3a\u7a7a\uff0c\u5982\u679c\u6b21\u6570\u8f83\u591a\uff0c\u53ef\u4ee5\u5148 re.compile \u518d\u7528\u3002<\/p>\n<pre class=\"lang:default decode:true\">import re\ndef remove_specify_symbols():\n\ts = \"[lol]\u4f60\u597d\uff0c\u5e2e\u6211\u628a\u8fd9\u4e9bmarkup\u6e05\u6389\uff0c[smile]\u3002\u8c22\u8c22\uff01\"\n\tsb = re.sub(r'\\[.+?\\]', '', s)\n\tprint type(s), s\n\tprint type(sb), sb<\/pre>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p>python how to remove specific str from string use regex<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/8703017\/remove-sub-string-by-using-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/8703017\/remove-sub-string-by-using-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/3939361\/remove-specific-characters-from-a-string-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/3939361\/remove-specific-characters-from-a-string-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/875968\/how-to-remove-symbols-from-a-string-with-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/875968\/how-to-remove-symbols-from-a-string-with-python<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/re.html\" target=\"_blank\">https:\/\/docs.python.org\/2\/library\/re.html<\/a><\/li>\n<\/ul>\n<h5>\u9898\u76ee\u4e09\uff1a\u5728\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u6253\u5370\u8017\u65f6\u8be6\u60c5<\/h5>\n<p>\u56e0\u4e3a\u4e4b\u524d\u5728coolshell\u7684\u535a\u5ba2\u4e2d\u89c1\u8fc7\u7c7b\u4f3c\u7684\u6587\u7ae0\uff08\u8bb2Python\u7684decorator\uff09\uff0c\u6240\u4ee5\u89c9\u5f97\u4e5f\u8fd8\u597d\u505a\uff0c\u4e0d\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u5fd8\u4e86\uff0c\u5c31\u53bb\u7f51\u4e0a\u641c\u4e86\u4e00\u4e0b\uff1a<\/p>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/5478351\/python-time-measure-function\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/5478351\/python-time-measure-function<\/a><\/li>\n<li><a href=\"http:\/\/www.huyng.com\/posts\/python-performance-analysis\/\" target=\"_blank\">http:\/\/www.huyng.com\/posts\/python-performance-analysis\/<\/a><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">import time\n\ndef timing(f):\n    def wrap(*args):\n        print '&lt;function name: {0}&gt;'.format(f.func_name)\n        print '&lt;function call begin&gt;'\n        time1 = time.time()\n        ret = f(*args)\n        time2 = time.time()\n        print '&lt;function call end&gt;'\n        print '[timecosts: {0} ms]'.format((time2-time1)*1000.0)\n        return ret\n    return wrap\n\n@timing\ndef hello(name):\n    print \"hello, %s\" % name\n\ndef main():\n    hello('tom')<\/pre>\n<h5>\u9898\u76ee\u56db\uff1a\u5c06\u9a7c\u5cf0\u547d\u540d\u6cd5\u5b57\u7b26\u4e32\u8f6c\u6210\u4e0b\u5212\u7ebf\u547d\u540d\u6cd5\u5b57\u7b26\u4e32<\/h5>\n<p>\u521a\u770b\u5230\u9898\u76ee\u7684\u65f6\u5019\u5728\u60f3\u8be5\u5982\u4f55\u5c06\u9a7c\u5cf0\u547d\u540d\u6cd5\u7684CamelCase\u62c6\u5206\u6210Camel\u548cCase\u7136\u540e\u7528\u4e0b\u5212\u7ebf\u62fc\u63a5\uff0c\u53bb\u7f51\u4e0a\u641c\u4e86\u4e00\u4e0b\u4e4b\u540e\u8305\u585e\u987f\u5f00\u2014\u2014<strong><span style=\"color: #ff0000;\">\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u53cd\u5411\u5f15\u7528<\/span><\/strong>\u3002\u8fd8\u6709\u5c31\u662f\u627e\u5bf9\u82f1\u6587\u5173\u952e\u5b57\u5bf9\u4e8e\u89e3\u9898(\u641c\u7d22)\u6765\u8bf4\u4e5f\u5f88\u5173\u952e\u3002<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>\u9a7c\u5cf0\u547d\u540d\u6cd5\u5b57\u7b26\u4e32<\/li>\n<li>Python convert CamelCase to UnderScoreCase<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">import re\n\ndef conv_CamelCase_2_UnderScoreCase(name):\n    s1 = re.sub('(.)([A-Z][a-z]+)', r'\\1_\\2', name)\n    return re.sub('([a-z0-9])([A-Z])', r'\\1_\\2', s1)\n\ndef main():\n    s1 = 'GetItem'\n    s2 = 'getItem'\n    s3 = 'doIT'\n    s4 = 'doITgetItem'\n    s5 = 'doITGetItem'\n    s6 = 'getHTTPResponseCode'\n    print s1, conv_CamelCase_2_UnderScoreCase(s1)\n    print s2, conv_CamelCase_2_UnderScoreCase(s2)\n    print s3, conv_CamelCase_2_UnderScoreCase(s3)\n    print s4, conv_CamelCase_2_UnderScoreCase(s4)\n    print s5, conv_CamelCase_2_UnderScoreCase(s5)\n    print s6, conv_CamelCase_2_UnderScoreCase(s6)<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1175208\/elegant-python-function-to-convert-camelcase-to-camel-case\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1175208\/elegant-python-function-to-convert-camelcase-to-camel-case<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/4303492\/how-can-i-simplify-this-conversion-from-underscore-to-camelcase-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/4303492\/how-can-i-simplify-this-conversion-from-underscore-to-camelcase-in-python<\/a><\/li>\n<li><a href=\"https:\/\/gist.github.com\/3660565\" target=\"_blank\">https:\/\/gist.github.com\/3660565<\/a><\/li>\n<li><a href=\"http:\/\/wiki.geany.org\/howtos\/convert_camelcase\" target=\"_blank\">http:\/\/wiki.geany.org\/howtos\/convert_camelcase<\/a><\/li>\n<\/ul>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u6628\u5929\u665a\u4e0a\u5728\u901b \u4f2f\u4e50\u5728\u7ebf \u7684\u65f6\u5019\u770b\u5230\u4e86\u4e00\u7bc7\u6587\u7ae0\u300c\u4e00\u4e9b\u7b80\u5355\u7684Python\u6d4b\u8bd5\u9898\u300d\uff0c\u95f2\u7740\u6ca1\u4e8b\u5c31\u505a\u4e86\u524d [&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":[581,8,19],"class_list":["post-2607","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-practise","tag-python","tag-tips"],"views":3345,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2607","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=2607"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2607\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}