{"id":208,"date":"2023-10-26T16:38:30","date_gmt":"2023-10-26T08:38:30","guid":{"rendered":"http:\/\/ai.gitpp.com\/?p=208"},"modified":"2023-10-27T09:38:38","modified_gmt":"2023-10-27T01:38:38","slug":"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","status":"publish","type":"post","link":"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\/","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<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>&nbsp;\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&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/functions.html#repr\"><code>repr()<\/code><\/a>, \u5b83\u53ea\u663e\u793a\u7f29\u7565\u56fe<strong>&gt;&gt;&gt; <\/strong><strong>import<\/strong><strong>reprlib<\/strong><strong>&gt;&gt;&gt; <\/strong>reprlib.repr(set(&#8216;supercalifragilisticexpialidocious&#8217;)) &#8220;set([&#8216;a&#8217;, &#8216;c&#8217;, &#8216;d&#8217;, &#8216;e&#8217;, &#8216;f&#8217;, &#8216;g&#8217;, &#8230;])&#8221;<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/pprint.html#module-pprint\"><code>pprint<\/code><\/a>&nbsp;\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:<strong>&gt;&gt;&gt; <\/strong><strong>import<\/strong><strong>pprint<\/strong><strong>&gt;&gt;&gt; <\/strong>t = [[[[&#8216;black&#8217;, &#8216;cyan&#8217;], &#8216;white&#8217;, [&#8216;green&#8217;, &#8216;red&#8217;]], [[&#8216;magenta&#8217;, <strong>&#8230;<\/strong> &#8216;yellow&#8217;], &#8216;blue&#8217;]]] <strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>pprint.pprint(t, width=30) [[[[&#8216;black&#8217;, &#8216;cyan&#8217;], &#8216;white&#8217;, [&#8216;green&#8217;, &#8216;red&#8217;]], [[&#8216;magenta&#8217;, &#8216;yellow&#8217;], &#8216;blue&#8217;]]]<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/textwrap.html#module-textwrap\"><code>textwrap<\/code><\/a>&nbsp;\u6a21\u5757\u4f1a\u6839\u636e\u5c4f\u5e55\u7684\u5bbd\u5ea6\u800c\u9002\u5f53\u5730\u53bb\u8c03\u6574\u6587\u672c\u6bb5\u843d:<strong>&gt;&gt;&gt; <\/strong><strong>import<\/strong><strong>textwrap<\/strong><strong>&gt;&gt;&gt; <\/strong>doc = &#8220;&#8221;&#8221;The wrap() method is just like fill() except that it returns <strong>&#8230; <\/strong>a list of strings instead of one big string with newlines to separate <strong>&#8230; <\/strong>the wrapped lines.&#8221;&#8221;&#8221; <strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>print(textwrap.fill(doc, width=40)) The wrap() method is just like fill() except that it returns a list of strings instead of one big string with newlines to separate the wrapped lines.<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/locale.html#module-locale\"><code>locale<\/code><\/a>&nbsp;\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:<strong>&gt;&gt;&gt; <\/strong><strong>import<\/strong><strong>locale<\/strong><strong>&gt;&gt;&gt; <\/strong>locale.setlocale(locale.LC_ALL, &#8216;English_United States.1252&#8217;) &#8216;English_United States.1252&#8217; <strong>&gt;&gt;&gt; <\/strong>conv = locale.localeconv() <em># \u5f97\u5230\u4e00\u4e2a\u5e38\u89c4\u6846\u67b6\u7684\u6620\u5c04<\/em><strong>&gt;&gt;&gt; <\/strong>x = 1234567.8 <strong>&gt;&gt;&gt; <\/strong>locale.format(&#8220;<em>%d<\/em>&#8220;, x, grouping=<strong>True<\/strong>) &#8216;1,234,567&#8217; <strong>&gt;&gt;&gt; <\/strong>locale.format_string(&#8220;<em>%s%.*f<\/em>&#8220;, (conv[&#8216;currency_symbol&#8217;], <strong>&#8230;<\/strong> conv[&#8216;frac_digits&#8217;], x), grouping=<strong>True<\/strong>) &#8216;$1,234,567.80&#8217;<\/p>\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&nbsp;<code>$<\/code>&nbsp;\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&nbsp;<code>$$<\/code>&nbsp;\u53bb\u8868\u793a \u5355\u72ec\u7684&nbsp;<code>$<\/code><strong>&gt;&gt;&gt; <\/strong><strong>from<\/strong><strong>string<\/strong><strong>import<\/strong> Template <strong>&gt;&gt;&gt; <\/strong>t = Template(&#8216;$<em>{village}<\/em>folk send $$10 to $cause.&#8217;) <strong>&gt;&gt;&gt; <\/strong>t.substitute(village=&#8217;Nottingham&#8217;, cause=&#8217;the ditch fund&#8217;) &#8216;Nottinghamfolk send $10 to the ditch fund.&#8217;<\/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,&nbsp;<code>substitute()<\/code>&nbsp;\u65b9\u6cd5\u4f1a\u629b\u51fa\u4e00\u4e2a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/exceptions.html#KeyError\"><code>KeyError<\/code><\/a>&nbsp;\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,&nbsp;<code>safe_substitute()<\/code>&nbsp;\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:<strong>&gt;&gt;&gt; <\/strong>t = Template(&#8216;Return the $item to $owner.&#8217;) <strong>&gt;&gt;&gt; <\/strong>d = dict(item=&#8217;unladen swallow&#8217;) <strong>&gt;&gt;&gt; <\/strong>t.substitute(d) Traceback (most recent call last): . . . KeyError: &#8216;owner&#8217; <strong>&gt;&gt;&gt; <\/strong>t.safe_substitute(d) &#8216;Return the unladen swallow to $owner.&#8217;<\/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:<strong>&gt;&gt;&gt; <\/strong><strong>import<\/strong><strong>time<\/strong>, <strong>os.path<\/strong><strong>&gt;&gt;&gt; <\/strong>photofiles = [&#8216;img_1074.jpg&#8217;, &#8216;img_1076.jpg&#8217;, &#8216;img_1077.jpg&#8217;] <strong>&gt;&gt;&gt; <\/strong><strong>class<\/strong><strong>BatchRename<\/strong>(Template): <strong>&#8230;<\/strong> delimiter = &#8216;%&#8217; <strong>&gt;&gt;&gt; <\/strong>fmt = input(&#8216;Enter rename style (<em>%d<\/em>-date %n-seqnum <em>%f<\/em>-format): &#8216;) Enter rename style (%d-date %n-seqnum %f-format): Ashley_%n%f <strong>&gt;&gt;&gt; <\/strong>t = BatchRename(fmt) <strong>&gt;&gt;&gt; <\/strong>date = time.strftime(&#8216;<em>%d<\/em>%b%y&#8217;) <strong>&gt;&gt;&gt; <\/strong><strong>for<\/strong> i, filename <strong>in<\/strong> enumerate(photofiles): <strong>&#8230;<\/strong> base, ext = os.path.splitext(filename) <strong>&#8230;<\/strong> newname = t.substitute(d=date, n=i, f=ext) <strong>&#8230;<\/strong> print(&#8216;<em>{0}<\/em> &#8211;&gt; <em>{1}<\/em>&#8216;.format(filename, newname)) img_1074.jpg &#8211;&gt; Ashley_0.jpg img_1076.jpg &#8211;&gt; Ashley_1.jpg img_1077.jpg &#8211;&gt; 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>&nbsp;\u6a21\u5757\u4e00\u4e9b\u51fd\u6570, \u5982&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#id10\">:fun:`pack`<\/a>&nbsp;\u548c&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#id12\">:fun:`unpack`<\/a>&nbsp;\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&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/zipfile.html#module-zipfile\"><code>zipfile<\/code><\/a>&nbsp;\u6a21\u5757\u7684\u60c5\u51b5\u4e0b\u53bb\u5faa\u73af\u5f97\u5230\u4e00\u4e2aZIP\u6587\u4ef6\u7684 \u6807\u9898\u4fe1\u606f. \u5305\u4ee3\u7801&nbsp;<code>H<\/code>&nbsp;\u548c&nbsp;<code>I<\/code>&nbsp;\u5206\u522b\u8868\u793a\u4e24\u4e2a\u548c\u56db\u4e2a\u5b57\u8282\u7684\u65e0\u7b26\u53f7\u6570\u5b57. \u800c&nbsp;<code>&lt;<\/code>&nbsp;\u5219\u8868\u793a\u5b83\u4eec\u662f\u6807\u51c6 \u5927\u5c0f\u5e76\u4ee5\u5b57\u8282\u5927\u5c0f\u7684\u987a\u5e8f\u6392\u5217\u5728\u540e\u9762:<strong>import<\/strong><strong>struct<\/strong> data = open(&#8216;myfile.zip&#8217;, &#8216;rb&#8217;).read() start = 0 <strong>for<\/strong> i <strong>in<\/strong> range(3): <em># \u663e\u793a\u6700\u5f00\u59cb\u76843\u4e2a\u6807\u9898<\/em> start += 14 fields = struct.unpack(&#8216;&lt;IIIHH&#8217;, data[start:start+16]) crc32, comp_size, uncomp_size, filenamesize, extra_size = fields start += 16 filename = data[start:start+filenamesize] start += filenamesize extra = data[start:start+extra_size] print(filename, hex(crc32), comp_size, uncomp_size) start += extra_size + comp_size <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&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/threading.html#module-threading\"><code>threading<\/code><\/a>&nbsp;\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:<strong>import<\/strong><strong>threading<\/strong>, <strong>zipfile<\/strong><strong>class<\/strong><strong>AsyncZip<\/strong>(threading.Thread): <strong>def<\/strong> __init__(self, infile, outfile): threading.Thread.__init__(self) self.infile = infile self.outfile = outfile <strong>def<\/strong> run(self): f = zipfile.ZipFile(self.outfile, &#8216;w&#8217;, zipfile.ZIP_DEFLATED) f.write(self.infile) f.close() print(&#8216;Finished background zip of:&#8217;, self.infile) background = AsyncZip(&#8216;mydata.txt&#8217;, &#8216;myarchive.zip&#8217;) background.start() print(&#8216;The main program continues to run in foreground.&#8217;) background.join() <em># \u7b49\u5f85\u540e\u53f0\u4efb\u52a1\u7ed3\u675f<\/em> print(&#8216;Main program waited until background was done.&#8217;)<\/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>&nbsp;\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&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/tutorial\/stdlib2.html#id16\">&#8220;<\/a>sys.stderr`:<strong>import<\/strong><strong>logging<\/strong> logging.debug(&#8216;Debugging information&#8217;) logging.info(&#8216;Informational message&#8217;) logging.warning(&#8216;Warning:config file <em>%s<\/em> not found&#8217;, &#8216;server.conf&#8217;) logging.error(&#8216;Error occurred&#8217;) logging.critical(&#8216;Critical error &#8212; shutting down&#8217;)<\/p>\n\n\n\n<p>\u4e0a\u8ff0\u4f1a\u4ea7\u751f\u4ee5\u4e0b\u7684\u8f93\u51fa:WARNING:root:Warning:config file server.conf <strong>not<\/strong> found ERROR:root:Error occurred CRITICAL:root:Critical error &#8212; 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.&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/weakref.html#module-weakref\"><code>weakref<\/code><\/a>&nbsp;\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:<strong>&gt;&gt;&gt; <\/strong><strong>import<\/strong><strong>weakref<\/strong>, <strong>gc<\/strong><strong>&gt;&gt;&gt; <\/strong><strong>class<\/strong><strong>A<\/strong>: <strong>&#8230; <\/strong><strong>def<\/strong> __init__(self, value): <strong>&#8230;<\/strong> self.value = value <strong>&#8230; <\/strong><strong>def<\/strong> __repr__(self): <strong>&#8230; <\/strong><strong>return<\/strong> str(self.value) <strong>&#8230;<\/strong><strong>&gt;&gt;&gt; <\/strong>a = A(10) <em># \u521b\u5efa\u4e00\u4e2a\u5f15\u7528<\/em><strong>&gt;&gt;&gt; <\/strong>d = weakref.WeakValueDictionary() <strong>&gt;&gt;&gt; <\/strong>d[&#8216;primary&#8217;] = a <em># \u6ca1\u6709\u521b\u5efa\u4e00\u4e2a\u5f15\u7528<\/em><strong>&gt;&gt;&gt; <\/strong>d[&#8216;primary&#8217;] <em># \u5982\u679c\u5b58\u5728\u7684\u8bdd\u83b7\u53d6\u8fd9\u4e2a\u5bf9\u65ec<\/em> 10 <strong>&gt;&gt;&gt; <\/strong><strong>del<\/strong> a <em># \u79fb\u9664\u8fd9\u4e2a\u5f15\u548c<\/em><strong>&gt;&gt;&gt; <\/strong>gc.collect() <em># \u76f4\u63a5\u8c03\u7528\u56de\u6536\u673a\u5236<\/em> 0 <strong>&gt;&gt;&gt; <\/strong>d[&#8216;primary&#8217;] <em># \u8c03\u7528\u7684\u5165\u53e3\u5df2\u7ecf\u81ea\u52a8\u88ab\u79fb\u9664\u4e86<\/em> Traceback (most recent call last): File &#8220;&lt;stdin&gt;&#8221;, line 1, in &lt;module&gt; d[&#8216;primary&#8217;] <em># \u8c03\u7528\u7684\u5165\u53e3\u5df2\u7ecf\u81ea\u52a8\u88ab\u79fb\u9664\u4e86<\/em> File &#8220;C:\/python31\/lib\/weakref.py&#8221;, line 46, in __getitem__ o = self.data[key]() KeyError: &#8216;primary&#8217;<\/p>\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>&nbsp;\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/array.html#module-array\"><code>array()<\/code><\/a>&nbsp;\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&nbsp;<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:<strong>&gt;&gt;&gt; <\/strong><strong>from<\/strong><strong>array<\/strong><strong>import<\/strong> array <strong>&gt;&gt;&gt; <\/strong>a = array(&#8216;H&#8217;, [4000, 10, 700, 22222]) <strong>&gt;&gt;&gt; <\/strong>sum(a) 26932 <strong>&gt;&gt;&gt; <\/strong>a[1:3] array(&#8216;H&#8217;, [10, 700])<\/p>\n\n\n\n<p><code>cllections<\/code>&nbsp;\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a&nbsp;<code>deque()<\/code>&nbsp;\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:<strong>&gt;&gt;&gt; <\/strong><strong>from<\/strong><strong>collections<\/strong><strong>import<\/strong> deque <strong>&gt;&gt;&gt; <\/strong>d = deque([&#8220;task1&#8221;, &#8220;task2&#8221;, &#8220;task3&#8221;]) <strong>&gt;&gt;&gt; <\/strong>d.append(&#8220;task4&#8221;) <strong>&gt;&gt;&gt; <\/strong>print(&#8220;Handling&#8221;, d.popleft()) Handling task1 unsearched = deque([starting_node]) def breadth_first_search(unsearched): node = unsearched.popleft() for m in gen_moves(node): if is_goal(m): return m 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&nbsp;<a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/bisect.html#module-bisect\"><code>bisect<\/code><\/a>&nbsp;\u6a21\u5757, \u5b83\u6709\u4e00\u4e9b\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u7684\u51fd\u6570:<strong>&gt;&gt;&gt; <\/strong><strong>import<\/strong><strong>bisect<\/strong><strong>&gt;&gt;&gt; <\/strong>scores = [(100, &#8216;perl&#8217;), (200, &#8216;tcl&#8217;), (400, &#8216;lua&#8217;), (500, &#8216;python&#8217;)] <strong>&gt;&gt;&gt; <\/strong>bisect.insort(scores, (300, &#8216;ruby&#8217;)) <strong>&gt;&gt;&gt; <\/strong>scores [(100, &#8216;perl&#8217;), (200, &#8216;tcl&#8217;), (300, &#8216;ruby&#8217;), (400, &#8216;lua&#8217;), (500, &#8216;python&#8217;)]<\/p>\n\n\n\n<p><a href=\"https:\/\/docspy3zh.readthedocs.io\/en\/latest\/library\/heapq.html#module-heapq\"><code>heapq<\/code><\/a>&nbsp;\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:<strong>&gt;&gt;&gt; <\/strong><strong>from<\/strong><strong>heapq<\/strong><strong>import<\/strong> heapify, heappop, heappush <strong>&gt;&gt;&gt; <\/strong>data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0] <strong>&gt;&gt;&gt; <\/strong>heapify(data) <em># \u5c06\u5217\u8868\u91cd\u65b0\u6574\u7406\u6210\u5806<\/em><strong>&gt;&gt;&gt; <\/strong>heappush(data, -5) <em># \u589e\u52a0\u4e00\u4e2a\u65b0\u5165\u53e3<\/em><strong>&gt;&gt;&gt; <\/strong>[heappop(data) <strong>for<\/strong> i <strong>in<\/strong> range(3)] <em># \u53d6\u51fa\u8fd9\u4e09\u4e2a\u6700\u5c0f\u7684\u5165\u53e3<\/em> [-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:<strong>&gt;&gt;&gt; <\/strong><strong>from<\/strong><strong>decimal<\/strong><strong>import<\/strong> * <strong>&gt;&gt;&gt; <\/strong>round(Decimal(&#8216;0.70&#8217;) * Decimal(&#8216;1.05&#8217;), 2) Decimal(&#8216;0.74&#8217;) <strong>&gt;&gt;&gt; <\/strong>round(.70 * 1.05, 2) 0.73<\/p>\n\n\n\n<p><code>Decimal<\/code>&nbsp;\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.<strong>&gt;&gt;&gt; <\/strong>Decimal(&#8216;1.00&#8217;) % Decimal(&#8216;.10&#8217;) Decimal(&#8216;0.00&#8217;) <strong>&gt;&gt;&gt; <\/strong>1.00 % 0.10 0.09999999999999995 <strong>&gt;&gt;&gt; <\/strong>sum([Decimal(&#8216;0.1&#8217;)]*10) == Decimal(&#8216;1.0&#8217;) True <strong>&gt;&gt;&gt; <\/strong>sum([0.1]*10) == 1.0 False<\/p>\n\n\n\n<p>\u7cbe\u786e\u7684\u663e\u793a\u4f7f\u5f97&nbsp;<code>Decimal<\/code>&nbsp;\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:<strong>&gt;&gt;&gt; <\/strong>Decimal(&#8216;1.00&#8217;) % Decimal(&#8216;.10&#8217;) Decimal(&#8216;0.00&#8217;) <strong>&gt;&gt;&gt; <\/strong>1.00 % 0.10 0.09999999999999995 <strong>&gt;&gt;&gt; <\/strong>sum([Decimal(&#8216;0.1&#8217;)]*10) == Decimal(&#8216;1.0&#8217;) True <strong>&gt;&gt;&gt; <\/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>&nbsp;\u6a21\u5757\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u9700\u6c42\u7684\u7cbe\u5ea6\u8fd0\u7b97:<strong>&gt;&gt;&gt; <\/strong>getcontext().prec = 36 <strong>&gt;&gt;&gt; <\/strong>Decimal(1) \/ Decimal(7) Decimal(&#8216;0.142857142857142857142857142857142857&#8217;)<\/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-208","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\/208","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=208"}],"version-history":[{"count":3,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":256,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/posts\/208\/revisions\/256"}],"wp:attachment":[{"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ai.gitpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}