python中是否可以使用关联列表?

我正在制作一个莫尔斯电码翻译器,我想要一个更容易确保莫尔斯电码匹配正确的列表。

这是当前的代码:

english = [ "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",            # list all translateable english characters
            "q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6",
            "7","8","9","0",".",",",";",":","!","?","(",")","-","_","&","=",
            "+","$","/","'"," ", "", "",'"']

morse = [ ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",           # list all translateable morse code characters, in same order
          ".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",
          ".--","-..-","-.--","--..",".----","..---","...--","....-",".....",
          "-....","--...","---..","----.","-----",".-.-.-","--..--","-.-.-.",
          "---...","-.-.--","..--..","-.--.","-.--.-","-....-","..--.-",
          ".-...","-...-",".-.-.","...-..-","-..-.",".----.","/", " ", "", ".-..-."]

morselesson = '''Morse code is a telecommunications language made of dots,
dashes, spaces, and slashes. The letters are comprised of
dots and dashes. Spaces are represented with slashes, and
gaps between letters marked with spaces. '''

print("nNote: not all characters are logged. nn")
print("Enter 'code553127' for a brief lesson about morse code. n")
print('''Otherwise, type your morse code or english and it will be
translated to the other language. ''')

while True:


    tobetranslated = input("nTranslatenn> ").lower()           # get input, make all letter cases the same

    if tobetranslated == "what is morse code":
        print(morselesson)

    else:

        
        try:                                                                                #morse to english

            tobetranslated = tobetranslated.replace(" ", "?")                                   # change spaces between letters into split markers (untypeable character)
            tobetranslated = tobetranslated.replace(" / ", "?/?")                                # change morse spaces into english spaces and an untypeable character


            splitupinput = list(tobetranslated.split("?"))                                      # split along the untypeable characters, leaving spaces, and splits
            
            finishedoutput = ""                                                                 # create variable for the end output, to be modified

            
            for i in splitupinput:                      # "for every character in this string:"

                if i in splitupinput:                                                        # IF inputted character is valid:
                    morselocation = morse.index(i.strip())                                          # find location of input morse coded text in the morse code list
                    finishedoutput = finishedoutput + english[morselocation]                    # add segment of translated text to text, separation is already there

            

        except:                                                                             #english to morse
            
            tobetranslated = tobetranslated.replace("?", " ")                                   # undoing what "try" did
            tobetranslated = tobetranslated.replace("?/?", " / ")
            splitupinput = list(tobetranslated)                                                 # divide input into individual characters to be translated
            finishedoutput = ""                                                                 # create variable for the end output, to be modified
            
            for i in splitupinput:                                                  # "for every character in this string:"

                if i in english:                                                        # IF inputted character is valid:
                    
                    englishlocation = english.index(i)                                      # Find location of input character in English character list
                    finishedoutput = finishedoutput + morse[englishlocation] + " "          # Add segment of translated text to output, and add separation
                    
        print("n"+finishedoutput+"n")                       # when done with translating, display translated text

所以现在,我有 2 个列表,并且位置是匹配的,但这使得对不正确的翻译进行故障排除变得困难。所以我想创造一个[("a" = "b"), ("c" = "d")]情境,然后我可以创造一些东西,让我从“a”中得到“b”,或者从“b”中得到“a”。

我想要一个列表,而不是一个巨大的变量列表。

我现在的情况,很快就分解了:

它适用于莫尔斯电码吗?

尝试:

在空格处将句子分开(分隔字母,斜线分隔单词)

在摩尔斯电码列表中查找段,并找到位置

在英文列表中找到位置

将英文版的segment添加到输出中

除了:

如果它不起作用并且有错误:

把它切成每一个字符

在英文列表中查找字符的位置

在摩尔斯电码列表中查找翻译版本(相同位置)

将段的摩尔斯电码版本添加到输出

打印输出

我不知道这是否有任何帮助(你可以查看我过多的评论(从它是一堆糟糕的代码开始,组织它),但我想要一些帮助

我只有标准库,没有其他模块之类的(严格的父母,爸爸说他正在“考虑”)所以如果你可以使用标准库就好了

回答

你可以做类似的事情

match = dict(zip(english, morse))

在字典中将所有英文字母组合成莫尔斯


以上是python中是否可以使用关联列表?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>