{"id":3544,"date":"2025-05-30T14:56:13","date_gmt":"2025-06-04T07:21:12","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3544"},"modified":"2025-06-04T14:56:13","modified_gmt":"2025-06-04T07:21:12","slug":"%e5%ad%b8%e7%bf%92python%e4%b8%ad%e7%9a%84translate%e5%87%bd%e6%95%b8%ef%bc%9a%e8%ae%93%e4%bd%a0%e7%9a%84%e7%a8%8b%e5%bc%8f%e6%9b%b4%e5%8a%a0%e9%9d%88%e6%b4%bb","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e5%ad%b8%e7%bf%92python%e4%b8%ad%e7%9a%84translate%e5%87%bd%e6%95%b8%ef%bc%9a%e8%ae%93%e4%bd%a0%e7%9a%84%e7%a8%8b%e5%bc%8f%e6%9b%b4%e5%8a%a0%e9%9d%88%e6%b4%bb\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `translate()` \u51fd\u6578\uff1a\u63d0\u5347\u5b57\u4e32\u8655\u7406\u7684\u9748\u6d3b\u6027\u8207\u6548\u7387"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, translate(), \u5b57\u4e32\u8655\u7406, Python \u6559\u5b78\"><\/p>\n<h1>Python \u7684 `translate()` \u51fd\u6578<\/h1>\n<p>`translate()` \u51fd\u6578\u662f Python \u4e2d\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5b57\u4e32\u8655\u7406\u5de5\u5177\uff0c\u80fd\u5920\u5c0d\u5b57\u4e32\u4e2d\u7684\u6bcf\u500b\u5b57\u5143\u9032\u884c\u8f49\u63db\u3001\u66ff\u63db\u6216\u522a\u9664\u3002\u9019\u4f7f\u5f97\u5b83\u5728\u5b57\u4e32\u8655\u7406\u6642\u8b8a\u5f97\u66f4\u52a0\u9748\u6d3b\u8207\u9ad8\u6548\u3002\u4ee5\u4e0b\u5c07\u4ecb\u7d39\u5176\u6700\u65b0\u8a9e\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u4f60\u5728 Python \u7de8\u7a0b\u4e2d\u66f4\u597d\u5730\u904b\u7528\u9019\u500b\u51fd\u6578\u3002<\/p>\n<p>## `translate()` \u51fd\u6578\u7684\u8a9e\u6cd5<\/p>\n<p>`translate()` \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nstr.translate(table[, deletechars])<br \/>\n&#8220;`<\/p>\n<p>&#8211; **table**\uff1a\u9019\u662f\u4e00\u500b\u6620\u5c04\u8868\uff0c\u901a\u5e38\u662f\u7531 `str.maketrans()` \u51fd\u6578\u751f\u6210\u7684\u5b57\u5178\uff0c\u7528\u4f86\u5c07\u5b57\u4e32\u4e2d\u7684\u6bcf\u500b\u5b57\u5143\u6620\u5c04\u5230\u53e6\u4e00\u500b\u5b57\u5143\u3002<br \/>\n&#8211; **deletechars**\uff08\u53ef\u9078\uff09\uff1a\u9019\u662f\u4e00\u500b\u5b57\u4e32\uff0c\u6307\u5b9a\u8981\u5f9e\u5b57\u4e32\u4e2d\u522a\u9664\u7684\u5b57\u5143\u3002<\/p>\n<p>## \u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>### 1. \u5b57\u5143\u66ff\u63db<\/p>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u5b57\u4e32\uff1a<\/p>\n<p>&#8220;`python<br \/>\ns = &#8216;Hello World!&#8217;<br \/>\n&#8220;`<\/p>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `translate()` \u51fd\u6578\u5c07\u5b57\u4e32\u4e2d\u7684\u67d0\u4e9b\u5b57\u5143\u66ff\u63db\u70ba\u5176\u4ed6\u5b57\u5143\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5275\u5efa\u4e00\u500b\u6620\u5c04\u8868\u4ee5\u66ff\u63db\u5b57\u5143<br \/>\ntranslation_table = str.maketrans({&#8216;H&#8217;: &#8216;A&#8217;, &#8216;W&#8217;: &#8216;B&#8217;})<br \/>\ns = s.translate(translation_table)<br \/>\nprint(s)  # \u8f38\u51fa\uff1aAello Borld!<br \/>\n&#8220;`<\/p>\n<p>### 2. \u5b57\u5143\u522a\u9664<\/p>\n<p>\u9664\u4e86\u66ff\u63db\u5b57\u5143\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u522a\u9664\u5b57\u4e32\u4e2d\u7684\u67d0\u4e9b\u5b57\u5143\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u522a\u9664\u5b57\u5143 H \u548c W<br \/>\ns = s.translate(str.maketrans(&#8221;, &#8221;, &#8216;HW&#8217;))<br \/>\nprint(s)  # \u8f38\u51fa\uff1aello orld!<br \/>\n&#8220;`<\/p>\n<p>### 3. \u5b57\u5143\u6392\u5e8f<\/p>\n<p>`translate()` \u51fd\u6578\u9084\u53ef\u4ee5\u7528\u65bc\u5c0d\u5b57\u4e32\u4e2d\u7684\u5b57\u5143\u9032\u884c\u6392\u5e8f\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\ns = &#8216;Hello World!&#8217;<br \/>\ns = s.translate(str.maketrans(&#8221;, &#8221;, &#8221;.join(sorted(s))))<br \/>\nprint(s)  # \u8f38\u51fa\uff1aHWdelloor!<br \/>\n&#8220;`<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `translate()` \u51fd\u6578\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<br \/>\n&#8211; \u78ba\u4fdd\u6620\u5c04\u8868\u6b63\u78ba\u751f\u6210\uff0c\u4f7f\u7528 `str.maketrans()` \u4f86\u5275\u5efa\u6709\u6548\u7684\u6620\u5c04\u3002<br \/>\n&#8211; \u6aa2\u67e5\u662f\u5426\u6709\u4e0d\u652f\u6301\u7684\u5b57\u5143\uff0c\u7279\u5225\u662f\u5728\u522a\u9664\u5b57\u5143\u6642\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>&#8211; **\u5b57\u4e32\u6e05\u7406**\uff1a\u53ef\u4ee5\u4f7f\u7528 `translate()` \u51fd\u6578\u4f86\u6e05\u7406\u7528\u6236\u8f38\u5165\u7684\u8cc7\u6599\uff0c\u522a\u9664\u4e0d\u5fc5\u8981\u7684\u5b57\u5143\u3002<br \/>\n&#8211; **\u6587\u672c\u52a0\u5bc6**\uff1a\u900f\u904e\u81ea\u5b9a\u7fa9\u7684\u5b57\u5143\u66ff\u63db\u8868\uff0c\u53ef\u4ee5\u8f15\u6613\u5be6\u73fe\u7c21\u55ae\u7684\u6587\u672c\u52a0\u5bc6\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u7684 `translate()` \u51fd\u6578\u4e0d\u50c5\u53ef\u4ee5\u5feb\u901f\u5730\u5c0d\u5b57\u4e32\u9032\u884c\u8655\u7406\uff0c\u9084\u80fd\u63d0\u9ad8\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u548c\u6548\u7387\u3002\u7121\u8ad6\u662f\u5b57\u5143\u66ff\u63db\u3001\u522a\u9664\u9084\u662f\u6392\u5e8f\uff0c\u9019\u500b\u51fd\u6578\u90fd\u80fd\u5e6b\u52a9\u4f60\u8f15\u9b06\u5be6\u73fe\u3002<\/p>\n<p>\u5982\u679c\u60f3\u4e86\u89e3\u66f4\u591a Python \u5b57\u4e32\u8655\u7406\u7684\u6280\u5de7\uff0c\u6b61\u8fce\u67e5\u770b [\u9019\u88e1](https:\/\/vocus.cc\/article\/60a2e4a3fd89780001a2417c) \u7684\u6559\u5b78\u6587\u7ae0\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `translate()` \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u9032\u884c\u54ea\u4e9b\u64cd\u4f5c\uff1f**<br \/>\nA1: `translate()` \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u66ff\u63db\u5b57\u4e32\u4e2d\u7684\u5b57\u5143\u3001\u522a\u9664\u7279\u5b9a\u5b57\u5143\u6216\u9032\u884c\u5b57\u5143\u6392\u5e8f\u7b49\u64cd\u4f5c\u3002<\/p>\n<p>**Q2: \u5728\u4f7f\u7528 `translate()` \u51fd\u6578\u6642\uff0c\u5982\u4f55\u751f\u6210\u6620\u5c04\u8868\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `str.maketrans()` \u51fd\u6578\u4f86\u751f\u6210\u6620\u5c04\u8868\uff0c\u9019\u6a23\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u6307\u5b9a\u5b57\u5143\u7684\u66ff\u63db\u95dc\u4fc2\u3002<\/p>\n<p>**Q3: `translate()` \u51fd\u6578\u7684\u6027\u80fd\u5982\u4f55\uff1f**<br \/>\nA3: `translate()` \u51fd\u6578\u5728\u8655\u7406\u5b57\u4e32\u6642\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u56e0\u70ba\u5b83\u4e0d\u9700\u8981\u4f7f\u7528\u8ff4\u5708\u4f86\u9010\u4e00\u8655\u7406\u5b57\u5143\uff0c\u4f7f\u5f97\u5b57\u4e32\u8655\u7406\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684translate()\u51fd\u6578\u53ef\u4ee5\u8b93\u4f60\u7684\u7a0b\u5f0f\u66f4\u52a0\u9748\u6d3b\uff0c\u5b83\u53ef\u4ee5\u5c07\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u66ff\u63db\u6210\u53e6\u4e00\u500b\u5b57\u7b26\uff0c\u4e26\u4e14\u53ef\u4ee5\u6307\u5b9a\u5b57\u7b26\u7684\u522a\u9664\u3002\u672c\u6587\u5c07\u4ecb\u7d39translate()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u8b93\u4f60\u7684\u7a0b\u5f0f\u66f4\u52a0\u9748\u6d3b\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3544","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Va","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3544"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3544\/revisions"}],"predecessor-version":[{"id":3545,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3544\/revisions\/3545"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}