{"id":976523,"date":"2024-12-27T06:22:18","date_gmt":"2024-12-26T22:22:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/976523.html"},"modified":"2024-12-27T06:22:20","modified_gmt":"2024-12-26T22:22:20","slug":"python%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99%e7%94%b5%e6%8a%a5%e8%a7%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/976523.html","title":{"rendered":"python\u5982\u4f55\u7f16\u5199\u7535\u62a5\u89e3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202204\/13403003-4623-40f7-bc16-c32cf77b0bea.webp\" alt=\"python\u5982\u4f55\u7f16\u5199\u7535\u62a5\u89e3\u7801\" \/><\/p>\n<p><p> <strong>\u8981\u7f16\u5199\u4e00\u4e2a\u7528\u4e8e\u7535\u62a5\u89e3\u7801\u7684Python\u7a0b\u5e8f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u89e3\u6790\u7535\u62a5\u683c\u5f0f\u3001\u8f6c\u6362\u7f16\u7801\u3001\u5904\u7406\u6570\u636e\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u4e86\u89e3\u7535\u62a5\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u5982Morse\u7801\u6216\u5176\u4ed6\u683c\u5f0f\uff0c\u7136\u540e\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u89e3\u6790\u8fd9\u4e9b\u7f16\u7801\u683c\u5f0f\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u7f16\u5199\u4ee3\u7801\u6765\u5c06\u8fd9\u4e9b\u7f16\u7801\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u6587\u672c\u683c\u5f0f\u3002\u6700\u540e\uff0c\u5904\u7406\u548c\u8f93\u51fa\u89e3\u7801\u540e\u7684\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u89e3\u6790\u7535\u62a5\u683c\u5f0f<\/p>\n<\/p>\n<p><p>\u7535\u62a5\u901a\u5e38\u4f7f\u7528\u7279\u5b9a\u7684\u7f16\u7801\u683c\u5f0f\u8fdb\u884c\u4fe1\u606f\u4f20\u8f93\uff0c\u6700\u5e38\u89c1\u7684\u683c\u5f0f\u4e4b\u4e00\u662f\u6469\u5c14\u65af\u7535\u7801\u3002\u6469\u5c14\u65af\u7535\u7801\u4f7f\u7528\u70b9\uff08.\uff09\u548c\u5212\uff08-\uff09\u8868\u793a\u5b57\u6bcd\u548c\u6570\u5b57\u3002\u4e3a\u4e86\u7f16\u5199\u4e00\u4e2a\u89e3\u7801\u7a0b\u5e8f\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u6469\u5c14\u65af\u7535\u7801\u5230\u5b57\u6bcd\u7684\u6620\u5c04\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6469\u5c14\u65af\u7535\u7801\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u89e3\u7801\u6469\u5c14\u65af\u7535\u7801\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6469\u5c14\u65af\u7535\u7801\u8868\u3002\u8fd9\u4e2a\u8868\u5c06\u6469\u5c14\u65af\u7b26\u53f7\u6620\u5c04\u5230\u5b57\u6bcd\u548c\u6570\u5b57\u3002\u4f8b\u5982\uff0c\u5b57\u6bcd\u201cA\u201d\u5728\u6469\u5c14\u65af\u7535\u7801\u4e2d\u8868\u793a\u4e3a\u201c.-\u201d\uff0c\u800c\u6570\u5b57\u201c1\u201d\u8868\u793a\u4e3a\u201c.&#8212;-\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u89e3\u6790\u8f93\u5165\u7535\u62a5<\/strong><\/p>\n<\/p>\n<p><p>\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u89e3\u6790\u8f93\u5165\u7684\u7535\u62a5\u5b57\u7b26\u4e32\u3002\u7535\u62a5\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bcd\u6216\u6570\u5b57\u901a\u5e38\u7531\u7a7a\u683c\u5206\u9694\uff0c\u800c\u5355\u8bcd\u4e4b\u95f4\u53ef\u80fd\u6709\u591a\u4e2a\u7a7a\u683c\u3002\u60a8\u9700\u8981\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u6309\u7a7a\u683c\u62c6\u5206\u4e3a\u6469\u5c14\u65af\u7535\u7801\u7b26\u53f7\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u7279\u6b8a\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u6807\u51c6\u7684\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u7535\u62a5\u4e2d\u8fd8\u53ef\u80fd\u5305\u542b\u7279\u6b8a\u683c\u5f0f\u6216\u7b26\u53f7\uff0c\u5982\u6807\u70b9\u7b26\u53f7\u3002\u786e\u4fdd\u60a8\u7684\u89e3\u7801\u8868\u4e2d\u5305\u542b\u8fd9\u4e9b\u7b26\u53f7\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u8f6c\u6362\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u4e00\u65e6\u60a8\u89e3\u6790\u4e86\u7535\u62a5\u7684\u683c\u5f0f\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u5c06\u8fd9\u4e9b\u7f16\u7801\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u89e3\u7801\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u7f16\u5199\u4e00\u4e2a\u89e3\u7801\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u6469\u5c14\u65af\u7535\u7801\u7b26\u53f7\uff0c\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u5b57\u6bcd\u6216\u6570\u5b57\u3002\u8fd9\u4e2a\u51fd\u6570\u5c06\u4f7f\u7528\u60a8\u4e4b\u524d\u521b\u5efa\u7684\u6469\u5c14\u65af\u7535\u7801\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u904d\u5386\u548c\u8f6c\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u904d\u5386\u89e3\u6790\u540e\u7684\u6469\u5c14\u65af\u7535\u7801\u7b26\u53f7\uff0c\u4f7f\u7528\u89e3\u7801\u51fd\u6570\u5c06\u6bcf\u4e2a\u7b26\u53f7\u8f6c\u6362\u4e3a\u76f8\u5e94\u7684\u5b57\u6bcd\u6216\u6570\u5b57\u3002\u5c06\u8fd9\u4e9b\u5b57\u6bcd\u6216\u6570\u5b57\u62fc\u63a5\u6210\u5b8c\u6574\u7684\u5355\u8bcd\u548c\u53e5\u5b50\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u7a7a\u683c\u548c\u5206\u9694\u7b26<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u89e3\u7801\u8fc7\u7a0b\u4e2d\uff0c\u6ce8\u610f\u5904\u7406\u5355\u8bcd\u4e4b\u95f4\u7684\u7a7a\u683c\u548c\u5206\u9694\u7b26\u3002\u786e\u4fdd\u5728\u8f93\u51fa\u7684\u6587\u672c\u4e2d\u4fdd\u7559\u6b63\u786e\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5904\u7406\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u8f6c\u6362\u7f16\u7801\u540e\uff0c\u60a8\u9700\u8981\u5bf9\u89e3\u7801\u540e\u7684\u6570\u636e\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u8f93\u51fa\u7684\u6b63\u786e\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9a8c\u8bc1\u89e3\u7801\u7ed3\u679c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8f93\u51fa\u89e3\u7801\u7ed3\u679c\u4e4b\u524d\uff0c\u9a8c\u8bc1\u89e3\u7801\u540e\u7684\u6587\u672c\u662f\u5426\u7b26\u5408\u9884\u671f\u3002\u5982\u679c\u6709\u4efb\u4f55\u4e0d\u5339\u914d\u6216\u9519\u8bef\uff0c\u68c0\u67e5\u8f93\u5165\u6570\u636e\u548c\u89e3\u7801\u8868\u662f\u5426\u6b63\u786e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u89e3\u7801\u540e\u7684\u6587\u672c\uff0c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002\u8fd9\u53ef\u80fd\u5305\u62ec\u6dfb\u52a0\u6362\u884c\u7b26\u3001\u7f29\u8fdb\u6216\u5176\u4ed6\u683c\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u89e3\u7801\u540e\u7684\u6587\u672c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u6216\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u67e5\u770b\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u793a\u4f8b\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u4ee3\u7801\u793a\u4f8b\uff0c\u7528\u4e8e\u89e3\u7801\u6469\u5c14\u65af\u7535\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6469\u5c14\u65af\u7535\u7801\u5230\u5b57\u6bcd\u7684\u6620\u5c04\u8868<\/p>\n<p>MORSE_CODE_DICT = {<\/p>\n<p>    &#39;.-&#39;: &#39;A&#39;, &#39;-...&#39;: &#39;B&#39;, &#39;-.-.&#39;: &#39;C&#39;, &#39;-..&#39;: &#39;D&#39;, &#39;.&#39;: &#39;E&#39;,<\/p>\n<p>    &#39;..-.&#39;: &#39;F&#39;, &#39;--.&#39;: &#39;G&#39;, &#39;....&#39;: &#39;H&#39;, &#39;..&#39;: &#39;I&#39;, &#39;.---&#39;: &#39;J&#39;,<\/p>\n<p>    &#39;-.-&#39;: &#39;K&#39;, &#39;.-..&#39;: &#39;L&#39;, &#39;--&#39;: &#39;M&#39;, &#39;-.&#39;: &#39;N&#39;, &#39;---&#39;: &#39;O&#39;,<\/p>\n<p>    &#39;.--.&#39;: &#39;P&#39;, &#39;--.-&#39;: &#39;Q&#39;, &#39;.-.&#39;: &#39;R&#39;, &#39;...&#39;: &#39;S&#39;, &#39;-&#39;: &#39;T&#39;,<\/p>\n<p>    &#39;..-&#39;: &#39;U&#39;, &#39;...-&#39;: &#39;V&#39;, &#39;.--&#39;: &#39;W&#39;, &#39;-..-&#39;: &#39;X&#39;, &#39;-.--&#39;: &#39;Y&#39;,<\/p>\n<p>    &#39;--..&#39;: &#39;Z&#39;, &#39;-----&#39;: &#39;0&#39;, &#39;.----&#39;: &#39;1&#39;, &#39;..---&#39;: &#39;2&#39;, &#39;...--&#39;: &#39;3&#39;,<\/p>\n<p>    &#39;....-&#39;: &#39;4&#39;, &#39;.....&#39;: &#39;5&#39;, &#39;-....&#39;: &#39;6&#39;, &#39;--...&#39;: &#39;7&#39;, &#39;---..&#39;: &#39;8&#39;,<\/p>\n<p>    &#39;----.&#39;: &#39;9&#39;<\/p>\n<p>}<\/p>\n<p>def decode_morse(morse_code):<\/p>\n<p>    # \u6309\u7a7a\u683c\u62c6\u5206\u6469\u5c14\u65af\u7535\u7801<\/p>\n<p>    words = morse_code.strip().split(&#39;   &#39;)  # \u5355\u8bcd\u4e4b\u95f4\u75283\u4e2a\u7a7a\u683c\u5206\u9694<\/p>\n<p>    decoded_message = []<\/p>\n<p>    for word in words:<\/p>\n<p>        letters = word.split(&#39; &#39;)<\/p>\n<p>        decoded_word = &#39;&#39;.join(MORSE_CODE_DICT.get(letter, &#39;&#39;) for letter in letters)<\/p>\n<p>        decoded_message.append(decoded_word)<\/p>\n<p>    return &#39; &#39;.join(decoded_message)<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>morse_input = &quot;... --- ...&quot;<\/p>\n<p>decoded_output = decode_morse(morse_input)<\/p>\n<p>print(f&quot;Decoded Message: {decoded_output}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u6469\u5c14\u65af\u7535\u7801\u8868\u89e3\u7801\u7b80\u5355\u7684\u6469\u5c14\u65af\u7535\u7801\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u9002\u5f53\u7684\u8c03\u6574\u548c\u6269\u5c55\uff0c\u60a8\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u7535\u62a5\u683c\u5f0f\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u7535\u62a5\u89e3\u7801\u7684\u57fa\u672c\u6982\u5ff5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7535\u62a5\u89e3\u7801\u662f\u6307\u5c06\u7535\u62a5\u4e2d\u4f7f\u7528\u7684\u7f16\u7801\u4fe1\u606f\u8f6c\u5316\u4e3a\u53ef\u8bfb\u6587\u672c\u7684\u8fc7\u7a0b\u3002\u7535\u62a5\u901a\u5e38\u4f7f\u7528\u7279\u5b9a\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u5982\u6469\u65af\u7535\u7801\u6216\u5176\u4ed6\u7b80\u5316\u7b26\u53f7\uff0c\u89e3\u7801\u7684\u76ee\u7684\u662f\u5c06\u8fd9\u4e9b\u7b26\u53f7\u7ffb\u8bd1\u6210\u6211\u4eec\u53ef\u4ee5\u7406\u89e3\u7684\u8bed\u8a00\u3002\u4f7f\u7528Python\u7f16\u5199\u7535\u62a5\u89e3\u7801\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u5904\u7406\u548c\u5b57\u5178\u6620\u5c04\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u5904\u7406\u7535\u62a5\u89e3\u7801\u4e2d\u7684\u6469\u65af\u7535\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5c06\u6469\u65af\u7535\u7801\u4e0e\u5bf9\u5e94\u7684\u5b57\u6bcd\u6216\u6570\u5b57\u8fdb\u884c\u6620\u5c04\u3002\u901a\u8fc7\u904d\u5386\u8f93\u5165\u7684\u7535\u62a5\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u8be5\u5b57\u5178\u5c06\u6469\u65af\u7535\u7801\u8f6c\u6362\u4e3a\u53ef\u8bfb\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u6469\u65af\u7535\u7801\u7684\u5b57\u5178\uff0c\u7136\u540e\u89e3\u6790\u8f93\u5165\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u6469\u65af\u7535\u7801\uff0c\u8f93\u51fa\u76f8\u5e94\u7684\u5b57\u7b26\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316\u7535\u62a5\u89e3\u7801\u7684\u8fc7\u7a0b\uff1f<\/strong><br \/>Python\u6709\u4e00\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316\u7535\u62a5\u89e3\u7801\u7684\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c<code>pymorse<\/code>\u5e93\u53ef\u4ee5\u7528\u6765\u5904\u7406\u6469\u65af\u7535\u7801\u7684\u7f16\u89e3\u7801\u3002\u6b64\u5916\uff0c<code>numpy<\/code>\u548c<code>pandas<\/code>\u7b49\u6570\u636e\u5904\u7406\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u7535\u62a5\u89e3\u7801\u5e94\u7528\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u548c\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u65f6\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u73b0\u6210\u7684\u51fd\u6570\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u51cf\u5c11\u5f00\u53d1\u65f6\u95f4\u548c\u590d\u6742\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7f16\u5199\u4e00\u4e2a\u7528\u4e8e\u7535\u62a5\u89e3\u7801\u7684Python\u7a0b\u5e8f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u89e3\u6790\u7535\u62a5\u683c\u5f0f\u3001\u8f6c\u6362\u7f16\u7801\u3001\u5904\u7406\u6570\u636e\u3002\u9996\u5148\uff0c\u60a8\u9700 [&hellip;]","protected":false},"author":3,"featured_media":976532,"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\/976523"}],"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=976523"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/976523\/revisions"}],"predecessor-version":[{"id":976537,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/976523\/revisions\/976537"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/976532"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=976523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=976523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=976523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}