{"id":264,"date":"2023-10-27T09:51:01","date_gmt":"2023-10-27T01:51:01","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=264"},"modified":"2023-10-31T20:37:25","modified_gmt":"2023-10-31T12:37:25","slug":"%e3%80%8apython%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b%e3%80%8b11-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d%ef%bc%88%e7%ac%ac%e4%ba%8c%e9%83%a8%e5%88%86%ef%bc%89","status":"publish","type":"post","link":"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/27\/%e3%80%8apython%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b%e3%80%8b11-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d%ef%bc%88%e7%ac%ac%e4%ba%8c%e9%83%a8%e5%88%86%ef%bc%89\/","title":{"rendered":"\u300aPython\u5165\u95e8\u6559\u7a0b\u300b11. \u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd\uff08\u7b2c\u4e8c\u90e8\u5206\uff09"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5f00\u80c3\u83dc<\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/2-%e4%bd%bf%e7%94%a8-python-%e8%a7%a3%e9%87%8a%e5%99%a8\/\">\u4f7f\u7528 Python \u89e3\u91ca\u5668<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/3-%e9%9d%9e%e6%ad%a3%e5%bc%8f%e4%bb%8b%e7%bb%8dpython\/\">\u975e\u6b63\u5f0f\u4ecb\u7ecdPython<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/4-%e6%b7%b1%e5%85%a5%e6%b5%81%e7%a8%8b%e6%8e%a7%e5%88%b6\/\">\u6df1\u5165\u6d41\u7a0b\u63a7\u5236<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/5-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84\/\">\u6570\u636e\u7ed3\u6784<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/6-%e6%a8%a1%e5%9d%97\/\">\u6a21\u5757<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/7-%e8%be%93%e5%85%a5%e5%92%8c%e8%be%93%e5%87%ba\/\">\u8f93\u5165\u548c\u8f93\u51fa<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/8-%e9%94%99%e8%af%af%e5%92%8c%e5%bc%82%e5%b8%b8\/\">\u9519\u8bef\u548c\u5f02\u5e38<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/9-%e7%b1%bb\/\">\u7c7b<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/10-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/11-%e6%a0%87%e5%87%86%e5%ba%93%e7%9a%84%e7%ae%80%e6%98%8e%e4%bb%8b%e7%bb%8d%ef%bc%88%e7%ac%ac%e4%ba%8c%e9%83%a8%e5%88%86%ef%bc%89\/\">\u6807\u51c6\u5e93\u7684\u7b80\u660e\u4ecb\u7ecd\uff08\u7b2c\u4e8c\u90e8\u5206\uff09<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/12-%e7%8e%b0%e5%9c%a8%e5%b9%b2%e4%bb%80%e4%b9%88\/\">\u73b0\u5728\u5e72\u4ec0\u4e48?<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/13-%e4%ba%a4%e4%ba%92%e5%bc%8f%e8%be%93%e5%85%a5%e7%bc%96%e8%be%91%e5%8f%8a%e5%8e%86%e5%8f%b2%e6%9b%bf%e4%bb%a3\/\">\u4ea4\u4e92\u5f0f\u8f93\u5165\u7f16\u8f91\u53ca\u5386\u53f2\u66ff\u4ee3<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/ai.gitpp.com\/index.php\/2023\/10\/26\/14-%e6%b5%ae%e7%82%b9%e7%ae%97%e6%9c%af-%e9%97%ae%e9%a2%98%e5%92%8c%e9%99%90%e5%88%b6\/\">\u6d6e\u70b9\u7b97\u672f: \u95ee\u9898\u548c\u9650\u5236<\/a><\/li>\n<\/ol>\n\n\n\n<p>\u7b2c\u4e8c\u90e8\u5206\u6db5\u76d6\u4e86\u8bb8\u591a\u5173\u4e8e\u4e13\u4e1a\u7f16\u7a0b\u9700\u6c42\u7684\u9ad8\u7ea7\u6a21\u5757. \u8fd9\u4e9b\u6a21\u5757\u5f88\u5c11\u51fa\u73b0\u5728\u5c0f\u7684\u7a0b\u5e8f\u5f53\u4e2d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">11.1. \u683c\u5f0f\u5316\u8f93\u51fa<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#tut-output-formatting\"><\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/reprlib.html#module-reprlib\"><code>reprlib<\/code><\/a>\u00a0\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9762\u5411\u5185\u5bb9\u5f88\u591a\u6216\u8005\u6df1\u5ea6\u5f88\u5e7f\u7684\u5d4c\u5957\u5bb9\u5668\u7684\u81ea\u5b9a\u4e49\u7248\u672c\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#repr\"><code>repr()<\/code><\/a>, \u5b83\u53ea\u663e\u793a\u7f29\u7565\u56fe<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0importreprlib<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>reprlib.repr(set(\u2018supercalifragilisticexpialidocious\u2019)) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u201cset([\u2018a\u2019, \u2018c\u2019, \u2018d\u2019, \u2018e\u2019, \u2018f\u2019, \u2018g\u2019, \u2026])\u201d<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/pprint.html#module-pprint\"><code>pprint<\/code><\/a>\u00a0\u6a21\u5757\u901a\u8fc7\u4e00\u79cd\u80fd\u591f\u8ba9\u89e3\u91ca\u5668\u8bfb\u61c2\u7684\u65b9\u6cd5, \u6765\u5bf9\u5185\u7f6e\u7684\u548c\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u4e00\u4e9b\u5bf9\u8c61\u7684\u8f93\u51fa\u8fdb\u884c \u66f4\u590d\u6742\u7684\u64cd\u4f5c. \u5f53\u8fd4\u56de\u7684\u7ed3\u679c\u5927\u4e8e\u4e00\u884c\u65f6, \u201cpretty printer\u201d \u529f\u80fd\u6a21\u5757\u4f1a\u52a0\u4e0a\u65ad\u884c\u7b26\u548c\u9002\u5f53\u7684\u7f29\u8fdb, \u4ee5 \u4f7f\u6570\u636e\u7684\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u660e\u6717:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0import pprint<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>t = [[[[\u2018black\u2019, \u2018cyan\u2019], \u2018white\u2019, [\u2018green\u2019, \u2018red\u2019]], [[\u2018magenta\u2019,\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0\u2018yellow\u2019], \u2018blue\u2019]]]\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>pprint.pprint(t, width=30) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">[[[[\u2018black\u2019, \u2018cyan\u2019],<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u2018white\u2019, [\u2018green\u2019, \u2018red\u2019]],<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> [[\u2018magenta\u2019, \u2018yellow\u2019], <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u2018blue\u2019]]]<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/textwrap.html#module-textwrap\"><code>textwrap<\/code><\/a>\u00a0\u6a21\u5757\u4f1a\u6839\u636e\u5c4f\u5e55\u7684\u5bbd\u5ea6\u800c\u9002\u5f53\u5730\u53bb\u8c03\u6574\u6587\u672c\u6bb5\u843d:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"927\" height=\"181\" src=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-18.png\" alt=\"\" class=\"wp-image-413\" srcset=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-18.png 927w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-18-300x59.png 300w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-18-768x150.png 768w\" sizes=\"auto, (max-width: 927px) 100vw, 927px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/locale.html#module-locale\"><code>locale<\/code><\/a>\u00a0\u6a21\u5757\u8bbf\u95ee\u4e00\u4e2a\u5305\u542b\u56e0\u7279\u5b9a\u8bed\u8a00\u73af\u5883\u800c\u5f02\u7684\u6570\u636e\u683c\u5f0f\u7684\u6570\u636e\u5e93. locale\u6a21\u5757\u7684\u683c\u5f0f\u5316\u51fd\u6570\u7684\u5206\u7ec4\u5c5e\u6027, \u53ef\u4ee5\u7528\u7ec4\u522b\u5206\u79bb\u5668, \u76f4\u63a5\u5730\u53bb\u683c\u5f0f\u5316\u6570\u5b57:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"706\" height=\"186\" src=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-19.png\" alt=\"\" class=\"wp-image-414\" srcset=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-19.png 706w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-19-300x79.png 300w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">11.2. \u6a21\u677f\u5316<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#tut-templating\"><\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/string.html#module-string\"><code>string<\/code><\/a>&nbsp;\u6a21\u5757\u5305\u62ec\u4e00\u4e2a\u591a\u5143\u5316\u7684&nbsp;<code>Template<\/code>&nbsp;\u7c7b, \u4e3a\u7528\u6237\u63d0\u4f9b\u7b80\u5316\u4e86\u7684\u8bed\u6cd5\u683c\u5f0f, \u4f7f\u5176\u53ef\u4ee5\u65b9\u4fbf\u7684\u7f16\u8f91. \u8fd9\u6837\u53ef\u4ee5\u4f7f\u7528\u6237\u81ea\u5b9a\u4e49\u81ea\u5df1\u7684\u7a0b\u5e8f\u800c\u4e0d\u7528\u53bb\u4fee\u6539\u7a0b\u5e8f\u672c\u8eab.<\/p>\n\n\n\n<p>\u8fd9\u79cd\u683c\u5f0f\u4f7f\u7528\u7531\u00a0<code>$<\/code>\u00a0\u548c\u5408\u6cd5\u7684Python\u6807\u8bc6\uff08\u5305\u62ec\u6587\u5b57\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff09\u7ec4\u6210\u7684\u5360\u4f4d\u7b26\u540d\u79f0. \u5c06\u8fd9\u4e9b\u5360\u4f4d\u7b26 \u5305\u542b\u5728\u4e00\u5bf9\u82b1\u62ec\u53f7\u91cc\u65f6\u5141\u8bb8\u5468\u56f4\u5b58\u5728\u66f4\u591a\u7684\u5b57\u6bcd\u6216\u8005\u6570\u5b57\u800c\u4e0d\u7528\u7406\u4f1a\u662f\u5426\u6709\u7a7a\u683c. \u5fc5\u8981\u65f6\u4f7f\u7528\u00a0<code>$$<\/code>\u00a0\u53bb\u8868\u793a \u5355\u72ec\u7684\u00a0<code>$<\/code><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0fromstringimport<\/strong>\u00a0Template\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>t = Template(\u2018$<em>{village}<\/em>folk send $$10 to $cause.\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>t.substitute(village=\u2019Nottingham\u2019, cause=\u2019the ditch fund\u2019)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u2018Nottinghamfolk send $10 to the ditch fund.\u2019<\/p>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u5b57\u5178\u6216\u8005\u5173\u952e\u5b57\u53c2\u6570\u6ca1\u6709\u7ed9\u5360\u4f4d\u7b26\u63d0\u4f9b\u76f8\u5e94\u7684\u503c\u65f6,\u00a0<code>substitute()<\/code>\u00a0\u65b9\u6cd5\u4f1a\u629b\u51fa\u4e00\u4e2a\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#KeyError\"><code>KeyError<\/code><\/a>\u00a0\u5f02\u5e38. \u5bf9\u4e8e\u50cfmail-merge\u98ce\u683c\u7684\u5e94\u7528\u7a0b\u5e8f, \u7528\u6237\u53ef\u80fd\u4f1a\u63d0\u4f9b\u4e0d\u5b8c\u6574\u7684\u6570\u636e, \u6b64\u65f6,\u00a0<code>safe_substitute()<\/code>\u00a0\u65b9\u6cd5\u53ef\u80fd\u4f1a\u66f4\u9002\u5408\u2014\u2014\u5f53\u6570\u636e\u7f3a\u5931\u7684\u65f6\u5019, \u5b83\u4e0d\u4f1a\u6539\u53d8\u5360\u4f4d\u7b26:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>t = Template(\u2018Return the $item to $owner.\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>d = dict(item=\u2019unladen swallow\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>t.substitute(d)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> Traceback (most recent call last): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">. . . <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">KeyError: \u2018owner\u2019\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>t.safe_substitute(d)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> \u2018Return the unladen swallow to $owner.\u2019<\/p>\n\n\n\n<p>Template\u7c7b\u7684\u5b50\u7c7b\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u81ea\u5df1\u7684\u5206\u9694\u7b26. \u4f8b\u5982, \u73b0\u5728\u6709\u4e00\u5927\u6279\u6587\u4ef6\u7684\u91cd\u547d\u540d\u5de5\u4f5c, \u9488\u5bf9\u7684\u662f\u4e00\u4e2a\u7167 \u7247\u6d4f\u89c8\u5668, \u5b83\u53ef\u80fd\u4f1a\u9009\u62e9\u4f7f\u7528\u767e\u5206\u7b26\u53f7\u5c06\u5f53\u524d\u65f6\u95f4\u3001\u56fe\u7247\u7684\u5e8f\u5217\u53f7\u6216\u8005\u6587\u4ef6\u683c\u5f0f\u5206\u9694\u51fa\u6765\u4f5c\u4e3a\u5360\u4f4d\u7b26:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0importtime<\/strong>,\u00a0<strong>os.path<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>photofiles = [\u2018img_1074.jpg\u2019, \u2018img_1076.jpg\u2019, \u2018img_1077.jpg\u2019]\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0classBatchRename<\/strong>(Template):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026<\/strong>\u00a0delimiter = \u2018%\u2019\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>fmt = input(\u2018Enter rename style (<em>%d<\/em>-date %n-seqnum\u00a0<em>%f<\/em>-format): \u2018)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> Enter rename style (%d-date %n-seqnum %f-format): Ashley_%n%f\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>t = BatchRename(fmt)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>date = time.strftime(\u2018<em>%d<\/em>%b%y\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0for<\/strong>\u00a0i, filename\u00a0<strong>in<\/strong>\u00a0enumerate(photofiles):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>\u2026<\/strong>\u00a0base, ext = os.path.splitext(filename)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0newname = t.substitute(d=date, n=i, f=ext)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>\u2026<\/strong>\u00a0print(\u2018<em>{0}<\/em>\u00a0\u2013>\u00a0<em>{1}<\/em>\u2018.format(filename, newname)) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">img_1074.jpg \u2013> Ashley_0.jpg<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> img_1076.jpg \u2013> Ashley_1.jpg <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">img_1077.jpg \u2013> Ashley_2.jpg<\/p>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u7528\u4e8e\u6a21\u677f\u5316\u7684\u5e94\u7528\u7a0b\u5e8f\u662f\u5c06\u9879\u76ee\u7684\u903b\u8f91\u6309\u591a\u79cd\u8f93\u51fa\u683c\u5f0f\u7684\u7ec6\u8282\u5206\u79bb\u5f00\u6765. \u8fd9\u4f7f\u5f97\u4ece\u4f20\u7edf\u7684\u6a21\u677f\u5f62\u5f0f\u8f6c\u5316 \u4e3aXML\u6587\u4ef6\u3001\u7eaf\u6587\u672c\u5f62\u5f0f\u548chtml\u7f51\u9875\u6210\u4e3a\u4e86\u53ef\u80fd.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">11.3. Working with Binary Data Record Layouts<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#working-with-binary-data-record-layouts\"><\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/struct.html#module-struct\"><code>struct<\/code><\/a>\u00a0\u6a21\u5757\u4e00\u4e9b\u51fd\u6570, \u5982\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#id10\">:fun:`pack`<\/a>\u00a0\u548c\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#id12\">:fun:`unpack`<\/a>\u00a0\u51fd\u6570\u53bb\u5904\u7406\u957f\u5ea6\u53ef\u53d8\u7684\u4e8c\u8fdb\u5236 \u8bb0\u5f55\u683c\u5f0f. \u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u6f14\u793a\u4e86\u5982\u4f55\u5728\u4e0d\u4f7f\u7528\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/zipfile.html#module-zipfile\"><code>zipfile<\/code><\/a>\u00a0\u6a21\u5757\u7684\u60c5\u51b5\u4e0b\u53bb\u5faa\u73af\u5f97\u5230\u4e00\u4e2aZIP\u6587\u4ef6\u7684 \u6807\u9898\u4fe1\u606f. \u5305\u4ee3\u7801\u00a0<code>H<\/code>\u00a0\u548c\u00a0<code>I<\/code>\u00a0\u5206\u522b\u8868\u793a\u4e24\u4e2a\u548c\u56db\u4e2a\u5b57\u8282\u7684\u65e0\u7b26\u53f7\u6570\u5b57. \u800c\u00a0<code>&lt;<\/code>\u00a0\u5219\u8868\u793a\u5b83\u4eec\u662f\u6807\u51c6 \u5927\u5c0f\u5e76\u4ee5\u5b57\u8282\u5927\u5c0f\u7684\u987a\u5e8f\u6392\u5217\u5728\u540e\u9762:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>importstruct<\/strong>\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">data = open(\u2018myfile.zip\u2019, \u2018rb\u2019).read() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">start = 0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>for<\/strong>\u00a0i\u00a0<strong>in<\/strong>\u00a0range(3):\u00a0<em># \u663e\u793a\u6700\u5f00\u59cb\u76843\u4e2a\u6807\u9898<\/em>\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">start += 14 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">fields = struct.unpack(\u2018&lt;IIIHH\u2019, data[start:start+16])<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> crc32, comp_size, uncomp_size, filenamesize, extra_size = fields <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">start += 16 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">filename = data[start:start+filenamesize]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> start += filenamesize<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> extra = data[start:start+extra_size]<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> print(filename, hex(crc32), comp_size, uncomp_size) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">start += extra_size + comp_size\u00a0<em># \u8df3\u5230\u53e6\u4e00\u4e2a\u6807\u9898<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">11.4. \u591a\u7ebf\u7a0b<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#tut-multi-threading\"><\/a><\/h2>\n\n\n\n<p>\u7ebf\u7a0b\u662f\u4e00\u79cd\u4f7f\u6ca1\u6709\u987a\u5e8f\u5173\u7cfb\u7684\u4efb\u52a1\u5e76\u53d1\u6267\u884c\u7684\u6280\u672f. \u7ebf\u7a0b\u53ef\u4ee5\u7528\u6765\u6539\u8fdb\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u65b9\u5f0f\u4f7f\u8fd9\u4e9b\u7a0b\u5e8f\u53ef \u5728\u63a5\u53d7\u7528\u6237\u8f93\u5165\u7684\u540c\u65f6\u5728\u540e\u53f0\u6267\u884c\u53e6\u4e00\u4e9b\u64cd\u4f5c. \u4e00\u4e2a\u4e0e\u6b64\u76f8\u5173\u7684\u4f8b\u5b50\u662f\u8fd0\u884c\u8f93\u5165\u8f93\u51fa\u7a0b\u5e8f\u7684\u540c\u65f6\u5728\u53e6\u4e00\u4e2a \u7ebf\u7a0b\u5e8f\u4e2d\u6267\u884c\u8ba1\u7b97\u64cd\u4f5c.<\/p>\n\n\n\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u663e\u793a\u4e86\u9ad8\u7ea7\u6a21\u5757\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/threading.html#module-threading\"><code>threading<\/code><\/a>\u00a0\u5982\u4f55\u5b9e\u73b0\u4e3b\u7a0b\u5e8f\u6267\u884c\u7684\u540c\u65f6\u5728\u540e\u53f0\u6267\u884c\u53e6\u4e00\u4e2a\u76f8\u5e94\u7684\u7a0b \u5e8f:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>import threading<\/strong>,\u00a0<strong>zipfile<\/strong><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>class AsyncZip<\/strong>(threading.Thread):<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>def<\/strong>\u00a0__init__(self, infile, outfile): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">threading.Thread.__init__(self)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> self.infile = infile <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">self.outfile = outfile<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>def<\/strong>\u00a0run(self): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">f = zipfile.ZipFile(self.outfile, \u2018w\u2019, zipfile.ZIP_DEFLATED) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">f.write(self.infile) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">f.close() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">print(\u2018Finished background zip of:\u2019, self.infile)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> background = AsyncZip(\u2018mydata.txt\u2019, \u2018myarchive.zip\u2019) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">background.start() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">print(\u2018The main program continues to run in foreground.\u2019)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> background.join()\u00a0<em># \u7b49\u5f85\u540e\u53f0\u4efb\u52a1\u7ed3\u675f<\/em>\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">print(\u2018Main program waited until background was done.\u2019)<\/p>\n\n\n\n<p>\u591a\u7ebf\u5c42\u5e94\u7528\u7a0b\u5e8f\u7684\u6700\u5927\u6311\u6218\u5c31\u662f\u534f\u8c03\u884c\u7ebf\u7a0b\u4e4b\u95f4\u6570\u636e\u6216\u8005\u5176\u5b83\u8d44\u6e90\u7684\u5171\u4eab. \u4e3a\u6b64, \u7ebf\u7a0b\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u540c\u6b65 \u539f\u59cb\u51fd\u6570, \u5305\u62ec\u9501\u5b9a\u3001\u6761\u4ef6\u53d8\u91cf\u548c\u4fe1\u53f7.<\/p>\n\n\n\n<p>\u867d\u7136\u6709\u8fd9\u4e9b\u5f3a\u5927\u7684\u5de5\u5177, \u4f46\u8bbe\u8ba1\u4e0a\u7684\u4e00\u4e2a\u5c0f\u9519\u8bef\u4ecd\u7136\u53ef\u4ee5\u5bfc\u81f3\u96be\u4ee5\u6062\u590d\u7684\u95ee\u9898. \u56e0\u6b64, \u5bf9\u4e8e\u534f\u8c03\u5404\u7ebf\u7a0b\u6211\u4eec \u66f4\u503e\u5411\u4e8e\u628a\u7684\u6709\u7684\u8bbf\u95ee\u96c6\u4e2d\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u4e0a, \u8fd9\u4e2a\u7ebf\u7a0b\u4f7f\u7528&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/queue.html#module-queue\"><code>queue<\/code><\/a>&nbsp;\u6a21\u5757\u628a\u5176\u5b83\u7ebf\u7a0b\u7684\u8bf7\u6c42\u5168 \u90e8\u96c6\u4e2d\u8d77\u6765. \u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528&nbsp;<code>Queue<\/code>&nbsp;\u7684\u5bf9\u8c61\u6765\u8fdb\u884c\u8de8\u7ebf\u7a0b\u7684\u4ea4\u6d41\u548c\u534f\u8c03\u53ef\u4ee5\u4f7f\u5f97\u8bbe\u8ba1\u53d8\u5f97\u66f4\u7b80\u5355, \u800c\u4e14\u66f4\u6613\u9605\u8bfb, \u66f4\u53ef\u9760.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">11.5. \u65e5\u5fd7<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#tut-logging\"><\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/logging.html#module-logging\"><code>logging<\/code><\/a>\u00a0\u6a21\u5757\u63d0\u4f9b\u4e00\u6574\u5957\u5bcc\u6709\u7279\u8272\u4e14\u7075\u6d3b\u7684\u65e5\u5fd7\u7cfb\u7edf. \u7528\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u8bf4, \u5c31\u662f\u628a\u65e5\u5fd7\u6d88\u606f\u4f20\u9001 \u7ed9\u4e00\u4e2a\u6587\u4ef6\u6216\u8005\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#id16\">\u201c<\/a>sys.stderr`:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>importlogging<\/strong>\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">logging.debug(\u2018Debugging information\u2019) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">logging.info(\u2018Informational message\u2019)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> logging.warning(\u2018Warning:config file\u00a0<em>%s<\/em>\u00a0not found\u2019, \u2018server.conf\u2019) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">logging.error(\u2018Error occurred\u2019)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> logging.critical(\u2018Critical error \u2014 shutting down\u2019)<\/p>\n\n\n\n<p>\u4e0a\u8ff0\u4f1a\u4ea7\u751f\u4ee5\u4e0b\u7684\u8f93\u51fa:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">WARNING:root:Warning:config file server.conf\u00a0<strong>not<\/strong>\u00a0found <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">ERROR:root:Error occurred<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> CRITICAL:root:Critical error \u2014 shutting down<\/p>\n\n\n\n<p>\u9ed8\u8ba4\u7684\u60c5\u51b5\u4e0b, \u4fe1\u606f\u548c\u8c03\u8bd5\u6d88\u606f\u4f1a\u88ab\u6355\u6349, \u5e76\u53d1\u9001\u7ed9\u6807\u51c6\u9519\u8bef\u6d41. \u5176\u5b83\u7684\u4e00\u4e9b\u8f93\u51fa\u9009\u9879\u5305\u62ec\u7ecf\u7531\u90ae\u4ef6\u3001\u6570\u636e\u62a5 \u5957\u63a5\u5b57\u6216\u8005\u53d1\u9001\u7ed9\u4e00\u4e2aHTTP\u670d\u52a1\u5668\u7684\u8def\u7531\u6d88\u606f. \u65b0\u7684\u5730\u6ee4\u5668\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u57fa\u4e8e\u6d88\u606f\u4f18\u5148\u7ea7\u7684\u7684\u8def\u7531, \u800c\u6d88\u606f \u7684\u4f18\u5148\u7ea7\u6709:&nbsp;<code>DEBUG<\/code>,&nbsp;<code>INFO<\/code>,&nbsp;<code>WARING<\/code>,&nbsp;<code>ERROR<\/code>, \u548c&nbsp;<code>CRITICAL<\/code>.<\/p>\n\n\n\n<p>\u65e5\u5fd7\u7cfb\u7edf\u53ef\u4ee5\u88abPython\u8bed\u8a00\u914d\u7f6e, \u6216\u8005\u88ab\u4e00\u4e2a\u7528\u6237\u7684\u53ef\u7f16\u8f91\u7684\u914d\u7f6e\u6587\u4ef6\u52a0\u8f7d, \u4ee5\u6b64\u53bb\u81ea\u5b9a\u4e49\u65e5\u5fd7\u800c\u4e0d\u7528\u53bb\u4fee\u7a0b\u5e8f\u672c\u8eab.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">11.6. \u5f31\u5f15\u7528<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#tut-weak-references\"><\/a><\/h2>\n\n\n\n<p>Python\u8bed\u8a00\u81ea\u52a8\u7ba1\u7406\u5185\u5b58\uff08\u5927\u591a\u6570\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5bf9\u5b83\u7684\u5f15\u7528\u800c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/glossary.html#term-garbage-collection\">garbage collection<\/a>&nbsp;\u5bf9\u5b83\u4eec\u8fdb \u884c\u56de\u6536\uff09. \u5f53\u6700\u540e\u4e00\u4e2a\u5f15\u7528\u7ed3\u675f\u4e4b\u540e\u5185\u5b58\u5373\u523b\u88ab\u56de\u6536.<\/p>\n\n\n\n<p>\u8fd9\u79cd\u673a\u5236\u5bf9\u5927\u591a\u6570\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\u90fd\u662f\u6709\u6548\u7684, \u4f46\u4e5f\u6709\u5076\u7136\u60c5\u51b5, \u53ea\u6709\u5f53\u5b83\u4eec\u88ab\u5176\u5b83\u4e1c\u897f\u5f15\u7528\u7684\u65f6\u5019\u624d\u9700\u8981\u53bb \u8ddf\u8e2a\u5bf9\u8c61. \u4e0d\u5e78\u7684\u662f, \u4ec5\u4ec5\u662f\u8ddf\u8e2a\u5b83\u4eec\u4e5f\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5f15\u7528.\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/weakref.html#module-weakref\"><code>weakref<\/code><\/a>\u00a0\u6a21\u5757\u63d0\u4f9b\u4e00\u4e9b\u5de5\u5177\u53ef\u4ee5 \u8fbe\u5230\u540c\u6837\u7684\u6548\u679c\u800c\u4e0d\u7528\u53bb\u521b\u5efa\u4e00\u4e2a\u5f15\u7528. \u5f53\u4e0d\u518d\u9700\u8981\u8fd9\u4e2a\u5bf9\u8c61\u7684\u65f6\u5019, \u5b83\u4f1a\u81ea\u52a8\u5730\u4ece\u4e00\u4e2aweakref\u8868\u4e2d\u79fb\u9664 \u7136\u540e\u9275\u53d1\u5bf9weadref\u5bf9\u8c61\u7684\u56de\u8c03. \u901a\u5e38\u5e94\u7528\u7a0b\u5e8f\u90fd\u4f1a\u5bf9\u90a3\u4e9b\u521b\u5efa\u65f6\u82b1\u8d39\u8f83\u591a\u65f6\u95f4\u7684\u5bf9\u8c61\u63d0\u4f9b\u4e00\u4e2a\u7f13\u5b58:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"356\" src=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-20-1024x356.png\" alt=\"\" class=\"wp-image-421\" srcset=\"http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-20-1024x356.png 1024w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-20-300x104.png 300w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-20-768x267.png 768w, http:\/\/ai.gitpp.com\/wp-content\/uploads\/2023\/10\/image-20.png 1123w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">11.7. \u5904\u7406\u5217\u8868\u7684\u5de5\u5177<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#tut-list-tools\"><\/a><\/h2>\n\n\n\n<p>\u8bb8\u591a\u6570\u636e\u7684\u7ed3\u6784\u90fd\u9700\u8981\u7528\u5230\u5185\u7f6e\u7684\u5217\u8868\u7c7b\u578b. \u4f46\u6709\u65f6\u5019\u9700\u8981\u5728\u53ef\u9009\u62e9\u5730\u4e0d\u540c\u5448\u73b0\u65b9\u5f0f\u4e2d\u8fdb\u884c\u6743\u8861.<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/array.html#module-array\"><code>array<\/code><\/a>\u00a0\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/array.html#module-array\"><code>array()<\/code><\/a>\u00a0\u5bf9\u8c61, \u8fd9\u4e2a\u5bf9\u8c61\u50cf\u4e00\u4e2a\u5217\u8868\u4e00\u6837\u5b58\u50a8\u540c\u4e00\u7c7b\u578b\u7684\u6570\u636e, \u800c\u4e14\u66f4\u7b80\u6d01. \u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u6f14\u793a\u4e86\u5c06\u4e00\u7ec4\u6570\u5b57\u4ee5\u4e24\u4e2a\u5b57\u8282\u7684\u65e0\u7b26\u53f7\u6574\u6570\uff08\u7c7b\u578b\u7801\u00a0<code>H<\/code>) \u5f62\u5f0f\u5b58\u50a8\u4e3a\u4e00\u4e2a\u6570\u7ec4 \u800c\u4e0d\u662f\u901a\u5e38\u7684Python\u7684list\u5bf9\u8c61\u768416\u5b57\u8282\u7684\u5f62\u5f0f:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0fromarrayimport<\/strong>\u00a0array<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>a = array(\u2018H\u2019, [4000, 10, 700, 22222])<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>sum(a) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">26932\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>a[1:3] <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">array(\u2018H\u2019, [10, 700])<\/p>\n\n\n\n<p><code>cllections<\/code>\u00a0\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u00a0<code>deque()<\/code>\u00a0\u5bf9\u8c61, \u5b83\u53ef\u4ee5\u50cf\u4e00\u4e2a\u5217\u8868\u4e00\u6837\u5728\u5de6\u8fb9\u8fdb\u884c \u5feb\u901f\u7684apend\u548cpop\u64cd\u4f5c, \u4f46\u5728\u5185\u90e8\u67e5\u5bfb\u65f6\u76f8\u5bf9\u8f83\u6162. \u8fd9\u4e9b\u5bf9\u8c61\u53ef\u4ee5\u65b9\u4fbf\u5730\u6210\u4e3a\u4e00\u4e2a\u961f\u5217\u548c\u5730\u884c\u5e7f\u5ea6\u4f18\u5148\u6811 \u641c\u7d22:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0fromcollectionsimport<\/strong>\u00a0deque\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>d = deque([\u201ctask1\u201d, \u201ctask2\u201d, \u201ctask3\u201d])\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>d.append(\u201ctask4\u201d)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>print(\u201cHandling\u201d, d.popleft())<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> Handling task1<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> unsearched = deque([starting_node]) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">def breadth_first_search(unsearched): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">node = unsearched.popleft() <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">for m in gen_moves(node): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">if is_goal(m): <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">return m <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">unsearched.append(m)<\/p>\n\n\n\n<p>\u6b64\u5916, \u6807\u51c6\u5e93\u91cc\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u5b83\u7684\u5de5\u5177, \u5982\u00a0<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/bisect.html#module-bisect\"><code>bisect<\/code><\/a>\u00a0\u6a21\u5757, \u5b83\u6709\u4e00\u4e9b\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u7684\u51fd\u6570:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0importbisect>>>\u00a0<\/strong>scores = [(100, \u2018perl\u2019), (200, \u2018tcl\u2019), (400, \u2018lua\u2019), (500, \u2018python\u2019)]\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>bisect.insort(scores, (300, \u2018ruby\u2019))<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>scores<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> [(100, \u2018perl\u2019), (200, \u2018tcl\u2019), (300, \u2018ruby\u2019), (400, \u2018lua\u2019), (500, \u2018python\u2019)]<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/heapq.html#module-heapq\"><code>heapq<\/code><\/a>\u00a0\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u901a\u8fc7\u5e38\u89c4\u5217\u8868\u53bb\u5b9e\u73b0\u5806. \u6700\u4f4e\u5c42\u7684\u5165\u53e3\u901a\u5e38\u90fd\u5728\u96f6\u5904. \u8fd9\u5bf9\u4e8e\u91cd\u590d\u8bbf \u95ee\u4e00\u4e9b\u5f88\u5c0f\u7684\u5143\u7d20\u4f46\u53c8\u4e0d\u60f3\u5bf9\u6574\u4e2a\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u7684\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\u5341\u5206\u6709\u6548:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0fromheapqimport<\/strong>\u00a0heapify, heappop, heappush\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>heapify(data)\u00a0<em># \u5c06\u5217\u8868\u91cd\u65b0\u6574\u7406\u6210\u5806<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>heappush(data, -5)\u00a0<em># \u589e\u52a0\u4e00\u4e2a\u65b0\u5165\u53e3<\/em><\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>[heappop(data)\u00a0<strong>for<\/strong>\u00a0i\u00a0<strong>in<\/strong>\u00a0range(3)]\u00a0<em># \u53d6\u51fa\u8fd9\u4e09\u4e2a\u6700\u5c0f\u7684\u5165\u53e3<\/em>\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">[-5, 0, 1]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">11.8. \u5341\u8fdb\u5236\u6d6e\u70b9\u6570\u7684\u8fd0\u7b97<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#tut-decimal-fp\"><\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/decimal.html#module-decimal\"><code>decimal<\/code><\/a>&nbsp;\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a&nbsp;<code>Decimal<\/code>&nbsp;\u9488\u5bf9\u5341\u8fdb\u5236\u6d6e\u70b9\u5c0f\u6570\u8fd0\u7b97\u7684\u6570\u636e\u7c7b\u578b. \u4e0e\u5185 \u7f6e\u7684\u6570\u636e\u7c7b\u578b&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#float\"><code>float<\/code><\/a>&nbsp;\uff08\u9488\u5bf9\u4e8c\u8fdb\u5236\u6d6e\u70b9\u5c0f\u6570\uff09 \u76f8\u6bd4\u800c\u8a00, \u5b83\u5bf9\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u66f4\u4e3a\u6709\u6548<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u91d1\u878d\u65b9\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u548c\u5176\u5b83\u9700\u8981\u51c6\u786e\u663e\u793a\u5c0f\u6570\u7684\u5730\u65b9<\/li>\n\n\n\n<li>\u9700\u8981\u7cbe\u786e\u63a7\u5236,<\/li>\n\n\n\n<li>\u9700\u8981\u56db\u820d\u4e94\u5165\u4ee5\u6ee1\u8db3\u6cd5\u5236\u6216\u8005\u76d1\u7ba1\u8981\u6c42,<\/li>\n\n\n\n<li>\u9700\u8981\u8ddf\u8e2a\u6709\u610f\u4e49\u7684\u5c0f\u6570\u90e8\u5206, \u5373\u7cbe\u5ea6, \u6216\u8005,<\/li>\n\n\n\n<li>\u4e00\u4e9b\u7528\u6237\u5e0c\u671b\u7ed3\u679c\u7b26\u5408\u81ea\u5df1\u7684\u8ba1\u7b97\u8981\u6c42\u7684\u5e94\u7528\u7a0b\u5e8f.<\/li>\n<\/ul>\n\n\n\n<p>\u4f8b\u5982, \u8ba1\u7b97\u4e03\u6bdb\u94b1\u8bdd\u8d39\u76845\uff05\u7684\u7a0e\u6536, \u7528\u5341\u8fdb\u5236\u6d6e\u70b9\u5c0f\u6570\u548c\u4e8c\u8fdb\u5236\u6d6e\u70b9\u5c0f\u6570, \u5f97\u5230\u7684\u7ed3\u679c\u4f1a\u4e0d\u540c. \u5982\u679c\u7ed3\u679c\u4ee5\u5206 \u7684\u7cbe\u786e\u5ea6\u6765\u820d\u5165\u7684\u8bdd, \u8fd9\u79cd\u5dee\u5f02\u5c31\u4f1a\u53d8\u5f97\u5f88\u91cd\u8981:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0fromdecimalimport<\/strong>\u00a0*\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>round(Decimal(\u20180.70\u2019) * Decimal(\u20181.05\u2019), 2) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Decimal(\u20180.74\u2019)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>round(.70 * 1.05, 2)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 0.73<\/p>\n\n\n\n<p><code>Decimal<\/code>\u00a0\u7684\u7ed3\u679c\u4f1a\u5728\u672b\u5c3e\u8ffd\u52a00, \u81ea\u52a8\u4ece\u6709\u4e24\u4f4d\u6709\u6548\u6570\u5b57\u7684\u4e58\u6570\u76f8\u4e58\u4e2d\u5224\u65ad\u5e94\u6709\u56db\u4f4d\u6709\u6548\u6570\u5b57. Decimal\u590d\u5236\u4e86\u624b\u5de5\u8fd0\u7b97\u7684\u7cbe\u5ea6, \u907f\u514d\u4e86\u4e8c\u8fdb\u5236\u6d6e\u70b9\u5c0f\u6570\u4e0d\u80fd\u51c6\u786e\u8868\u793a\u5341\u8fdb\u6570\u7cbe\u5ea6\u800c\u4ea7\u751f\u7684\u95ee\u9898.<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>Decimal(\u20181.00\u2019) % Decimal(\u2018.10\u2019) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Decimal(\u20180.00\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>1.00 % 0.10<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> 0.09999999999999995<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">\u00a0<strong>>>>\u00a0<\/strong>sum([Decimal(\u20180.1\u2019)]*10) == Decimal(\u20181.0\u2019) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">True\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>sum([0.1]*10) == 1.0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> False<\/p>\n\n\n\n<p>\u7cbe\u786e\u7684\u663e\u793a\u4f7f\u5f97\u00a0<code>Decimal<\/code>\u00a0\u53ef\u4ee5\u8fdb\u884c\u6a21\u8fd0\u7b97\u548c\u5224\u65ad\u503c\u7684\u7b49\u540c\u6027, \u800c\u8fd9\u4e9b\u662f\u4e8c\u8fdb\u5236\u6d6e\u70b9\u6570\u4e0d\u9002\u5408\u7684:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>Decimal(\u20181.00\u2019) % Decimal(\u2018.10\u2019)<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"> Decimal(\u20180.00\u2019)\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>1.00 % 0.10 <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">0.09999999999999995\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>sum([Decimal(\u20180.1\u2019)]*10) == Decimal(\u20181.0\u2019) True\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>sum([0.1]*10) == 1.0 False<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/decimal.html#module-decimal\"><code>decimal<\/code><\/a>\u00a0\u6a21\u5757\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u9700\u6c42\u7684\u7cbe\u5ea6\u8fd0\u7b97:<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>getcontext().prec = 36\u00a0<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><strong>>>>\u00a0<\/strong>Decimal(1) \/ Decimal(7) <\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\">Decimal(\u20180.142857142857142857142857142857142857\u2019)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b2c\u4e8c\u90e8\u5206\u6db5\u76d6\u4e86\u8bb8\u591a\u5173\u4e8e\u4e13\u4e1a\u7f16\u7a0b\u9700\u6c42\u7684\u9ad8\u7ea7\u6a21\u5757. \u8fd9\u4e9b\u6a21\u5757\u5f88\u5c11\u51fa\u73b0\u5728\u5c0f\u7684\u7a0b\u5e8f\u5f53\u4e2d. 11.1. \u683c\u5f0f\u5316\u8f93\u51fa re [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-264","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":"","_links":{"self":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=264"}],"version-history":[{"count":12,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/264\/revisions"}],"predecessor-version":[{"id":425,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/264\/revisions\/425"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}