{"id":174338,"date":"2024-05-08T18:29:43","date_gmt":"2024-05-08T10:29:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/174338.html"},"modified":"2024-05-08T18:29:48","modified_gmt":"2024-05-08T10:29:48","slug":"python-%e5%a6%82%e4%bd%95%e5%bf%ab%e9%80%9f%e6%9c%89%e8%a7%84%e5%be%8b%e7%9a%84%e5%91%bd%e5%90%8d%e5%91%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/174338.html","title":{"rendered":"python \u5982\u4f55\u5feb\u901f\u6709\u89c4\u5f8b\u7684\u547d\u540d\u5462"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27050512\/edda7f51-c8d7-467f-9421-d773b4b05044.webp\" alt=\"python \u5982\u4f55\u5feb\u901f\u6709\u89c4\u5f8b\u7684\u547d\u540d\u5462\" \/><\/p>\n<p><p>\u5728Python\u4e2d\u5b9e\u73b0\u5feb\u901f\u3001\u89c4\u8303\u7684\u547d\u540d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b8c\u6210\uff0c<strong>\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u3001\u5229\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4ee5\u53ca\u5f15\u5165\u547d\u540d\u89c4\u5219\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u5229\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong>\u5728\u5b9e\u73b0\u6279\u91cf\u4e14\u6709\u89c4\u5f8b\u7684\u547d\u540d\u4e2d\u5c24\u4e3a\u9ad8\u6548\u4e14\u4f7f\u7528\u5e7f\u6cdb\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u4e00\u5b9a\u7684\u6a21\u677f\uff0c\u7ed3\u5408\u5faa\u73af\u53d8\u91cf\uff0c\u6765\u751f\u6210\u4e00\u7cfb\u5217\u7b26\u5408\u89c4\u8303\u4e14\u6613\u4e8e\u533a\u5206\u7684\u53d8\u91cf\u540d\u3001\u6587\u4ef6\u540d\u6216\u5176\u4ed6\u6807\u8bc6\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5faa\u73af\u7ed3\u6784\u547d\u540d<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u7ed3\u6784\u662f\u5b9e\u73b0\u6279\u91cf\u547d\u540d\u7684\u57fa\u7840\uff0c\u53ef\u4ee5\u901a\u8fc7for\u5faa\u73af\u914d\u5408\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u6216\u683c\u5f0f\u5316\u529f\u80fd\u6765\u5b8c\u6210\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u547d\u540d\u5e8f\u5217\u8f83\u77ed\u4e14\u89c4\u5f8b\u7b80\u5355\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>+<\/code>\u64cd\u4f5c\u7b26\uff0c\u53ef\u4ee5\u5c06\u5faa\u73af\u53d8\u91cf\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u5e76\u4e0e\u5176\u4ed6\u5b57\u7b26\u4e32\u62fc\u63a5\u8d77\u6765\uff0c\u5f62\u6210\u65b0\u7684\u547d\u540d\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 11):<\/p>\n<p>    var_name = &quot;item&quot; + str(i)<\/p>\n<p>    print(var_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u751f\u6210<code>item1<\/code>\u5230<code>item10<\/code>\u7684\u4e00\u7cfb\u5217\u53d8\u91cf\u540d\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5982<code>f-strings<\/code>\uff08Python 3.6+\uff09\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7075\u6d3b\u548c\u6613\u8bfb\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5e26\u6709\u52a8\u6001\u5185\u5bb9\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 11):<\/p>\n<p>    var_name = f&quot;item{i}&quot;<\/p>\n<p>    print(var_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u66f4\u52a0\u7075\u6d3b\u5730\u63a7\u5236\u6570\u5b57\u7684\u683c\u5f0f\uff0c\u5982\u586b\u5145\u96f6\u4f7f\u957f\u5ea6\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u662f\u5b9e\u73b0\u9ad8\u6548\u3001\u6709\u89c4\u5f8b\u547d\u540d\u7684\u5173\u952e\u6280\u672f\u4e4b\u4e00\u3002\u5b83\u4e0d\u4ec5\u5e94\u7528\u4e8e\u5faa\u73af\u7ed3\u6784\u547d\u540d\u4e2d\uff0c\u4e5f\u80fd\u72ec\u7acb\u4f7f\u7528\u4e8e\u66f4\u590d\u6742\u7684\u547d\u540d\u9700\u8981\u3002<\/p>\n<\/p>\n<p><h4>\u5229\u7528<code>str.format()<\/code><\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u53d8\u91cf\uff0c\u5e76\u53ef\u4ee5\u63a7\u5236\u53d8\u91cf\u7684\u663e\u793a\u683c\u5f0f\uff0c\u975e\u5e38\u9002\u7528\u4e8e\u590d\u6742\u7684\u547d\u540d\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 11):<\/p>\n<p>    var_name = &quot;item{:02d}&quot;.format(i)<\/p>\n<p>    print(var_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u683c\u5f0f\u5316\u9009\u9879\uff0c\u5982<code>{:02d}<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6570\u5b57\u7684\u96f6\u586b\u5145\uff0c\u4fdd\u8bc1\u6570\u5b57\u90e8\u5206\u603b\u662f\u4e24\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528f-strings<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8ePython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\uff0c<code>f-strings<\/code>\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u5176\u76f4\u63a5\u5185\u5d4c\u53d8\u91cf\u7684\u529f\u80fd\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(1, 11):<\/p>\n<p>    var_name = f&quot;item{i:02d}&quot;<\/p>\n<p>    print(var_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u76f8\u6bd4\u4e8e<code>.format()<\/code>\u65b9\u6cd5\uff0c<code>f-strings<\/code>\u5728\u6267\u884c\u901f\u5ea6\u548c\u8bed\u6cd5\u7b80\u6d01\u6027\u4e0a\u90fd\u6709\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5f15\u5165\u547d\u540d\u89c4\u5219\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u9879\u76ee\uff0c\u53ef\u4ee5\u8003\u8651\u5229\u7528\u73b0\u6709\u7684\u547d\u540d\u89c4\u5219\u5e93\u6765\u5b9e\u73b0\u5feb\u901f\u4e14\u89c4\u8303\u7684\u547d\u540d\u3002\u8fd9\u4e9b\u5e93\u901a\u5e38\u63d0\u4f9b\u4e86\u4e00\u5957\u9884\u8bbe\u7684\u547d\u540d\u89c4\u5219\uff0c\u6216\u8005\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u89c4\u5219\uff0c\u6765\u81ea\u52a8\u5316\u5730\u751f\u6210\u7b26\u5408\u7279\u5b9a\u89c4\u8303\u7684\u547d\u540d\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528Inflector\u5e93<\/h4>\n<\/p>\n<p><p>Inflector\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Python\u5e93\uff0c\u652f\u6301\u591a\u79cd\u547d\u540d\u98ce\u683c\u7684\u8f6c\u6362\uff0c\u5982\u5355\u6570\u4e0e\u590d\u6570\u4e92\u8f6c\uff0c\u9a7c\u5cf0\u547d\u540d\u4e0e\u4e0b\u5212\u7ebf\u547d\u540d\u4e92\u8f6c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from inflector import Inflector, English<\/p>\n<p>inflector = Inflector(English())<\/p>\n<p>var_name = &quot;person&quot;<\/p>\n<p>for i in range(1, 3):<\/p>\n<p>    plural_name = inflector.pluralize(f&quot;{var_name}{i}&quot;)<\/p>\n<p>    print(plural_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5f15\u5165Inflector\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u590d\u6742\u7684\u547d\u540d\u89c4\u5219\uff0c\u4f7f\u547d\u540d\u66f4\u52a0\u6807\u51c6\u5316\u3002<\/p>\n<\/p>\n<p><h4>\u81ea\u5b9a\u4e49\u547d\u540d\u89c4\u5219<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7279\u5b9a\u9879\u76ee\u7684\u7279\u5b9a\u9700\u6c42\uff0c\u53ef\u4ee5\u5728\u73b0\u6709\u5e93\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u4e8c\u6b21\u5f00\u53d1\uff0c\u6216\u8005\u5b8c\u5168\u81ea\u5b9a\u4e49\u4e00\u5957\u547d\u540d\u89c4\u5219\uff0c\u4ee5\u6ee1\u8db3\u590d\u6742\u7684\u547d\u540d\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff0c\u63a5\u53d7\u51e0\u4e2a\u53c2\u6570\u4f5c\u4e3a\u547d\u540d\u89c4\u5219\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u901a\u8fc7\u903b\u8f91\u5224\u65ad\u548c\u5b57\u7b26\u4e32\u64cd\u4f5c\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u547d\u540d\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_naming(prefix, index, suffix=&quot;&quot;):<\/p>\n<p>    return f&quot;{prefix}{index:02d}{suffix}&quot;<\/p>\n<p>for i in range(1, 11):<\/p>\n<p>    var_name = custom_naming(&quot;item&quot;, i, &quot;_obj&quot;)<\/p>\n<p>    print(var_name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u7684\u65b9\u6cd5\u867d\u7136\u9700\u8981\u66f4\u591a\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u4f46\u5374\u80fd\u63d0\u4f9b\u6781\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u9488\u5bf9\u6027\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u548c\u591a\u6837\u5316\u7684\u547d\u540d\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5faa\u73af\u7ed3\u6784\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4ee5\u53ca\u5f15\u5165\u6216\u81ea\u5b9a\u4e49\u547d\u540d\u89c4\u5219\u5e93\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u5feb\u901f\u3001\u6709\u89c4\u5f8b\u7684\u547d\u540d\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u547d\u540d\u4efb\u52a1\u7684\u590d\u6742\u7a0b\u5ea6\u548c\u89c4\u8303\u8981\u6c42\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u5728\u53ef\u8bfb\u6027\u4e0e\u6548\u7387\u4e4b\u95f4\u627e\u5230\u5e73\u8861\uff0c\u662f\u6210\u529f\u5b9e\u73b0\u89c4\u8303\u5316\u547d\u540d\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528Python\u5feb\u901f\u751f\u6210\u6709\u89c4\u5f8b\u7684\u547d\u540d\uff1f<\/strong><\/p>\n<p>\u751f\u6210\u6709\u89c4\u5f8b\u7684\u547d\u540d\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u3001\u683c\u5f0f\u5316\u548c\u5207\u7247\u7b49\u529f\u80fd\u6765\u751f\u6210\u6709\u89c4\u5f8b\u7684\u547d\u540d\u3002\u4f8b\u5982\uff1a\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u5b57\u7b26\u4e32\u62fc\u63a5\u6765\u751f\u6210\u4e00\u7cfb\u5217\u7684\u547d\u540d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\u6765\u81ea\u52a8\u751f\u6210\u5e26\u6709\u5e8f\u53f7\u7684\u547d\u540d\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5207\u7247\u529f\u80fd\u6765\u622a\u53d6\u9700\u8981\u7684\u90e8\u5206\u4f5c\u4e3a\u547d\u540d\u3002<\/p>\n<p><strong>2. \u6709\u54ea\u4e9b\u5b9e\u7528\u7684\u547d\u540d\u89c4\u5219\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u6709\u5f88\u591a\u5b9e\u7528\u7684\u547d\u540d\u89c4\u5219\u53ef\u4ee5\u5e2e\u52a9\u4f60\u521b\u5efa\u6709\u610f\u4e49\u4e14\u6613\u8bfb\u7684\u547d\u540d\u3002\u4e00\u4e9b\u5e38\u89c1\u7684\u547d\u540d\u89c4\u5219\u5305\u62ec\uff1a\u9a7c\u5cf0\u547d\u540d\u6cd5\uff08CamelCase\uff09\u3001\u4e0b\u5212\u7ebf\u547d\u540d\u6cd5\uff08snake_case\uff09\u548c\u5e15\u65af\u5361\u547d\u540d\u6cd5\uff08PascalCase\uff09\u7b49\u3002\u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u548c\u4e2a\u4eba\u559c\u597d\uff0c\u9009\u62e9\u9002\u5408\u7684\u547d\u540d\u89c4\u5219\u6765\u547d\u540d\u4f60\u7684\u53d8\u91cf\u3001\u51fd\u6570\u548c\u7c7b\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u907f\u514d\u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u6df7\u4e71\u95ee\u9898\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u547d\u540d\u51b2\u7a81\u548c\u547d\u540d\u6df7\u4e71\u95ee\u9898\uff0c\u53ef\u4ee5\u91c7\u53d6\u4e00\u4e9b\u6709\u6548\u7684\u547d\u540d\u7b56\u7565\u3002\u9996\u5148\uff0c\u5c3d\u91cf\u9009\u62e9\u5177\u6709\u63cf\u8ff0\u6027\u7684\u540d\u79f0\uff0c\u8ba9\u53d8\u91cf\u3001\u51fd\u6570\u548c\u7c7b\u7684\u7528\u9014\u4e00\u76ee\u4e86\u7136\u3002\u5176\u6b21\uff0c\u907f\u514d\u4f7f\u7528\u5355\u4e2a\u5b57\u7b26\u6216\u8005\u7b80\u5355\u7684\u6570\u5b57\u4f5c\u4e3a\u547d\u540d\uff0c\u5c3d\u91cf\u4f7f\u7528\u6709\u610f\u4e49\u7684\u5355\u8bcd\u6216\u77ed\u8bed\u3002\u6700\u540e\uff0c\u5728\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u5bf9\u547d\u540d\u89c4\u8303\u8fdb\u884c\u89c4\u5212\u548c\u7ea6\u5b9a\uff0c\u786e\u4fdd\u6574\u4e2a\u9879\u76ee\u4f7f\u7528\u4e00\u81f4\u7684\u547d\u540d\u89c4\u5219\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u5feb\u901f\u3001\u89c4\u8303\u7684\u547d\u540d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b8c\u6210\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u3001\u5229\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4ee5\u53ca\u5f15\u5165\u547d\u540d\u89c4 [&hellip;]","protected":false},"author":3,"featured_media":174351,"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\/174338"}],"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=174338"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/174338\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/174351"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=174338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=174338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=174338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}