匹配键时使用字典并打印出值,反之亦然 [英] Using dictionary and printing out value when matched key and vise-versa

查看:58
本文介绍了匹配键时使用字典并打印出值,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本字典,其中 key 是字母表中的字母,而 value 是其对应的摩尔斯电码字母(例如" A":.-").我也有一个用户输入,我在其中输入用户的消息.按下Enter键后,它会检查每个输入字母以查看其是否在摩尔斯电码或英文字母中,方法是查看其是否在值或键中.之后,我希望它随后打印其对应的字母(例如,如果它发现将打印".-","A").我该怎么做?

I have a dictionary where the key is a letter in the alphabet and it’s value is its corresponding Morse code letter (e.g. "A": ".-"). I also have a user input where the user I puts there message. Once they press enter, it checks each input letter to see if it is in Morse code or an English letter by seeing if it is in the value or key. After that, I want it to then print its corresponding letter (e.g. if it found that ".-", "A" would be printed). How would I do this?

到目前为止,这是我的代码:

Here is my code so far:

translation = {
"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": "--..",
" ": "  "
}

user_input = input("Input english or morse code message:\n").upper()

for i in user_input:
    if i in translation.keys(): 
        print(translation.values()) 
    if i in translation.values():
        print(translation.keys())

推荐答案

要将文本翻译为莫尔斯电码,只需将 map 字符串中的字符映射为 translation 字典:

For translating text to morse, just map the characters of the string to the corresponding value from the translation dictionary:

>>> msg = "HELLO WORLD"
>>> morse = ' '.join(map(translation.get, msg))
>>> morse
'.... . .-.. .-.. ---    .-- --- .-. .-.. -..'

请注意,我用空格分隔了代码,否则几乎不可能将消息解码回去,因为某些序列可能会产生不同的字符组合.为了进行翻译,您首先必须对字典进行逆运算.然后通过空格拆分莫尔斯电文,并从逆字典中获取其值.

Note that I separated the codes with spaces, otherwise it will be nearly impossible to decode the message back, as some sequences could yield in different combinations of characters. For the translation back, you first have to inverse the dictionary; then split the morse message by space and get the value from the inverse dictionary.

>>> trans_back = {v: k for k, v in translation.items()}
>>> ''.join(map(trans_back.get, morse.split()))
'HELLOWORLD'

请注意,尽管如此,该空格已删除.要解决此问题,您可以使用与空格不同的符号来分隔莫尔斯电码序列.或使用这个稍微复杂一点的版本,使用 re.split 在一个空格处分割,只有在该空格之后或之前没有另一个空格的情况下:

Note that this removed the space, though. To fix this, you could use a different symbol than space to separate the morse code sequences. Or use this slightly more complicated version using re.split to split at a space, only if that space is not followed, or not preceded by another space:

>>> ''.join(map(trans_back.get, re.split("(?<! ) | (?! )", morse)))
'HELLO WORLD'

要确定翻译方式(即原始文本是莫尔斯电文还是纯文本),您只需检查字符串的第一个字符或所有字符是否都在 translation 中字典,或者它们是否是有效的摩尔斯符号:

For deciding which way to translate, i.e. whether the original text is morse or plain-text, you could just check whether either the first, or all of the characters of the string are in the translation dictionary, or whether they are valid morse symbols:

if all(c in translation for c in msg):
    # translate to morse
elif all(c in ".- " for c in msg):
    # translate back
else:
    # don't know what to do


注意:此答案是在将尾随空格添加到字典中的所有条目之前发布的.


Note: This answer was posted before trailing spaces were added to all the entries in the dict.

这篇关于匹配键时使用字典并打印出值,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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