Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python中的词典翻译短语_Python_Python 2.7_Dictionary - Fatal编程技术网

用Python中的词典翻译短语

用Python中的词典翻译短语,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,我正在做一个项目,目标是创建两个程序。一个程序从用户那里读取输入并将其翻译成Leetspeak,而另一个程序将Leetspeak转换回英语。我决定尝试使用字典来处理翻译,而不是滥发If语句或replace(“,”) 转换成Leetspeak很容易。我只是用了一本字典和一个for循环 但是,由于Leetspeak中的“字母”有时包含多个字符,我发现很难将其转换回来而不出现关键错误。 这是我到目前为止所拥有的 phrase = raw_input('Enter a phrase: ') output

我正在做一个项目,目标是创建两个程序。一个程序从用户那里读取输入并将其翻译成Leetspeak,而另一个程序将Leetspeak转换回英语。我决定尝试使用字典来处理翻译,而不是滥发If语句或replace(“,”)

转换成Leetspeak很容易。我只是用了一本字典和一个for循环

但是,由于Leetspeak中的“字母”有时包含多个字符,我发现很难将其转换回来而不出现关键错误。

这是我到目前为止所拥有的

phrase = raw_input('Enter a phrase: ')
output = []

KEY = {
    '4': 'A',
    '8': 'B',
    '(': 'C',
    '|)': 'D',
    '3': 'E',
    '|=': 'F',
    '6': 'G',
    '|-|': 'H',
    '!': 'I',
    '_|': 'J',
    '|<': 'K',
    '1': 'L',
    '|\/|': 'M',
    '|\|': 'N',
    '0': 'O',
    '|D': 'P',
    '(,)': 'Q',
    '|?': 'R',
    '5': 'S',
    '7': 'T',
    '|_|': 'U',
    '\/': 'V',
    '\/\/': 'W',
    '><': 'X',
    '`/': 'Y',
    '2': 'Z',
    ' ': ' ',
}

for char in phrase:
    if char in KEY:
        # This line works perfectly, since it only requires a single   
        # character
        print(KEY[char])
    else:
        while KEY[char] == False:
        # If I'm not getting KeyErrors I'm getting errors with appending
        # special characters or NoneType characters
        output = output.append(char)
    print(output)
    # I tried clearing the output after every iteration so that it could be
    # reused by the next char in phrase
    output = []

# Understand that all of the prints() are for testing the program. I'm   
# hoping to just have a single print() function at the end once everything  
# has been translated.      
phrase=raw_输入('输入短语:')
输出=[]
键={
‘4’:‘A’,
‘8’:‘B’,
“(”:“C”,
“|”):“D”,
‘3’:‘E’,
“|=”:“F”,
‘6’:‘G’,
“|-|”:“H”,
“!”:“我”,
"|":"J",,

“|我将生成一个键列表,并根据长度对它们进行排序(因此“//”将是列表中的第一个)。一旦有了一个反向排序的列表,您就可以检查测试中当前键的字符串开头以进行反向翻译

decodeString = ""
x = "! 4|\/| 31!73"
while len(x) > 0:
    for currentSearch in keys:
        if currentSearch in x[:len(currentSearch)]:
            print(currentSearch)
            decodeString += KEY[currentSearch]
            x = x[len(currentSearch):]

print( decodeString)
I AM ELITE

前进很容易,正如你所指出的:

message = 'HALLOWEEN'
KEY_reversed = {v:k for k, v in KEY.items()}

>>> print(''.join(KEY_reversed[c] for c in message))
|-|4110\/\/33|\|
往另一个方向走有点困难,但您可以使用
replace()


如果没有排序,可能会出现V和W等字符的歧义,并且它们可能是不一致的歧义,因为字典没有默认的排序。

如果有多个字母,而不是以字符开头,则可能会将字典更改为嵌套的dicts作为值,即对于字符
,您将有
':{'|)':'D','|-|':'H','|@RNar你能给我举个例子,说明如何进行“类型检查”吗你建议了吗?我只使用Python几周。我假设我必须更改for循环中的某些内容。我的方法可能不是当时最好使用的方法。我建议使用@BlivetWidget的方法,因为遍历键并将其替换为其他键会更好。正如我对他的答案所评论的那样,我会这样做按反向长度(首先是最长长度)对键进行rt,以消除键中的一些歧义。然而,一行中的2V总是读作W,但我不认为这真的可以避免。这不太管用。如果您输入
“!4 | \/| 31!73”
,您会得到
“IA”
。这对M不起作用(
“| \/|”
)。我得到
“|V”
。您可以通过迭代排序(KEY.items(),KEY=lambda x:len(x[0]),reverse=True)中k,v的
来改进这一点。
它将首先迭代具有最长键的项列表,以消除字母M、v和WGood edit、gents的键的歧义,从而解决任意字典顺序的问题。
output = '|-|4110\\/\\/33|\\|'
for k, v in sorted(KEY.items(), key=lambda x: len(x[0]), reverse=True):
    if k in output:
        output = output.replace(k, v)

>>> output
'HALLOWEEN'