{"id":122,"date":"2018-11-25T23:20:54","date_gmt":"2018-11-25T15:20:54","guid":{"rendered":"http:\/\/h-k.pw\/?p=122"},"modified":"2019-01-29T13:53:09","modified_gmt":"2019-01-29T05:53:09","slug":"py-characteristic","status":"publish","type":"post","link":"https:\/\/harson.co\/index.php\/2018\/11\/25\/py-characteristic\/","title":{"rendered":"Py Characteristic"},"content":{"rendered":"<h2>\u533f\u540d\u51fd\u6570<\/h2>\n<pre class=\"pure-highlightjs\"><code class=\"\">lambda x: x * x\r\n\u76f8\u5f53\u4e8e\r\ndef f(x):\r\n    return x*x<\/code><\/pre>\n<h2>map<\/h2>\n<pre>map(func, list)<\/pre>\n<p>map\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5df2\u7ecf\u5b9a\u4e49\u597d\u7684\u51fd\u6570\uff08\u6216\u662f\u533f\u540d\u51fd\u6570\uff09\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ea\u9700\u4f20\u5165\u4e00\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u7136\u540emap \u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u9010\u4e00\u4f20\u7ed9\u524d\u9762\u7684\u90a3\u4e2a\u51fd\u6570\uff0c\u5e76\u5c06\u8fd4\u56de\u503c\u586b\u5165\u4e00\u4e2a\u5143\u7ec4\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528list()\u51fd\u6570\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">\u7ecf\u5178\u793a\u4f8b:\r\nlist(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))<\/code><\/pre>\n<h2>filter<\/h2>\n<p>\u548cmap()\u7c7b\u4f3c\uff0cfilter()\u4e5f\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u5e8f\u5217\u3002\u548cmap()\u4e0d\u540c\u7684\u662f\uff0cfilter()\u628a\u4f20\u5165\u7684\u51fd\u6570\u4f9d\u6b21\u4f5c\u7528\u4e8e\u6bcf\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u6839\u636e\u8fd4\u56de\u503c\u662fTrue\u8fd8\u662fFalse\u51b3\u5b9a\u4fdd\u7559\u8fd8\u662f\u4e22\u5f03\u8be5\u5143\u7d20\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">def is_odd(n):\r\n    return n % 2 == 1\r\n\r\nlist(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15]))\r\n# \u7ed3\u679c: [1, 5, 9, 15]\r\n#filter\u8981\u6c42\u7b2c\u4e00\u53c2\u6570\u7684\u51fd\u6570\u80fd\u8fd4\u56deTrue\u6216False<\/code><\/pre>\n<h2>reduce<\/h2>\n<pre>from functools import reduce\r\nreduce(func, list)<\/pre>\n<p>reduce \u8981\u6c42\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u51fd\u6570\u9700\u6c42\u4e24\u4e2a\u53c2\u6570\uff0creduce\u53ef\u4ee5\u628a\u7ed3\u679c\u7ee7\u7eed\u548c\u5e8f\u5217\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u505a\u7d2f\u79ef\u8ba1\u7b97\uff0c\u5b83\u76f8\u5f53\u4e8e<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)\r\n\u7ecf\u5178\u6848\u4f8b\r\nfrom functools import reduce\r\ndef fn(x, y):\r\n    return x * 10 + y\r\n\r\nreduce(fn, [1, 3, 5, 7, 9])\r\n#\u8fde\u63a5\u5217\u8868\u4e2d\u7684\u6570\u5b57\uff0c\u7ed3\u679c\u4e3a13579<\/code><\/pre>\n<h2>List Comprehensions<\/h2>\n<p>\u5217\u8868\u751f\u6210\u5f0f\u5c31\u662f\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u76f4\u63a5\u5199for \u5faa\u73af\u7b97\u6cd5\uff0c\u524d\u9762\u5199\u64cd\u4f5c\uff0c\u540e\u9762\u5199\u7b97\u6cd5\u8bed\u53e5<\/p>\n<pre>[x * x for x in range(1, 11) if x % 2 == 0]#\u8fd4\u56de1\u81f310\u5076\u6570\u7684\u5e73\u65b9<\/pre>\n<h2>Generator<\/h2>\n<p>\u5982\u679c\u8981\u751f\u62100-10\u7684\u5e73\u65b9\u6570\uff0c\u53ef\u4ee5\u5c06\u7528\u5217\u8868\u751f\u6210\u5f0f\u5c06\u6240\u6709\u7684\u7ed3\u679c\u7528\u4e00\u4e2a\u5217\u8868\u4fdd\u5b58\u8d77\u6765\uff0c\u4f46\u5f53\u9700\u6c42\u7684\u6570\u4e0d\u65ad\u589e\u591a\uff0c\u5217\u8868\u7684\u7a7a\u95f4\u5360\u7528\u4f1a\u65e0\u6bd4\u5730\u5e9e\u5927\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">#\u5e38\u89c4\u65b9\u6cd5\r\nLi = [x * x for x in range(1000)]\r\nprint(Li[0])\r\nprint(Li[999])<\/code><\/pre>\n<p>\u800c\u751f\u6210\u5668(generator)\u53ef\u4ee5\u5355\u72ec\u751f\u6210\u6bcf\u4e00\u4e2a\u9879\u7684\u503c\uff0c\u5373\u4f7f\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u6570\u5b57\uff0c\u5185\u5b58\u5360\u7528\u4e5f\u4f1a\u548c\u7b2c\u4e00\u9879\u6216\u7b2c\u4e8c\u9879\u4e00\u6837\u591a\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\">#\u751f\u6210\u5668\u65b9\u6cd5\r\ng = (x * x for x in range(10))\r\nfor n in g:\r\n    print(n)<\/code><\/pre>\n<h3>yield<\/h3>\n<p>\u53ea\u8981\u4e00\u4e2a\u51fd\u6570\u5305\u542byield\uff0c\u90a3\u4e48\u8fd9\u4e2a\u51fd\u6570\u5c31\u53ef\u88ab\u79f0\u4e4b\u4e3a\u751f\u6210\u5668<\/p>\n<p>\u8fd9\u4e2a\u51fd\u6570\u5728\u6267\u884c\u5230yield\u65f6\uff0c\u5c31\u4f1a\u88ab\u51bb\u7ed3\uff0c\u7136\u540e\u5c06yield\u5bf9\u5e94\u7684\u8bed\u53e5\u7684\u503c\u8fd4\u56de\uff0c\u4e4b\u540e\u51fd\u6570\u518d\u4ece\u4e0a\u6b21\u51bb\u7ed3\u7684\u4f4d\u7f6e\u91cd\u65b0\u5f00\u59cb\u3002<\/p>\n<p>\u56e0\u6b64\uff0cyield\u53ef\u4ee5\u8ba4\u4e3a\u662f\u4e00\u4e2a\u7c7b\u4f3c\u4e8ereturn\u7684\u8bed\u53e5\uff0c\u4f46\u548creturn\u4e0d\u540c\u7684\u662f\uff0cyield\u4f1a\u5728\u4e0b\u6b21\u8fed\u4ee3\u65f6\uff0c\u4ece\u521a\u521a\u90a3\u4e2a\u4f4d\u7f6e\u5f00\u59cb\uff0c\u5185\u5b58\u5360\u7528\u59cb\u7ec8\u4e3a\u5e38\u6570\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>global\u5173\u952e\u5b57<\/strong><\/p>\n<p>\u52a0\u4e0aglobal\u7684\u53d8\u91cf\uff0c\u4f1a\u6210\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u4f46\u8fd9\u79cd\u5168\u5c40\u53d8\u91cf\u4ec5\u9650\u4e8e\u8bfb\u53d6\u3002\u56e0\u4e3a\u51fd\u6570\u5728\u5c1d\u8bd5\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u65f6\uff0cpython\u4f1a\u4e3a\u4e86\u4fdd\u62a4\u5168\u5c40\u53d8\u91cf\uff0c\u5728\u5f53\u524d\u51fd\u6570\u4f5c\u7528\u57df\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u540c\u540d\u53d8\u91cf<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u95ed\u5305<\/strong><\/p>\n<p>Python\u4e2d\u4e07\u7269\u7686\u5bf9\u8c61\uff0c\u6240\u4ee5\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a<em>\u5185\u5d4c\u51fd\u6570 \u3002<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u533f\u540d\u51fd\u6570 lambda x: x * x \u76f8\u5f53\u4e8e def f(x): return x*x map map(f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-122","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":11,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":243,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/122\/revisions\/243"}],"wp:attachment":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}