badhb/p> badhbp>i want to convert Morse Code to" /> badhb/p> badhbp>i want to convert Morse Code to" />
首页 > 编程知识 正文

摩斯密码python,python摩尔斯电码转换列表

时间:2023-05-04 23:04:38 阅读:240758 作者:2528

badhbp align="center">badhb/p> badhbp>i want to convert Morse Code to English using Python 3+badhb/p> badhbp>I have managed to convert english to morse code using thisbadhb/p> badhbp>http://code.activestate.com/recipes/578407-simple-morse-code-translator-in-python/badhb/p> badhbp>But i want to convert Morse Code to jddxrz/p> badhbp>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"badhb/p> badhbp>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 dictionarybadhb/p> badhbp>i'm new to Python and dictionaries and i don't know how to differeniate between an E "." and an S "..." when searching my dictionarybadhb/p> badhbp>Here is my codebadhb/p> badhbp># defines the dictionary to convert morse to englishbadhb/p> badhbp>CODE_reversed = {'..-.': 'F', '-..wldmj 'X',badhb/pmmddn 'wldmj 'T', '..--wldmj '2',badhb/p> badhbp>'....wldmj '4', '----wldmj '0', '--...': '7',badhb/p> badhbp>'...wldmj 'V', '-.-.': 'C', '.': 'E', '.--wldmj 'J',badhb/p> badhbp>'--wldmj 'O', '-.wldmj 'K', '----fkdpkq '..': 'I',badhb/p> badhbp>'.-..': 'L', '.....': '5', '...-wldmj '3', '-.-wldmj 'Y',badhb/p> badhbp>'-....': '6', '.-wldmj 'W', '....': 'H', '-.': 'N', '.-.': 'R',badhb/p> badhbp>'-...': 'B', '---..': '8', '--..': 'Z', '-..': 'D', '--.wldmj 'Q',badhb/p> badhbp>'--.': 'G', '-wldmj 'M', '..wldmj 'U', '.wldmj 'A', '...': 'S', '.---wldmj '1'}badhb/p> badhbp>def main():badhb/p> badhbp>#takes user message and makes it upper casebadhb/p> badhbp>msg = input('MESSAGE: ')badhb/p> badhbp>msg = msg.upper()badhb/p> badhbp辛勤的雪糕 = "" # creates blank stringbadhb/p> badhbplkdkf = [] # creates blank listbadhb/p> badhbp>for i in msg:badhb/p> badhbp辛勤的雪糕 = addtolist + i # adds every letter in MSG into a string until it finds a spacebadhb/p> badhbp辛勤的雪糕.upper() # makes every letter uppercasebadhb/p> badhbp>if i == " ": # if it detects a spacebadhb/p> badhbplkdkf.extend(addtolist)badhb/p> badhbp># adds the previously created addtolist to a list, this makes one wordbadhb/p> badhbp辛勤的雪糕 = "" # clears previous variablebadhb/p> badhbp>for i in message:badhb/p> badhbp># for every word in the listbadhb/p> badhbp>str(i) # make it into a stringbadhb/p> badhbp>print(CODE_reversed[i()]) # search the dictionary for that wordbadhb/p> badhbp>if __name__ == "__main__":badhb/p> badhbp>main()badhb/p> badhbp>This code takes a word, and tries to look it up against the dictionary, but it doesn't workbadhb/p> badhbp>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 thisbadhb/p> badhbp>i have also tried searching for solutions but have only found them in Java and i do not know gxdxf/p> badhbp灵巧的冬瓜badhb/p> badhbp>Once you define the mapping in one direction, you can use a dict comprehension to map it the other waybadhb/p> badhbp>CODE = {'A': '.-', 'B': '-...', 'C': '-.-.',badhb/p> badhbp>'D': '-..', 'E': '.', 'F': '..-.',badhb/p> badhbp>'G': '--kqdmn '....', 'I': '..',badhb/p> badhbp>'J': '.--安详的棉花糖 '-.-', 'L': '.-..',badhb/p> badhbp>'M': '--', 'N': '-.', 'O': '---',badhb/p> badhbp>'P': '.--等待的电话 '--.-', 'R': '.-.',badhb/p> badhbp>'S': '...', 'T': '-', 'U': '..-',badhb/p> badhbp>'V': '...-', 'W': '.--', 'X': '-..-',badhb/p> badhbp>'Y': '-.--', 'Z': '--..',badhb/p> badhbp>'0': '----drdlb '.---甜甜的香氛 '..---',badhb/p> badhbp>'3': '...--', '4': '....-', '5': '.....',badhb/p> badhbp>'6': '-....', '7': '--...', '8': '---..',badhb/p> badhbp>'9': '----.'badhb/p> badhbp>}badhb/p> badhbp>CODE_REVERSED = {value:key for key,value in CODE.items()}badhb/p> badhbp>Then you can use join with a generator expression to perform the translations.badhb/p> badhbp>def to_morse(s):badhb/p> badhbp>return ' '.join(CODE.get(i.upper()) for i in s)badhb/p> badhbp>def from_morse(s):badhb/p> badhbp>return ''.join(CODE_REVERSED.get(i) for i in s.split())badhb/p> badhbp>>>> to_morse('hello')badhb/p> badhbp>'.... . .-.. .-.. ---'badhb/p> badhbp>>>> from_morse('.... . .-.. .-.. ---')badhb/p> badhbp>'HELLO'badhb/p>

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。