{"id":1564,"date":"2014-11-12T15:07:35","date_gmt":"2014-11-12T15:07:35","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1564"},"modified":"2014-11-12T15:07:35","modified_gmt":"2014-11-12T15:07:35","slug":"python%e7%9a%84%e4%b8%80%e4%ba%9b%e5%b0%8f%e7%9f%a5%e8%af%86%e7%82%b9_2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1564.html","title":{"rendered":"Python\u7684\u4e00\u4e9b\u5c0f\u77e5\u8bc6\u70b9_2"},"content":{"rendered":"<h6>0.\u5982\u4f55\u67e5\u770bPython\u6709\u54ea\u4e9b\u5185\u5efa\u51fd\u6570\uff1f<\/h6>\n<p>\u5982\u4f55\u67e5\u770bPython\u4e2d\u6709\u54ea\u4e9bbuiltin\u51fd\u6570\/\u65b9\u6cd5\uff1f<\/p>\n<p>\u8fdb\u5165IPython\uff1b<br \/>\n\u8f93\u5165\uff1afrom __builtin__ import [Tab] #{\u8fde\u7eed\u6309Tab\u8fdb\u884c\u8865\u5168}<\/p>\n<h6>1.The Python Challenge<\/h6>\n<ul>\n<li><a href=\"http:\/\/www.pythonchallenge.com\/\" target=\"_blank\">The Python Challenge<\/a><\/li>\n<li><a href=\"http:\/\/wiki.pythonchallenge.com\/index.php?title=Main_Page\" target=\"_blank\">Solutions<\/a><\/li>\n<li><a href=\"http:\/\/holger.thoelking.name\/python-challenge\/all\" target=\"_blank\">Solutions for the Python Challenge\u2014Possible solutions for all challenges<\/a><\/li>\n<li><a href=\"https:\/\/wiki.python.org\/moin\/ProblemSets\" target=\"_blank\">ProblemSets &#8211; Python Wiki<\/a><\/li>\n<li><a href=\"http:\/\/search.aol.com\/aol\/search?q=python+challenge\" target=\"_blank\">python challenge &#8211; AOL Search<\/a><\/li>\n<\/ul>\n<h6>2.Unicode\u7f16\u7801<\/h6>\n<pre class=\"lang:default decode:true \">#coding=utf-8\nimport sys\nprint sys.getdefaultencoding()      # --&gt; ascii\nu1 = '\u4e2d\u56fd'\nprint type(u1), repr(u1)            # --&gt; &lt;type 'str'&gt; '\\xe4\\xb8\\xad\\xe5\\x9b\\xbd'\nu2 = u'\u4e2d\u56fd2009'\nprint type(u2), repr(u2)            # --&gt; &lt;type 'unicode'&gt; u'\\u4e2d\\u56fd2009'\n\n# str --&gt; unicode\nprint '# str --&gt; unicode'\nu1_1 = u1.decode('utf8')\nprint type(u1_1), repr(u1_1)        # --&gt; &lt;type 'unicode'&gt; u'\\u4e2d\\u56fd'\nu1_2 = unicode(u1, 'utf8')\nprint type(u1_2), repr(u1_2)        # --&gt; &lt;type 'unicode'&gt; u'\\u4e2d\\u56fd'\n\n# unicode --&gt; str\nprint\nprint '# unicode --&gt; str'\nu2_1 = u2.encode('utf8')\nprint type(u2_1), repr(u2_1)        # --&gt; &lt;type 'str'&gt; '\\xe4\\xb8\\xad\\xe5\\x9b\\xbd2009'\nu2_2 = u2.encode('gbk')\nprint type(u2_2), repr(u2_2)        # --&gt; &lt;type 'str'&gt; '\\xd6\\xd0\\xb9\\xfa2009'\nu2_3 = u2.encode('gb2312')\nprint type(u2_3), repr(u2_3)        # --&gt; &lt;type 'str'&gt; '\\xd6\\xd0\\xb9\\xfa2009'<\/pre>\n<h6>3.Python\u548c\u65e5\u671f\u76f8\u5173\u7684\u4ee3\u7801{datetime\u6a21\u5757}<\/h6>\n<pre class=\"lang:default decode:true \">import datetime\nfor day in range(30):\n    print (datetime.datetime.today()-datetime.timedelta(days=day)).strftime('%Y-%m-%d')\nday_end = (datetime.datetime.today()-datetime.timedelta(days=2)).strftime('%Y-%m-%d')\nday_start = (datetime.datetime.today()-datetime.timedelta(days=9)).strftime('%Y-%m-%d')\n####\ndate = '2014-10-18'\nstime = datetime.datetime.strptime(date, '%Y-%m-%d')\netime = stime + datetime.timedelta(days=1)<\/pre>\n<h6>4.IP\u4e0e\u6574\u6570\u7684\u76f8\u4e92\u8f6c\u6362<\/h6>\n<pre class=\"lang:default decode:true \">def IP2Int(ip):\n    o = map(int, ip.split('.'))\n    res = (16777216 * o[0]) + (65536 * o[1]) + (256 * o[2]) + o[3]\n    return res\n\ndef Int2IP(ipnum):\n    o1 = int(ipnum \/ 16777216) % 256\n    o2 = int(ipnum \/ 65536) % 256\n    o3 = int(ipnum \/ 256) % 256\n    o4 = int(ipnum) % 256\n    return '%(o1)s.%(o2)s.%(o3)s.%(o4)s' % locals()<\/pre>\n<h6>5.Python\u7684\u5185\u90e8\u51fd\u6570any\u7684\u4f7f\u7528<\/h6>\n<pre class=\"lang:default decode:true \">if needle.endswith('ly') or needle.endswith('ed') or needle.endswith('ing') or needle.endswith('ers'):\n    print('Is valid')\nelse:\n    print('Invalid')\n#\nif needle in ('ly', 'ed', 'ing', 'ers'):\n    print('Is valid')\nelse:\n    print('Invalid')\n#\nif any([needle.endswith(e) for e in ('ly', 'ed', 'ing', 'ers')]):\n    print('Is valid')\nelse:\n    print('Invalid')<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5341\u5206\u4f18\u96c5\u7684\u89e3\u51b3\u4e86\uff1a\u68c0\u6d4b&#8221;\u4e00\u6bb5string\u662f\u5426\u4ee5\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u7ed3\u5c3e?&#8221;\u7684\u95ee\u9898\uff0c\u7ed9\u529b\u554a\uff01{\u53c2\u8003\uff1a<a href=\"http:\/\/www.weiguda.com\/blog\/45\/\" target=\"_blank\">Python\u8bed\u8a00\u7684\u4e00\u5c0f\u70b9\u4f18\u96c5\u8868\u73b0<\/a>}<\/p>\n<h6>6.Python\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u5b50\u4e32\u7684\u65b9\u6cd5<\/h6>\n<p>Python\u7684string\u5bf9\u8c61\u6ca1\u6709contains\u65b9\u6cd5\uff0c\u4e0d\u80fd\u4f7f\u7528string.contains\u7684\u65b9\u6cd5\u5224\u65ad\u67d0\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u67d0\u4e00\u5b50\u4e32\uff0c\u4f46\u662fPython\u6709\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u5b9e\u73b0\u8be5\u529f\u80fd\u3002<\/p>\n<p><span style=\"color: #0000ff;\"><strong>\u65b9\u6cd51\uff1a\u4f7f\u7528 in \u65b9\u6cd5<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">site = 'http:\/\/ixyzero.com\/blog\/'\nif \"crazyof\" in site:\nprint('site contains crazyof')\n#\u8f93\u51fa\u7ed3\u679c\uff1asite contains crazyof<\/pre>\n<p><span style=\"color: #0000ff;\"><strong>\u65b9\u6cd52\uff1a\u4f7f\u7528 find \u51fd\u6570<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">s = \"This be a string\"\nif s.find(\"is\") == -1:\nprint \"No 'is' here!\"\nelse:\nprint \"Found 'is' in the string.\"<\/pre>\n<p><span style=\"color: #0000ff;\"><strong>\u65b9\u6cd53\uff1a\u4f7f\u7528 count \u51fd\u6570<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true \">s = \"This be a string\"\nif s.count(\"is\") &gt; 0:\nprint \"Found 'is' in the string.\"<\/pre>\n<h6>7.\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u5b50\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5b57\u7b26\u7684\u5b9e\u73b0\uff08Python\uff09<\/h6>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n# -*- coding: utf8 -*-\n# Python 2.7.2\n\"\"\"\n\u95ee\u9898\uff1a\u5047\u8bbe\u8fd9\u6709\u4e00\u4e2a\u7531\u5404\u79cd\u5b57\u6bcd\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u5047\u8bbe\u8fd9\u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u800c\u4e14\u8fd9\u4e2a\u5b57\u7b26\u4e32\u91cc\u7684\u5b57\u6bcd\u6570\u76f8\u5bf9\u5c11\u4e00\u4e9b\u3002\u4ece\u7b97\u6cd5\u4e0a\u8bb2\uff0c\u4ec0\u4e48\u65b9\u6cd5\u80fd\u6700\u5feb\u7684\u67e5\u51fa\u6240\u6709\u5c0f\u5b57\u7b26\u4e32\u91cc\u7684\u5b57\u6bcd\u5728\u5927\u5b57\u7b26\u4e32\u91cc\u90fd\u6709\uff1f\n\u6bd4\u5982\uff0c\u5982\u679c\u662f\u4e0b\u9762\u4e24\u4e2a\u5b57\u7b26\u4e32\uff1a\nString a: ABCDEFGHLMNOPQRS\nString b: DCGSRQPOM\n\u7b54\u6848\u662ftrue\uff0c\u6240\u6709\u5728string b\u91cc\u7684\u5b57\u6bcdstring a\u4e5f\u90fd\u6709\u3002\n\n\u5982\u679c\u662f\u4e0b\u9762\u4e24\u4e2a\u5b57\u7b26\u4e32\uff1a\nString a: ABCDEFGHLMNOPQRS\nString b: DCGSRQPOZ\n\u7b54\u6848\u662ffalse\uff0c\u56e0\u4e3a\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32\u91cc\u7684Z\u5b57\u6bcd\u4e0d\u5728\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u91cc\u3002\n\"\"\"\n\ndef a_has_b(a, b):\n    \"\"\"\u5224\u65ad\u5b57\u7b26\u4e32a\u4e2d\u662f\u5426\u5305\u542bb\u4e2d\u7684\u6240\u6709\u5b57\u7b26\n    \"\"\"\n    dictionary = 0\n\n    #\u6bcf\u4e2a\u5b57\u6bcd\u7684ASCII\u7801\u503c\uff0c\u53ef\u4ee5\u5bf9\u5e94\u4e00\u4e2a\u4f4d\u56fe\u4e2d\u7684\u4f4d\u3002\n    #\u5148\u904d\u5386\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u751f\u6210\u4e00\u4e2a\u201c\u4f4d\u56fe\u5b57\u5178\u201d\u3002\n    for a_ch in a:\n        dictionary |= (0x01 &lt;&lt; (ord(a_ch) - ord('A')))\n\n    #\u6211\u4eec\u904d\u5386\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32\uff0c\u7528\u67e5\u5b57\u5178\u7684\u65b9\u5f0f\u8f83\u68c0\uff0c\u4f2a\u4ee3\u7801\u4e3a\n    for b_ch in b:\n        if dictionary != (dictionary |(0x01 &lt;&lt; (ord(b_ch) - ord('A')))):\n            return False\n    return True\n\n#\u4e3b\u51fd\u6570\uff0c\u6d4b\u8bd5\u90e8\u5206\nstr1=\"abcdefghijklmnopqrstuvwxyz\"\nstr2=\"akjsdfasdfiasdflasdfjklffhasdfasdfjklasdfjkasdf\"\nstr3=\"asdffaxcfsf\"\nstr4=\"asdfai\"\nprint(a_has_b(str1, str2))\nprint(a_has_b(str1, str3))\nprint(a_has_b(str3, str4))<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/blog.sina.com.cn\/s\/blog_3fe961ae0100zbuj.html\" target=\"_blank\">\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u5b50\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5b57\u7b26\u7684\u5b9e\u73b0\uff08Python\uff09<\/a><\/li>\n<li><a href=\"http:\/\/hi.baidu.com\/tinylee\/item\/ab498e8cfed975ccee083dfb\" target=\"_blank\">Python Cookbook 1.8 \u5224\u65ad\u5b57\u7b26\u4e32\u5185\u662f\u5426\u5305\u542b\u6307\u5b9a\u96c6\u5408\u7684\u5b57\u7b26<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>0.\u5982\u4f55\u67e5\u770bPython\u6709\u54ea\u4e9b\u5185\u5efa\u51fd\u6570\uff1f \u5982\u4f55\u67e5\u770bPython\u4e2d\u6709\u54ea\u4e9bbuiltin\u51fd\u6570\/\u65b9\u6cd5\uff1f \u8fdb\u5165IPyt [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[8,19],"class_list":["post-1564","post","type-post","status-publish","format-standard","hentry","category-other","category-programing","tag-python","tag-tips"],"views":1807,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1564","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=1564"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1564\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}