﻿{"id":5749,"date":"2021-02-11T08:29:34","date_gmt":"2021-02-11T00:29:34","guid":{"rendered":"https:\/\/www.linuxmi.com\/?p=5749"},"modified":"2021-02-11T08:46:30","modified_gmt":"2021-02-11T00:46:30","slug":"python-gaoji-shiyong","status":"publish","type":"post","link":"https:\/\/www.linuxmi.com\/python-gaoji-shiyong.html","title":{"rendered":"Python\u7684\u9ad8\u7ea7\u7279\u6027\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4eec"},"content":{"rendered":"<p>Python\u662f\u6570\u636e\u79d1\u5b66\u9886\u57df\u6700\u53d7\u6b22\u8fce\u7684\u8bed\u8a00\u4e4b\u4e00\u3002\u5b83\u662f\u4e00\u79cd\u5f88\u5bb9\u6613\u4e0a\u624b\u7684\u8bed\u8a00\uff0c\u5e76\u4e14\u6709\u5927\u91cf\u7684\u5e93\u652f\u6301\u3002\u7f16\u5199\u548c\u7406\u89e3Python\u66f4\u5bb9\u6613\u3002\u5b83\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u8bed\u8a00\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u7f16\u5199\u51fd\u6570\u4ee3\u7801\u3002\u4ece\u9884\u6d4b\u6a21\u578b\u5230UI\uff0c\u51e0\u4e4e\u6240\u6709\u7c7b\u578b\u7684\u5e94\u7528\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u4f7f\u7528Python\u5b9e\u73b0\u3002\u8fd9\u662fPython\u88ab\u7528\u4e8e\u51e0\u4e4e\u6240\u6709\u751f\u4ea7\u7ea7\u9879\u76ee\u7684\u4e3b\u8981\u539f\u56e0\u3002<\/p>\n<p>\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u4f7f\u7528\u7684\u4e00\u4e9b\u6700\u5f3a\u5927\u548c\u6700\u5e38\u89c1\u7684\u5e93\u662fNumPy\u548cPandas\u3002\u8fd9\u4e9b\u5e93\u5229\u7528\u4e86python\u7684\u73b0\u6709\u529f\u80fd\uff0c\u4f7f\u5176\u6210\u4e3a\u6570\u636e\u5206\u6790\u7684\u660e\u663e\u9009\u62e9\u3002<\/p>\n<p>\u4e0b\u9762\u662fPython\u7f16\u7a0b\u8bed\u8a00\u7684\u4e00\u4e9b\u6700\u9ad8\u7ea7\u7684\u7279\u6027!<\/p>\n<p><strong>Lambda\u51fd\u6570<\/strong><\/p>\n<p>Lambda\u51fd\u6570\u662f\u672c\u8d28\u4e0a\u533f\u540d\u7684\u51fd\u6570\u3002 \u5b83\u6ca1\u6709\u51fd\u6570\u540d\u79f0\u3002<\/p>\n<p>\u4e0ePython\u4e2d\u7684\u5e38\u89c4\u51fd\u6570\u5b9a\u4e49\u4e0d\u540c\uff0c\u8fd9\u4e9b\u51fd\u6570\u4f7f\u7528\u5173\u952e\u5b57\u201clambda\u201d\u5b9a\u4e49\u3002 \u8fd9\u4e9b\u51fd\u6570\u7684\u5168\u90e8\u76ee\u7684\u4e0d\u662f\u4e13\u95e8\u4e3a\u51fd\u6570\u521b\u5efa\u5185\u5b58\uff0c\u800c\u662f\u533f\u540d\u4f7f\u7528\u5b83\u6765\u6267\u884c\u5e26\u6709\u51e0\u4e2a\u53c2\u6570\u7684\u7b80\u5355\u64cd\u4f5c\u3002 \u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u6709\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u4f46\u53ea\u80fd\u6709\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u56e0\u4e3alambda\u51fd\u6570\u7684\u6982\u5ff5\u5728\u8bed\u6cd5\u4e0a\u4ec5\u9650\u4e8e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>lambda\u51fd\u6570\u7684\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre style=\"margin: 1.2em 0px; line-height: 1.2em; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 1em;\"><code style=\"background: #2f1e2e; margin: 0px 0.15em; padding: 0.5em; border-radius: 3px; border: 1px solid #cccccc; color: #a39e9b; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 0.85em; display: block !important; white-space: pre; -ms-overflow-x: auto; -moz-text-size-adjust: none;\">i = <span style=\"color: #815ba4;\">lambda<\/span> a, b : a * b\r\nprint(i(<span style=\"color: #f99b15;\">7<\/span>, <span style=\"color: #f99b15;\">8<\/span>)) <span style=\"color: #8d8687;\"># prints '56'<\/span>\r\n\r\ni = <span style=\"color: #815ba4;\">lambda<\/span> a : a*<span style=\"color: #f99b15;\">5<\/span> + <span style=\"color: #f99b15;\">5<\/span>\r\nprint(i(<span style=\"color: #f99b15;\">5<\/span>)) <span style=\"color: #8d8687;\"># prints '30'<\/span>\r\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5756 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/001.png\" alt=\"\" width=\"1224\" height=\"634\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/001.png 1224w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/001-1024x530.png 1024w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/><\/p>\n<p>\u770b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u591a\u4e48\u7b80\u5355\uff01\u6211\u4eec\u6267\u884c\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u800c\u65e0\u9700\u5b9a\u4e49\u6574\u4e2a\u51fd\u6570\u3002\u8fd9\u662f Python \u7684\u4f17\u591a\u7279\u5f81\u4e4b\u4e00\uff0c\u8fd9\u4e9b\u7279\u5f81\u4f7f\u5b83\u6210\u4e3a\u4e00\u79cd\u5e72\u51c0\u3001\u7b80\u5355\u7684\u7f16\u7a0b\u8bed\u8a00\u3002<\/p>\n<p><strong>\u8fed\u4ee3\u5668Itertools<\/strong><\/p>\n<p>python\u4e2d\u7684Itertools\u6a21\u5757\u662f\u4e00\u7ec4\u7528\u4e8e\u5904\u7406\u8fed\u4ee3\u5668\u7684\u5de5\u5177\u3002\u8fed\u4ee3\u5668\u662f\u5728\u5faa\u73af\u65f6\u4f7f\u7528\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u4fbf\u6709\u6548\u5730\u8bbf\u95ee\u548c\u64cd\u4f5c\u6570\u636e\u7ed3\u6784\u3002Itertools\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u7f16\u5199\u9ad8\u6548\u3001\u7f8e\u89c2\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u6ce8\u610f:<\/p>\n<p>\u4e3a\u4e86\u8bbf\u95ee\u8fd9\u4e2a\u6a21\u5757\u4e2d\u53ef\u7528\u7684\u65b9\u6cd5\uff0c\u5fc5\u987b\u5bfc\u5165Itertools\u3002<\/p>\n<p>\u4f8b\u5b50:<\/p>\n<pre style=\"margin: 1.2em 0px; line-height: 1.2em; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 1em;\"><code style=\"background: #2f1e2e; margin: 0px 0.15em; padding: 0.5em; border-radius: 3px; border: 1px solid #cccccc; color: #a39e9b; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 0.85em; display: block !important; white-space: pre; -ms-overflow-x: auto; -moz-text-size-adjust: none;\"><span style=\"color: #8d8687;\"># Linux\u8ff7 www.linuxmi.com<\/span>\r\n\r\n<span style=\"color: #815ba4;\">import<\/span> itertools\r\n<span style=\"color: #815ba4;\">import<\/span> operator\r\nx = [<span style=\"color: #f99b15;\">1<\/span>, <span style=\"color: #f99b15;\">2<\/span>, <span style=\"color: #f99b15;\">3<\/span>, <span style=\"color: #f99b15;\">4<\/span>, <span style=\"color: #f99b15;\">5<\/span>]\r\nsol = itertools.accumulate(x,operator.mul)\r\n<span style=\"color: #815ba4;\">for<\/span> i <span style=\"color: #815ba4;\">in<\/span> sol:\r\n    print(i)\r\n<\/code><\/pre>\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5755 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/002.png\" alt=\"\" width=\"1224\" height=\"706\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/002.png 1224w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/002-1024x591.png 1024w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/><\/p>\n<p>Itertools\u6709\u5927\u91cf\u4f7f\u7528\u65b9\u6cd5\u3002 \u4e00\u4e9b\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff1a<\/p>\n<p>count(start,[step])\uff1a<\/p>\n<p>\u8ba1\u6570\u8fc7\u7a0b\u4ece\u8d77\u59cb\u503c\u5f00\u59cb\uff0c\u4ee5\u6307\u5b9a\u6b65\u957f\u9012\u589e\uff08\u9ed8\u8ba4\u4e3a1\uff09\u3002<\/p>\n<p>cycle(p)\uff1a<\/p>\n<p>cycle\u5c31\u662f\u4e00while True\uff0c\u65e0\u9650\u5faa\u73af\u91cc\u9762\u7684\u6570\u5b57\u3002\u8fed\u4ee3\u5206\u6790\u5b57\u7b26\u4e32\u7684\u5143\u7d20\uff0c\u5e76\u63d0\u4f9b\u5305\u542b\u5b57\u7b26\u4e32\u4e2d\u5404\u4e2a\u5143\u7d20\u7684\u7ed3\u679c\u3002<\/p>\n<p>repeat(elem,[n])\uff1a<\/p>\n<p>\u91cd\u590d\u8fed\u4ee3elem\uff0cn\u6b21<\/p>\n<p><strong>Maps<\/strong><\/p>\n<p>Map()\u7528\u4e8e\u5bf9\u5217\u8868\u6216\u5b57\u5178\u7b49\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u5143\u7d20\u5e8f\u5217\u6267\u884c\u76f8\u540c\u7684\u51fd\u6570\u3002Maps\u53ef\u4ee5\u7528\u6765\u907f\u514d\u5faa\u73af\uff0c\u4ece\u800c\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u76f8\u540c\u7684\u66f4\u6539\uff0c\u4f7f\u5176\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/p>\n<p>Python\u4e2d\u7684map\u51fd\u6570\u5e94\u7528\u4e8e\u6bcf\u4e00\u4e2a\u53ef\u8fed\u4ee3\u7684\u9879\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u7ed3\u679c\u8fed\u4ee3\u5668\u3002\u5982\u679c\u6709\u5176\u4ed6\u7684\u53ef\u8fed\u4ee3\u53c2\u6570\u4f20\u8fdb\u6765\uff0cmap\u51fd\u6570\u5219\u4f1a\u628a\u6bcf\u4e00\u4e2a\u53c2\u6570\u90fd\u4ee5\u76f8\u5e94\u7684\u5904\u7406\u51fd\u6570\u8fdb\u884c\u8fed\u4ee3\u5904\u7406\u3002map()\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u662f\u51fd\u6570\uff0c\u4e00\u4e2a\u662f\u5e8f\u5217\uff0cmap\u5c06\u4f20\u5165\u7684\u51fd\u6570\u4f9d\u6b21\u4f5c\u7528\u5230\u5e8f\u5217\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u628a\u7ed3\u679c\u4f5c\u4e3amap\u5bf9\u8c61\u8fd4\u56de\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f map() \u65b9\u6cd5\u7684\u8bed\u6cd5:<\/p>\n<p>map(function, iterable, &#8230;)<\/p>\n<pre style=\"margin: 1.2em 0px; line-height: 1.2em; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 1em;\"><code style=\"background: #2f1e2e; margin: 0px 0.15em; padding: 0.5em; border-radius: 3px; border: 1px solid #cccccc; color: #a39e9b; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 0.85em; display: block !important; white-space: pre; -ms-overflow-x: auto; -moz-text-size-adjust: none;\"><span style=\"color: #06b6ef;\"><span style=\"color: #815ba4;\">def<\/span> <span style=\"color: #8d8687;\">square<\/span><span style=\"color: #f99b15;\">(x)<\/span> :<\/span>            <span style=\"color: #8d8687;\"># \u8ba1\u7b97\u5e73\u65b9\u6570<\/span>\r\n    <span style=\"color: #815ba4;\">return<\/span> x ** 2\r\nlinuxmi=list(map(square, [<span style=\"color: #f99b15;\">1<\/span>,<span style=\"color: #f99b15;\">2<\/span>,<span style=\"color: #f99b15;\">3<\/span>,<span style=\"color: #f99b15;\">4<\/span>,<span style=\"color: #f99b15;\">5<\/span>]))   <span style=\"color: #8d8687;\"># \u8ba1\u7b97\u5217\u8868\u5404\u4e2a\u5143\u7d20\u7684\u5e73\u65b9<\/span>\r\nprint(linuxmi)\r\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5757 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/004.png\" alt=\"\" width=\"1224\" height=\"562\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/004.png 1224w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/004-1024x470.png 1024w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/><\/p>\n<p>map() \u51fd\u6570\u4f1a\u6839\u636e\u63d0\u4f9b\u7684\u51fd\u6570\u5bf9\u6307\u5b9a\u5e8f\u5217\u505a\u6620\u5c04\u3002<\/p>\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570 function \u4ee5\u53c2\u6570\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u8c03\u7528 function \u51fd\u6570\uff0c\u8fd4\u56de\u5305\u542b\u6bcf\u6b21 function \u51fd\u6570\u8fd4\u56de\u503c\u7684\u65b0\u5217\u8868\u3002<\/p>\n<p><strong>\u7ec4\u5305\/\u89e3\u5305 (Packing \/ Unpacking)<\/strong><\/p>\n<p>\u5728\u5c06\u53d8\u91cf\u5206\u914d\u7ed9\u5217\u8868\u6216\u5143\u7ec4\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528*\u8fd0\u7b97\u7b26\u6765\u8f6c\u6362\u5c06\u5143\u7d20\u5206\u914d\u7ed9\u53d8\u91cf\u7684\u65b9\u5f0f\u3002 \u5b83\u662fPython\u63d0\u4f9b\u7684\u6700\u65b9\u4fbf\u7684\u6280\u672f\u4e4b\u4e00\u3002 \u8bf8\u5982\u6b64\u7c7b\u7684\u6280\u672f\u8bc1\u660e\u4e86Python\u5728\u6570\u636e\u79d1\u5b66\u548c\u673a\u5668\u5b66\u4e60\u4e2d\u662f\u9996\u9009\u7684\u539f\u56e0\u3002<\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<pre style=\"margin: 1.2em 0px; line-height: 1.2em; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 1em;\"><code style=\"background: #2f1e2e; margin: 0px 0.15em; padding: 0.5em; border-radius: 3px; border: 1px solid #cccccc; color: #a39e9b; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 0.85em; display: block !important; white-space: pre; -ms-overflow-x: auto; -moz-text-size-adjust: none;\"><span style=\"color: #8d8687;\"># Linux\u8ff7 www.linuxmi.com<\/span>\r\n\r\na, *b, c = [<span style=\"color: #f99b15;\">1<\/span>, <span style=\"color: #f99b15;\">2<\/span>, <span style=\"color: #f99b15;\">3<\/span>, <span style=\"color: #f99b15;\">4<\/span>, <span style=\"color: #f99b15;\">5<\/span>, <span style=\"color: #f99b15;\">6<\/span>, <span style=\"color: #f99b15;\">7<\/span>]\r\nprint(a)\r\nprint(b)\r\nprint(c)<\/code><\/pre>\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5758 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/005.png\" alt=\"\" width=\"1224\" height=\"562\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/005.png 1224w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/005-1024x470.png 1024w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/><\/p>\n<p><strong>\u751f\u6210\u5668(Generators) <\/strong><\/p>\n<p>\u751f\u6210\u5668(generator)\u662f\u7528\u4e8e\u521b\u5efa\u5143\u7d20\u5e8f\u5217\u7684\u5bf9\u8c61\u3002 \u5b83\u66f4\u50cf\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u56e0\u6b64\u4e0efor\u8bed\u53e5\u5e76\u9a7e\u9f50\u9a71\u3002 \u4f7f\u7528\u4f7f\u7528yield\u5173\u952e\u5b57\u751f\u6210\u503c\u7684\u51fd\u6570\u521b\u5efa\u751f\u6210\u5668\u3002<\/p>\n<p>\u5217\u8868\u63a8\u5bfc\u5f0f(list comprehension)\u4e0e\u751f\u6210\u5668(generator)\u4e4b\u95f4\u7684\u533a\u522b\u5728\u4e8e\uff0c\u524d\u8005\u751f\u6210\u6574\u4e2a\u5217\u8868\uff0c\u800c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e00\u6b21\u751f\u6210\u4e00\u4e2a\u5143\u7d20\u3002 \u7531\u4e8e\u8fd9\u4e2a\u539f\u56e0\uff0c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6bd4\u7b49\u6548\u7684\u5217\u8868\u7406\u89e3\u5177\u6709\u66f4\u9ad8\u7684\u5185\u5b58\u6548\u7387\u3002 \u8fd8\u4f7f\u7528\u4e86\u751f\u6210\u5668\uff0c\u56e0\u4e3a\u751f\u6210\u5668\u53ea\u9700\u8f83\u5c11\u7684\u4ee3\u7801\u884c\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre style=\"margin: 1.2em 0px; line-height: 1.2em; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 1em;\"><code style=\"background: #2f1e2e; margin: 0px 0.15em; padding: 0.5em; border-radius: 3px; border: 1px solid #cccccc; color: #a39e9b; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 0.85em; display: block !important; white-space: pre; -ms-overflow-x: auto; -moz-text-size-adjust: none;\"><span style=\"color: #8d8687;\"># Linux\u8ff7 www.linuxmi.com<\/span>\r\n\r\ngenerator = (x*<span style=\"color: #f99b15;\">3<\/span> <span style=\"color: #815ba4;\">for<\/span> x <span style=\"color: #815ba4;\">in<\/span> range(<span style=\"color: #f99b15;\">8<\/span>))\r\nprint(type(generator))<span style=\"color: #8d8687;\"># \u751f\u6210\u5668\u7684type<\/span>\r\nprint(generator)<span style=\"color: #8d8687;\"># \u76f4\u63a5\u6253\u5370\u751f\u6210\u5668<\/span>\r\n\r\n<span style=\"color: #815ba4;\">while<\/span> <span style=\"color: #815ba4;\">True<\/span>:\r\n    <span style=\"color: #815ba4;\">try<\/span>:\r\n        print(generator.__next__())\r\n    <span style=\"color: #815ba4;\">except<\/span>:\r\n        print(<span style=\"color: #48b685;\">\"Linux\u8ff7\u60a8\u7684\u751f\u6210\u5668\u751f\u6210\u6570\u5b57OK\"<\/span>)\r\n        <span style=\"color: #815ba4;\">break<\/span>\r\n<\/code><\/pre>\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5759 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/006.png\" alt=\"\" width=\"1224\" height=\"1066\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/006.png 1224w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/006-1024x892.png 1024w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/><\/p>\n<p>\u6ce8\u610f\uff1a<br \/>\n\u751f\u6210\u5668\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4e0e\u5faa\u73af\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<p><strong>\u8fc7\u6ee4\u5668\uff08filter\uff09<\/strong><\/p>\n<p>\u987e\u540d\u601d\u4e49\uff0cfilter() \u7528\u4e8e\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u8fc7\u6ee4\u5143\u7d20\u5e8f\u5217\u3002 \u901a\u5e38\uff0c\u5728\u68c0\u67e5\u6761\u4ef6\u65f6\uff0c\u7ed3\u679c\u4e3a\u5e03\u5c14\u6570\u636e\u7c7b\u578b\u3002filter\u5c06\u8fd9\u4e00\u6982\u5ff5\u5411\u524d\u63a8\u8fdb\uff0c\u5e76\u8fd4\u56de\u4e0e\u5e03\u5c14\u503cTrue\u76f8\u5339\u914d\u7684\u503c\u3002<\/p>\n<p>filter\u542c\u8d77\u6765\u53ef\u80fd\u7c7b\u4f3c\u4e8eMap\u51fd\u6570\u3002 \u4e24\u8005\u4e4b\u95f4\u7684\u533a\u522b\u5728\u4e8e\uff0c\u524d\u8005\u4ec5\u5728\u5e94\u7528\u51fd\u6570\u8fd4\u56deTrue\u65f6\u624d\u8fd4\u56de\u3002 \u53e6\u4e00\u65b9\u9762\uff0c\u540e\u8005\u64cd\u7eb5\u6570\u636e\u7ed3\u6784\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<p>filter()\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u51fd\u6570 f \u548c\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u51fd\u6570 f \u7684\u4f5c\u7528\u662f\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5224\u65ad\uff0c\u8fd4\u56de True\u6216 False\uff0cfilter()\u6839\u636e\u5224\u65ad\u7ed3\u679c\u81ea\u52a8\u8fc7\u6ee4\u6389\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u8fd4\u56de\u7531\u7b26\u5408\u6761\u4ef6\u5143\u7d20\u7ec4\u6210\u7684filter\u5bf9\u8c61\uff08\u4e00\u4e2a\u8fed\u4ee3\u5668\uff09\u3002<\/p>\n<p>\u8be5\u6280\u672f\u53ef\u4ee5\u901a\u8fc7\u8868\u8fbe\u5f0f\u8fdb\u884c\u9a8c\u8bc1\u5e76\u6784\u9020\u8fd4\u56de\u5217\u8868\u3002<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre style=\"margin: 1.2em 0px; line-height: 1.2em; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 1em;\"><code style=\"background: #2f1e2e; margin: 0px 0.15em; padding: 0.5em; border-radius: 3px; border: 1px solid #cccccc; color: #a39e9b; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 0.85em; display: block !important; white-space: pre; -ms-overflow-x: auto; -moz-text-size-adjust: none;\"><span style=\"color: #8d8687;\"># Linux\u8ff7 www.linuxmi.com<\/span>\r\n\r\n<span style=\"color: #06b6ef;\"><span style=\"color: #815ba4;\">def<\/span> <span style=\"color: #8d8687;\">is_odd<\/span><span style=\"color: #f99b15;\">(n)<\/span>:<\/span>\r\n    <span style=\"color: #815ba4;\">return<\/span> n % <span style=\"color: #f99b15;\">2<\/span> == <span style=\"color: #f99b15;\">1<\/span>\r\n\r\nnewlist = filter(is_odd, [<span style=\"color: #f99b15;\">1<\/span>, <span style=\"color: #f99b15;\">2<\/span>, <span style=\"color: #f99b15;\">3<\/span>, <span style=\"color: #f99b15;\">4<\/span>, <span style=\"color: #f99b15;\">5<\/span>, <span style=\"color: #f99b15;\">6<\/span>, <span style=\"color: #f99b15;\">7<\/span>, <span style=\"color: #f99b15;\">8<\/span>, <span style=\"color: #f99b15;\">9<\/span>, <span style=\"color: #f99b15;\">10<\/span>])\r\nprint(f<span style=\"color: #48b685;\">'odd:{newlist}'<\/span>)\r\nprint(f<span style=\"color: #48b685;\">'odd:{list(newlist)}'<\/span>)\r\n<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5760 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/007.png\" alt=\"\" width=\"1224\" height=\"634\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/007.png 1224w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/007-1024x530.png 1024w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/><\/p>\n<p><strong>\u5f02\u6b65Asyncio<\/strong><\/p>\n<p>Asyncio\u662fpython\u4e2d\u7684\u4e00\u4e2a\u5e93\uff0c\u7528\u4e8e\u4f7f\u7528async\/await\u7f16\u5199\u5e76\u53d1\u4ee3\u7801\u3002 \u5f53\u4f7f\u7528\u7f51\u7edc\uff0cWeb\u670d\u52a1\u5668\u548c\u6570\u636e\u5e93\u65f6\uff0c\u8fd9\u4e9b\u529f\u80fd\u81f3\u5173\u91cd\u8981\u3002 \u5b83\u7528\u4e8e\u63a7\u5236\u5b50\u6d41\u7a0b\u5728\u6bcf\u4e2a\u6d41\u7a0b\u4e2d\u6267\u884c\u3002 \u5f02\u6b65\u4ee3\u7801\u7684\u4e3b\u8981\u76ee\u6807\u662f\u7f16\u5199\u540c\u6b65\u5e76\u53d1\u4ee3\u7801\u3002<\/p>\n<p>\u4e3a\u4e86\u4f7f\u7528\u6b64\u6a21\u5757\uff0c\u5fc5\u987b\u50cfitertools\u4e00\u6837\u9996\u5148\u5c06\u5176\u5bfc\u5165\u3002<\/p>\n<pre style=\"margin: 1.2em 0px; line-height: 1.2em; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 1em;\"><code style=\"background: #2f1e2e; margin: 0px 0.15em; padding: 0.5em; border-radius: 3px; border: 1px solid #cccccc; color: #a39e9b; overflow: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 0.85em; display: block !important; white-space: pre; -ms-overflow-x: auto; -moz-text-size-adjust: none;\"><span style=\"color: #8d8687;\"># Linux\u8ff7 www.linuxmi.com<\/span>\r\n\r\n<span style=\"color: #815ba4;\">import<\/span> asyncio\r\nasync <span style=\"color: #06b6ef;\"><span style=\"color: #815ba4;\">def<\/span> <span style=\"color: #8d8687;\">main<\/span><span style=\"color: #f99b15;\">()<\/span>:<\/span>\r\n    print(<span style=\"color: #48b685;\">'Hello World!'<\/span>)\r\n    await asyncio.sleep(<span style=\"color: #f99b15;\">1<\/span>)\r\n    print(<span style=\"color: #48b685;\">'Linux\u8ff7 www.linuxmi.com'<\/span>)\r\nasyncio.run(main())\r\n<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5762 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/008.png\" alt=\"\" width=\"1224\" height=\"634\" srcset=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/008.png 1224w, https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/02\/008-1024x530.png 1024w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/><\/p>\n<p>Async\/Await\u4ee3\u7801\u7528\u4e8e\u5904\u7406\u5b50\u8fdb\u7a0b\u671f\u95f4\u7684\u7ade\u4e89\u6761\u4ef6\uff0c\u5e76\u4ee5\u7279\u5b9a\u7684\u65b9\u5f0f\u540c\u6b65\u4ee3\u7801\u884c\u3002\u63a7\u5236\u8fd9\u4e9b\u5b50\u6d41\u7a0b\u5de5\u4f5c\u65b9\u5f0f\u7684\u80fd\u529b\u662f\u7531asyncio\u63d0\u4f9b\u7684\u3002\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u51fa\u73b0\u7b2c\u4e00\u4e2aprint\u8bed\u53e5\uff0c\u7136\u540e\u6709\u4e00\u4e2a\u7761\u7720\u5468\u671f\uff0c\u63a5\u7740\u662f\u4e0b\u4e00\u4e2aprint\u8bed\u53e5\u3002\u56e0\u6b64\uff0c\u4e0d\u53ef\u80fd\u5728\u7b2c\u4e00\u4e2aprint\u8bed\u53e5\u4e4b\u524d\u6253\u5370\u7b2c\u4e8c\u4e2aprint\u8bed\u53e5\u3002<\/p>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>Python\u5df2\u7ecf\u6210\u4e3a\u73b0\u4ee3\u901a\u7528\u7f16\u7a0b\u8bed\u8a00\u3002\u5b83\u62e5\u6709\u4e00\u4e2a\u5145\u6ee1\u6d3b\u529b\u7684\u793e\u533a\uff0c\u5e76\u4e14\u4e0d\u65ad\u66f4\u65b0\u8bed\u8a00\u672c\u8eab\uff0c\u63d0\u9ad8\u4e86Python\u7684\u5f00\u53d1\u4f53\u9a8c\u3002\u5bf9\u8be5\u8bed\u8a00\u7684\u5e93\u652f\u6301\u4f7f\u8be5\u8bed\u8a00\u6210\u4e3a\u4e00\u79cd\u975e\u5e38\u901a\u7528\u7684\u8bed\u8a00\u3002Python\u63d0\u4f9b\u4e86\u5f88\u591a\u66f4\u9ad8\u7ea7\u7684\u6280\u672f\uff0c\u8fd9\u4e9b\u6280\u672f\u53ef\u80fd\u4f1a\u8ba9\u6bcf\u4e2a\u5f00\u53d1\u4eba\u5458\u90fd\u611f\u5230\u6b23\u6170\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u662f\u6570\u636e\u79d1\u5b66\u9886\u57df\u6700\u53d7\u6b22\u8fce\u7684\u8bed\u8a00\u4e4b\u4e00\u3002\u5b83\u662f\u4e00\u79cd\u5f88\u5bb9\u6613\u4e0a\u624b\u7684\u8bed\u8a00\uff0c\u5e76\u4e14\u6709\u5927\u91cf\u7684\u5e93\u652f\u6301\u3002\u7f16\u5199\u548c\u7406 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[44],"class_list":["post-5749","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"aioseo_notices":[],"views":621,"_links":{"self":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/5749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/comments?post=5749"}],"version-history":[{"count":7,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/5749\/revisions"}],"predecessor-version":[{"id":5764,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/5749\/revisions\/5764"}],"wp:attachment":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/media?parent=5749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/categories?post=5749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/tags?post=5749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}