{"id":1002050,"date":"2024-12-27T10:05:23","date_gmt":"2024-12-27T02:05:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1002050.html"},"modified":"2024-12-27T10:05:25","modified_gmt":"2024-12-27T02:05:25","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e5%8d%95%e4%bd%8dcm","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1002050.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51fa\u5355\u4f4dcm"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075953\/e172842b-0896-40f3-b6dc-aa871b3e5453.webp\" alt=\"python\u5982\u4f55\u8f93\u51fa\u5355\u4f4dcm\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8f93\u51fa\u5355\u4f4d\u201ccm\u201d\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u5b57\u7b26\u4e32\u62fc\u63a5\u548c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u5b9e\u73b0\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u8f93\u51fa\u5e26\u6709\u5355\u4f4d\u201ccm\u201d\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u8f93\u51fa\u5e26\u5355\u4f4d\u7684\u503c\u662f\u4e00\u79cd\u975e\u5e38\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662ff-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff09\u3002f-string\u5141\u8bb8\u4f60\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\u5e76\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u8f93\u51fa\u5e26\u5355\u4f4d\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 10<\/p>\n<p>unit = &quot;cm&quot;<\/p>\n<p>formatted_output = f&quot;{value} {unit}&quot;<\/p>\n<p>print(formatted_output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u503c<code>value<\/code>\u548c\u4e00\u4e2a\u5355\u4f4d<code>unit<\/code>\uff0c\u7136\u540e\u4f7f\u7528f-string\u5c06\u5b83\u4eec\u7ec4\u5408\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u5e76\u8f93\u51fa\u3002f-string\u901a\u8fc7\u5728\u5927\u62ec\u53f7\u4e2d\u76f4\u63a5\u63d2\u5165\u53d8\u91cf\u540d\u6765\u5f15\u7528\u53d8\u91cf\u7684\u503c\uff0c\u975e\u5e38\u76f4\u89c2\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5173\u4e8ePython\u4e2d\u5982\u4f55\u8f93\u51fa\u5355\u4f4d\u201ccm\u201d\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b57\u7b26\u4e32\u62fc\u63a5<\/p>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u62fc\u63a5\u662f\u4e00\u79cd\u57fa\u672c\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5c06\u6570\u503c\u4e0e\u5355\u4f4d\u5b57\u7b26\u4e32\u8fdb\u884c\u8fde\u63a5\uff0c\u53ef\u4ee5\u751f\u6210\u5e26\u5355\u4f4d\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 20<\/p>\n<p>unit = &quot;cm&quot;<\/p>\n<p>output = str(value) + &quot; &quot; + unit<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\uff0c\u4f46\u5728\u590d\u6742\u683c\u5f0f\u5316\u9700\u6c42\u65f6\u53ef\u80fd\u663e\u5f97\u5197\u957f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<ol>\n<li><strong>f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff09<\/strong><\/li>\n<\/ol>\n<p><p>Python 3.6\u5f15\u5165\u4e86f-string\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6027\u80fd\u66f4\u597d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 15<\/p>\n<p>output = f&quot;{value} cm&quot;<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>f-string\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u5927\u62ec\u53f7\uff0c\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528\u53d8\u91cf\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u52a8\u6001\u751f\u6210\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>str.format()\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>str.format()\u65b9\u6cd5\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u652f\u6301\u590d\u6742\u7684\u683c\u5f0f\u5316\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = 25<\/p>\n<p>output = &quot;{} cm&quot;.format(value)<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4e8e\u9700\u8981\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u591a\u4e2a\u53d8\u91cf\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u5355\u4f4d\u8f93\u51fa\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u591a\u6b21\u4f7f\u7528\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def format_with_unit(value, unit=&quot;cm&quot;):<\/p>\n<p>    return f&quot;{value} {unit}&quot;<\/p>\n<p>print(format_with_unit(30))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u51fd\u6570\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u590d\u7528\u6027\uff0c\u8fd8\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u5b9e\u73b0\u66f4\u591a\u7684\u903b\u8f91\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408\u79d1\u5b66\u8ba1\u7b97\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u6216\u6570\u636e\u5206\u6790\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u5e26\u5355\u4f4d\u7684\u6570\u503c\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408\u5e93\u5982NumPy\u3001Pandas\u7b49\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;Length&#39;: [10, 20, 30]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&#39;Length&#39;] = df[&#39;Length&#39;].astype(str) + &quot; cm&quot;<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u5e76\u8fdb\u884c\u6279\u91cf\u683c\u5f0f\u5316\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u5904\u7406\u5e26\u5355\u4f4d\u7684\u6570\u503c\uff0c\u4f8b\u5982Pint\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pint import UnitRegistry<\/p>\n<p>ureg = UnitRegistry()<\/p>\n<p>length = 10 * ureg.centimeter<\/p>\n<p>print(length)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pint\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u7269\u7406\u5355\u4f4d\uff0c\u5e76\u652f\u6301\u5355\u4f4d\u8f6c\u6362\u7b49\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8f93\u51fa\u5e26\u5355\u4f4d\u7684\u6570\u503c\uff0c\u4ece\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u5230\u4f7f\u7528\u4e13\u4e1a\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u573a\u666f\u3002\u5728\u9009\u62e9\u5177\u4f53\u65b9\u6cd5\u65f6\uff0c\u5e94\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u9879\u76ee\u590d\u6742\u5ea6\u8fdb\u884c\u6743\u8861\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5370\u5e26\u6709\u5355\u4f4d\u201ccm\u201d\u7684\u6570\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u5c06\u6570\u503c\u548c\u5355\u4f4d\u7ed3\u5408\u8d77\u6765\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u6765\u8f93\u51fa\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u53d8\u91cf<code>length<\/code>\uff0c\u5176\u503c\u4e3a10\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8f93\u51fa&quot;10 cm&quot;\uff1a  <\/p>\n<pre><code class=\"language-python\">length = 10\nprint(f&quot;{length} cm&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5229\u7528\u4e86f-string\u683c\u5f0f\u5316\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u5e26\u6709\u5355\u4f4d\u7684\u6570\u503c\u8fd0\u7b97\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u9700\u8981\u5bf9\u5e26\u6709\u5355\u4f4d\u7684\u6570\u503c\u8fdb\u884c\u8fd0\u7b97\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7c7b\u6765\u5c01\u88c5\u8fd9\u4e9b\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>Length<\/code>\u7c7b\uff0c\u5305\u542b\u6570\u503c\u548c\u5355\u4f4d\u5c5e\u6027\uff0c\u5e76\u5b9a\u4e49\u76f8\u5e94\u7684\u8fd0\u7b97\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">class Length:\n    def __init__(self, value, unit):\n        self.value = value\n        self.unit = unit\n\n    def __add__(self, other):\n        if self.unit == other.unit:\n            return Length(self.value + other.value, self.unit)\n        else:\n            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u5355\u4f4d\u4e0d\u4e00\u81f4&quot;)\n\nlength1 = Length(10, &quot;cm&quot;)\nlength2 = Length(5, &quot;cm&quot;)\nresult = length1 + length2\nprint(f&quot;{result.value} {result.unit}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5355\u4f4d\u4e00\u81f4\u7684\u6570\u503c\u8fd0\u7b97\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u683c\u5f0f\u5316\u8f93\u51fa\u5e26\u5355\u4f4d\u7684\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u8f93\u51fa\u5e26\u6709\u5355\u4f4d\u7684\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u63a7\u5236\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002\u4f7f\u7528<code>:.2f<\/code>\u53ef\u4ee5\u786e\u4fdd\u8f93\u51fa\u65f6\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">length = 10.5678\nprint(f&quot;{length:.2f} cm&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4f1a\u8f93\u51fa\u201c10.57 cm\u201d\uff0c\u786e\u4fdd\u4e86\u6570\u636e\u7684\u7f8e\u89c2\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8f93\u51fa\u5355\u4f4d\u201ccm\u201d\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u62fc\u63a5\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1002057,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002050"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1002050"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002050\/revisions"}],"predecessor-version":[{"id":1002064,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002050\/revisions\/1002064"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1002057"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1002050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1002050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1002050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}