莫尔斯码到英文python3 [英] morse code to english python3

查看:325
本文介绍了莫尔斯码到英文python3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python 3+
将Morse Code转换成英文我已经设法使用这个
将一个英语转换成莫尔斯码(a
http://code.activestate.com/recipes/578407-simple-morse-code-translator-in-python/



但是,我想将莫氏代码转换为英文



我试图做一个charecter一个时间,但问题是莫尔斯码字母不是一个长号的英文字母,所以E是。而S是...,我遇到的问题是字典循环会找到。并将其与E进行匹配,所以不是得到S我得到EEE
i试图通过检测空格并一次做一个单词来解决这个问题,而不是寻找单词中的字母搜索整个单词对字典
我是Python和字典的新手,我不知道如何区分一个E。和一个S...搜索我的字典



这是我的代码

 code>#定义将莫尔斯转换成英文的字典
CODE_reversed = {'..-。':'F','-..-':'X',
' - 。':'P',' - ':'T','..---':'2',
'....-':'4' ':'0','--...':'7',
'...-':'V','-.-。':'C' ''''''''''''''''''''''''''''''' '':'我',
'.- ..':'L','.....':'5','...--':'3',' - 。 - ':'Y',
'-....':'6','.--':'W','....':'H',' - 。 ''''''''''''''''''''''''''''''''' ',' - ..':'D','--.-':'Q',
' - 。':'G' ' - ':'M','..-':'U','.-':'A','...':'S','。


def main():
#take用户消息,并使其大写
msg = input('MESSAGE:')
msg = msg .upper()

addtolist =#创建空字符串
message = []#创建空白列表
为我在msg中:
addtolist = addtolist + i #将MSG中的每个字母添加到字符串中,直到找到一个空格
addtolist.upper()#使每个字母大写

如果i ==:#如果它检测到一个空格
message.extend(addtolist)
#将以前创建的addtolist添加到列表中,这使得一个单词
addtolist =#清除以前的变量

消息:
#对于列表中的每个单词
str(i)#使其成为一个字符串
print(CODE_reversed [i()])#搜索该​​字词的字典


如果__name__ ==__main__:
main()

此代码需要一个字,并试图查找与字典,但它不工作
i也尝试搜索每个单独的莫尔斯密码字典对字典,但问题是,你不能知道什么时候开始和结束,所以...出来是EEE而不是S,我不知道如何修复这个



我也试图搜索解决方案,只在Java中找到它们,我不知道Java

解决方案

一旦你在一个方向定义了映射,你可以使用字体理解以另一种方式映射

  CODE = {'A':'.-','B':' - ...','C':'-.-',
'D':' - ..','E':',','F':'..- b $ b'G':' - 。','H':'....','I':'..',
'J':'.---','K ':'-.-', ''','''',
'M':' - ','N':' - 。','O':'---',
' :'','''''''''''''''''''''''''''' ,'U':'..-',
'V':'...-','W':'.--','X':'-..-',
'Y':'-.--','Z':' - ..',

'0':'-----','1' ---','2':'..---',
'3':'...--','4':'....-','5' .....',
'6':'-....','7':'--...','8':'--- ..',
'9':'----。'
}

CODE_REVERSED = {value:key for key,value in CODE.items()}

然后,您可以使用 join 与生成器表达式执行翻译。

  def to_morse(s):
return''.join(CODE.get(i.upper())for i在s)

def from_morse(s):
return''.join(CODE_REVERSED.get(i)for s in s.split())

>> ; to_morse('hello')
'.... .- .. ..- .. ---'
>>> from_morse('....。..- .. ..- .. ---')
'HELLO'


i want to convert Morse Code to English using Python 3+ I have managed to convert english to morse code using this http://code.activestate.com/recipes/578407-simple-morse-code-translator-in-python/

But i want to convert Morse Code to English

I have attempted to do it one charecter at a time, but the problem is that morse code letters are not 1 charecter long like english letters, so E is "." and S is "...", the problem i have is that the dictionary loop will find the "." and match it to E, so instead of getting S i get "E E E" i tried to fix this by detecting spaces and doing it a word at a time, but instead of looking for the letters in the word it searches the entire word against the dictionary i'm new to Python and dictionaries and i don't know how to differeniate between an E "." and an S "..." when searching my dictionary

Here is my code

# defines the dictionary to convert morse to english
CODE_reversed = {'..-.': 'F', '-..-': 'X',
                 '.--.': 'P', '-': 'T', '..---': '2',
                 '....-': '4', '-----': '0', '--...': '7',
                 '...-': 'V', '-.-.': 'C', '.': 'E', '.---': 'J',
                 '---': 'O', '-.-': 'K', '----.': '9', '..': 'I',
                 '.-..': 'L', '.....': '5', '...--': '3', '-.--': 'Y',
                 '-....': '6', '.--': 'W', '....': 'H', '-.': 'N', '.-.': 'R',
                 '-...': 'B', '---..': '8', '--..': 'Z', '-..': 'D', '--.-': 'Q',
                 '--.': 'G', '--': 'M', '..-': 'U', '.-': 'A', '...': 'S', '.----': '1'}


    def main():
        #takes user message and makes it upper case
        msg = input('MESSAGE: ')
        msg = msg.upper()

    addtolist = "" # creates blank string
    message = [] # creates blank list
    for i in msg:
        addtolist = addtolist + i # adds every letter in MSG into a string until it finds a space
        addtolist.upper() # makes every letter uppercase

        if i == " ": # if it detects a space
            message.extend(addtolist)
            # adds the previously created addtolist to a list, this makes one word
            addtolist = "" # clears previous variable

    for i in message:
        # for every word in the list
        str(i) # make it into a string
    print(CODE_reversed[i()]) # search the dictionary for that word


if __name__ == "__main__":
    main()

This code takes a word, and tries to look it up against the dictionary, but it doesn't work i have also tried searching each individual morse code letter against the dictionary but the problem is that you can't tell when a letter starts and ends, so "..." comes out as "EEE" instead of "S" and i don't know how to fix this

i have also tried searching for solutions but have only found them in Java and i do not know Java

解决方案

Once you define the mapping in one direction, you can use a dict comprehension to map it the other way

CODE = {'A': '.-',     'B': '-...',   'C': '-.-.', 
        'D': '-..',    'E': '.',      'F': '..-.',
        'G': '--.',    'H': '....',   'I': '..',
        'J': '.---',   'K': '-.-',    'L': '.-..',
        'M': '--',     'N': '-.',     'O': '---',
        'P': '.--.',   'Q': '--.-',   'R': '.-.',
        'S': '...',    'T': '-',      'U': '..-',
        'V': '...-',   'W': '.--',    'X': '-..-',
        'Y': '-.--',   'Z': '--..',

        '0': '-----',  '1': '.----',  '2': '..---',
        '3': '...--',  '4': '....-',  '5': '.....',
        '6': '-....',  '7': '--...',  '8': '---..',
        '9': '----.' 
        }

CODE_REVERSED = {value:key for key,value in CODE.items()}

Then you can use join with a generator expression to perform the translations.

def to_morse(s):
    return ' '.join(CODE.get(i.upper()) for i in s)

def from_morse(s):
    return ''.join(CODE_REVERSED.get(i) for i in s.split())

>>> to_morse('hello')
'.... . .-.. .-.. ---'
>>> from_morse('.... . .-.. .-.. ---')
'HELLO'

这篇关于莫尔斯码到英文python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆