{"id":2831,"date":"2017-10-30T17:43:09","date_gmt":"2017-10-30T09:43:09","guid":{"rendered":"http:\/\/cn.hostease.com\/xueyuan\/?p=2831"},"modified":"2025-01-26T15:46:29","modified_gmt":"2025-01-26T07:46:29","slug":"%e5%87%a0%e4%b8%aa%e5%b0%8f%e4%be%8b%e5%ad%90%e5%91%8a%e8%af%89%e4%bd%a0-%e4%b8%80%e8%a1%8cpython%e4%bb%a3%e7%a0%81%e8%83%bd%e5%b9%b2%e5%93%aa%e4%ba%9b%e4%ba%8b","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/xueyuan\/uncategorized\/%e5%87%a0%e4%b8%aa%e5%b0%8f%e4%be%8b%e5%ad%90%e5%91%8a%e8%af%89%e4%bd%a0-%e4%b8%80%e8%a1%8cpython%e4%bb%a3%e7%a0%81%e8%83%bd%e5%b9%b2%e5%93%aa%e4%ba%9b%e4%ba%8b\/","title":{"rendered":"\u51e0\u4e2a\u5c0f\u4f8b\u5b50\u544a\u8bc9\u4f60, \u4e00\u884cPython\u4ee3\u7801\u80fd\u5e72\u54ea\u4e9b\u4e8b .."},"content":{"rendered":"<div id=\"postmessage_6571647\" class=\"t_msgfont\"><span style=\"font-family: ' ';\">\u9996\u5148\u4f60\u8981\u4e86\u89e3\u4e00\u4e0bPython\u4e4b\u7985\uff0c\u4e00\u884c\u4ee3\u7801\u8f93\u51fa\u201cThe Zen of Python\u201d\uff1a<\/span><span style=\"font-family: ' ';\"><br \/>\n<\/span><br \/>\npython -c &#8220;import this&#8221;<br \/>\n&#8220;&#8221;&#8221;<br \/>\nThe Zen of Python, by Tim Peters<br \/>\nBeautiful is better than ugly.<br \/>\nExplicit is better than implicit.<br \/>\nSimple is better than complex.<br \/>\nComplex is better than complicated.<br \/>\nFlat is better than nested.<br \/>\nSparse is better than dense.<br \/>\nReadability counts.<br \/>\nSpecial cases aren&#8217;t special enough to break the rules.<br \/>\nAlthough practicality beats purity.<br \/>\nErrors should never pass silently.<br \/>\nUnless explicitly silenced.<br \/>\nIn the face of ambiguity, refuse the temptation to guess.<br \/>\nThere should be one&#8211; and preferably only one &#8211;obvious way to do it.<br \/>\nAlthough that way may not be obvious at first unless you&#8217;re Dutch.<br \/>\nNow is better than never.<br \/>\nAlthough never is often better than *right* now.<br \/>\nIf the implementation is hard to explain, it&#8217;s a bad idea.<br \/>\nIf the implementation is easy to explain, it may be a good idea.<br \/>\nNamespaces are one honking great idea &#8212; let&#8217;s do more of those!<br \/>\n&#8220;&#8221;&#8221;<span style=\"font-family: ' ';\">\u4ece\u201cThe Zen of Python\u201d\u4e5f\u80fd\u770b\u51fa\uff0cPython\u5021\u5bfcBeautiful\u3001Explicit\u3001Simple\u7b49\u539f\u5219\uff0c\u5f53\u7136\u6211\u4eec\u63a5\u4e0b\u6765\u8981\u4ecb\u7ecd\u7684\u4e00\u884cPython\u80fd\u5b9e\u73b0\u54ea\u4e9b\u597d\u73a9\u7684\u529f\u80fd\uff0c\u53ef\u80fd\u548cExplicit\u539f\u5219\u76f8\u8fdd\u80cc\u3002<\/span><span style=\"font-family: ' ';\">\u5982\u679c\u4f60\u6709\u5176\u4ed6\u8fd9\u65b9\u9762\u7684\u5c0f\u4f8b\u5b50\uff0c\u4e5f\u6b22\u8fce\u8bc4\u8bba\uff0c\u6211\u4f1a\u52a0\u5230\u6587\u7ae0\u4e2d\uff0c\u6587\u7ae0\u4e5f\u8bb8\u4f1a\u957f\u671f\u66f4\u65b0\u3002<\/span><br \/>\n<span style=\"font-family: ' ';\"><strong>(1)\u4e00\u884c\u4ee3\u7801\u542f\u52a8\u4e00\u4e2aWeb\u670d\u52a1<\/strong><\/span><\/p>\n<p><strong>python -m SimpleHTTPServer 8080 # python2<br \/>\npython3 -m http.server 8080 # python3<br \/>\n<\/strong><\/p>\n<p><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/8080.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-9791\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/8080-300x291.png\" alt=\"\" width=\"300\" height=\"291\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/8080-300x291.png 300w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/8080.png 550w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong style=\"font-family: ' '; font-size: inherit; background-color: var(--base-3); color: var(--contrast);\">(2)\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u53d8\u91cf\u503c\u4e92\u6362<\/strong><\/p>\n<p>a, b = 1, 2; a, b = b, a<br \/>\n<span style=\"font-family: ' ';\"><strong>(3)\u4e00\u884c\u4ee3\u7801\u89e3\u51b3FizzBuzz\u95ee\u9898:<\/strong><\/span><br \/>\n<span style=\"font-family: ' ';\">FizzBuzz\u95ee\u9898\uff1a\u6253\u5370\u6570\u5b571\u5230100, 3\u7684\u500d\u6570\u6253\u5370\u201cFizz\u201d, 5\u7684\u500d\u6570\u6253\u5370\u201cBuzz\u201d, \u65e2\u662f3\u53c8\u662f5\u7684\u500d\u6570\u7684\u6253\u5370\u201cFizzBuzz\u201d<\/span><\/p>\n<p><strong>for<\/strong> x in range(1, 101): print(&#8220;fizz&#8221;[x % 3 * 4:]+&#8221;buzz&#8221;[x % 5 * 4:] or x)<span style=\"font-family: ' ';\"><br \/>\n<\/span><br \/>\n<span style=\"font-family: ' ';\"><strong>(4)\u4e00\u884c\u4ee3\u7801\u8f93\u51fa\u7279\u5b9a\u5b57\u7b26\u201dLove\u201d\u62fc\u6210\u7684\u5fc3\u5f62<\/strong><\/span><\/p>\n<p>print(&#8216;\\n&#8217;.join([&#8221;.join([(&#8216;Love'[(x-y) % len(&#8216;Love&#8217;)] if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3 &lt;= 0 <strong>else <\/strong>&#8216; &#8216;) <strong>for<\/strong> x in range(-30, 30)]) <strong>for<\/strong> y in range(30, -30, -1)]))<\/p>\n<p><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/love.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-9793\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/love-286x300.png\" alt=\"\" width=\"286\" height=\"300\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/love-286x300.png 286w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/love.png 550w\" sizes=\"auto, (max-width: 286px) 100vw, 286px\" \/><\/a><\/p>\n<p><strong style=\"font-family: ' '; font-size: inherit; background-color: var(--base-3); color: var(--contrast);\">(5)\u4e00\u884c\u4ee3\u7801\u8f93\u51faMandelbrot\u56fe\u50cf<\/strong><\/p>\n<p><span style=\"font-family: ' ';\">Mandelbrot\u56fe\u50cf\uff1a\u56fe\u50cf\u4e2d\u7684\u6bcf\u4e2a\u4f4d\u7f6e\u90fd\u5bf9\u5e94\u4e8e\u516c\u5f0fN=x+y*i\u4e2d\u7684\u4e00\u4e2a\u590d\u6570<\/span><\/p>\n<p>print(&#8216;\\n&#8217;.join([&#8221;.join([&#8216;*&#8217;if abs((lambda a: lambda z, c, n: a(a, z, c, n))(lambda s, z, c, n: z if n == 0 <strong>else<\/strong> s(s, z*z+c, c, n-1))(0, 0.02*x+0.05j*y, 40)) &lt; 2 <strong>else <\/strong>&#8216; &#8216; <strong>for<\/strong> x in range(-80, 20)]) <strong>for<\/strong> y in range(-20, 20)]))<\/p>\n<p><span style=\"font-family: ' ';\"><strong>(6)\u4e00\u884c\u4ee3\u7801\u6253\u5370\u4e5d\u4e5d\u4e58\u6cd5\u8868<\/strong><\/span><\/p>\n<p>print(&#8216;\\n&#8217;.join([&#8216; &#8216;.join([&#8216;%s*%s=%-2s&#8217; % (y, x, x*y) <strong>for<\/strong> y in range(1, x+1)]) <strong>for<\/strong> x in range(1, 10)]))<\/p>\n<p><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/99.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-9795\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/99-300x95.png\" alt=\"\" width=\"300\" height=\"95\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/99-300x95.png 300w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/99.png 550w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><span style=\"font-family: ' ';\"><strong>(7)\u4e00\u884c\u4ee3\u7801\u8ba1\u7b97\u51fa1-100\u4e4b\u95f4\u7684\u7d20\u6570(\u4e24\u4e2a\u7248\u672c)<\/strong><\/span><\/p>\n<p><strong>print(&#8216; &#8216;.join([str(item) for item in filter(lambda x: not [x % i for i in range(2, x) if x % i == 0], range(2, 101))]))<br \/>\nprint(&#8216; &#8216;.join([str(item) for item in filter(lambda x: all(map(lambda p: x % p != 0, range(2, x))), range(2, 101))]))<\/strong><\/p>\n<p><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/1-100.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-9797\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/1-100-300x18.png\" alt=\"\" width=\"300\" height=\"18\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/1-100-300x18.png 300w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/1-100.png 550w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><span style=\"font-family: ' ';\"><strong>(8)\u4e00\u884c\u4ee3\u7801\u8f93\u51fa\u6590\u6ce2\u90a3\u5951\u6570\u5217<br \/>\n<\/strong><\/span>print([x[0] <strong>for<\/strong> x in [(a<i>[0], a.append([a[1], a[0]+a[1]])) <strong>for<\/strong> a in ([[1, 1]], ) <strong>for<\/strong> i in range(30)]])<br \/>\n<a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/print.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-9799\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/print-300x5.png\" alt=\"\" width=\"300\" height=\"5\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/print-300x5.png 300w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/print.png 550w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n<\/i><i><span style=\"font-family: ' ';\"><strong>(9)\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u5feb\u6392\u7b97\u6cd5<\/strong><\/span><\/i><\/p>\n<ul type=\"1\">\n<li>\n<ul type=\"1\">\n<li>\n<ul type=\"1\">\n<li>\n<ul type=\"1\">\n<li><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>qsort = lambda arr: len(arr) &gt; 1 and qsort(list(filter(lambda x: x &lt;= arr[0], arr[1:]))) + arr[0:1] + qsort(list(filter(lambda x: x &gt; arr[0], arr[1:]))) or arr<\/li>\n<li>\n<ul type=\"1\">\n<li>\n<ul type=\"1\">\n<li><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><i><span style=\"font-family: ' ';\"><strong>(10)\u4e00\u884c\u4ee3\u7801\u89e3\u51b3\u516b\u7687\u540e\u95ee\u9898<\/strong><\/span><\/i><\/p>\n<ul type=\"1\">\n<li>\n<ul type=\"1\">\n<li><\/li>\n<\/ul>\n<\/li>\n<li>[__import__(&#8216;sys&#8217;).stdout.write(&#8216;\\n&#8217;.join(&#8216;.&#8217; * i + &#8216;Q&#8217; + &#8216;.&#8217; * (8-i-1) <strong>for<\/strong> i in vec) + &#8220;\\n========\\n&#8221;) <strong>for<\/strong> vec in __import__(&#8216;itertools&#8217;).permutations(range(8)) if 8 == len(<strong>set<\/strong>(vec<i>+i <strong>for<\/strong> i in range(8))) == len(<strong>set<\/strong>(vec-i <strong>for<\/strong> i in range(8)))]<br \/>\n<a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/QQ.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9801\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2017\/10\/QQ.png\" alt=\"\" width=\"118\" height=\"252\" \/><\/a><br \/>\n<\/i><\/li>\n<\/ul>\n<p><i><\/i><i><span style=\"font-family: ' ';\"><strong>(11)\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u6570\u7ec4\u7684flatten\u529f\u80fd: \u5c06\u591a\u7ef4\u6570\u7ec4\u8f6c\u5316\u4e3a\u4e00\u7ef4<\/strong><\/span><\/i><\/p>\n<ul type=\"1\">\n<li>flatten = lambda x: [y <strong>for<\/strong> l in x <strong>for<\/strong> y in flatten(l)] if isinstance(x, list) <strong>else<\/strong> [x]<\/li>\n<\/ul>\n<p><i><\/i><br \/>\n<i><span style=\"font-family: ' ';\"><strong>(12)\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0list, \u6709\u70b9\u7c7b\u4f3c\u4e0e\u4e0a\u4e2a\u529f\u80fd\u7684\u53cd\u529f\u80fd<\/strong><\/span><\/i><\/p>\n<ul type=\"1\">\n<li>array = lambda x: [x[i:i+3] <strong>for<\/strong> i in range(0, len(x), 3)]<\/li>\n<\/ul>\n<p><i><\/i><br \/>\n<i><span style=\"font-family: ' ';\"><strong>(13)\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u6c42\u89e32\u76841000\u6b21\u65b9\u7684\u5404\u4f4d\u6570\u4e4b\u548c<\/strong><\/span><\/i><\/p>\n<ul type=\"1\">\n<li>print(sum(map(<strong>int<\/strong>, str(2**1000))))<\/li>\n<\/ul>\n<\/div>\n<div id=\"post_rate_div_6571647\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u4f60\u8981\u4e86\u89e3\u4e00\u4e0bPython\u4e4b\u7985\uff0c\u4e00\u884c\u4ee3\u7801\u8f93\u51fa\u201cThe Zen of Python\u201d\uff1a python -c &#038; &#8230; <a title=\"\u51e0\u4e2a\u5c0f\u4f8b\u5b50\u544a\u8bc9\u4f60, \u4e00\u884cPython\u4ee3\u7801\u80fd\u5e72\u54ea\u4e9b\u4e8b ..\" class=\"read-more\" href=\"https:\/\/cn.hostease.com\/xueyuan\/uncategorized\/%e5%87%a0%e4%b8%aa%e5%b0%8f%e4%be%8b%e5%ad%90%e5%91%8a%e8%af%89%e4%bd%a0-%e4%b8%80%e8%a1%8cpython%e4%bb%a3%e7%a0%81%e8%83%bd%e5%b9%b2%e5%93%aa%e4%ba%9b%e4%ba%8b\/\" aria-label=\"\u9605\u8bfb \u51e0\u4e2a\u5c0f\u4f8b\u5b50\u544a\u8bc9\u4f60, \u4e00\u884cPython\u4ee3\u7801\u80fd\u5e72\u54ea\u4e9b\u4e8b ..\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[380],"class_list":["post-2831","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-python"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/comments?post=2831"}],"version-history":[{"count":4,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2831\/revisions"}],"predecessor-version":[{"id":9806,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2831\/revisions\/9806"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/media?parent=2831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/categories?post=2831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/tags?post=2831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}