这是我在提出建议之后最终得出的结果,好像Eng词典与Tuc词典相同。这个程序将英语单词翻译成Tuccin,但我不能为我的生活得到它翻译Tuccin到英语Pleae告诉我如何实现这一目标。如果输入了非存储的单词,我将其设置为只打印单词本身。但是,如果它不是存储的英语单词,我甚至无法让elif触发它直接进入其他条件。
.rs-range-clip-custom
答案 0 :(得分:0)
您目前设置的方式,Eng
和Tuc
完全相同。看起来你想要的是
>>> Eng = {e[0]: [t] for t, e in Tuc.items()}
>>> Eng
{'yem': ['my'], 'ye': ['me'], 'uo': ['you'], 'o': ['i'], 'sia': 'are'], 'yeme': ['mine'], 'wau': ['love']}
作为旁注,实际上并不需要在哈希列表中创建值,因为它们都包含单个字符串,但这不是什么大问题。
答案 1 :(得分:0)
你有一个拼写错误:
Eng = {e[0]: [t] for t, e in Tuc.items()}
答案 2 :(得分:0)
你应该做的
eng = {v[0]: [k] for k, v in tuc.items()}
或iteritems()
使用Python 2。
注意,而不是:
while phrase == True
你应该写:
while phrase
答案 3 :(得分:0)
您可以在修复此错误时清理代码(您复制了字典而不是反转它):
Tuc={"i":"o", "love":"wau", "you":"uo", "me":"ye", "my":"yem", "mine":"yeme", "are":"sia"}
Eng = {e:t for t, e in Tuc.items()}
print "ENG"
print Eng
print "TUC"
print Tuc
phrase=True
while phrase:
translation = raw_input("Enter content for translation.\n").lower()
phrase = translation.split() # empty input will break the loop
for word in phrase:
print Tuc.get(word, Eng.get(word, 'Word Not Stored'))
答案 4 :(得分:0)
此版本假设每个单词都有多个翻译(这就是您在每个单词上使用列表的原因):
from collections import defaultdict
tuc_dictionary = {
"i": ["o"],
"love": ["wau"],
"you": ["uo"],
"me": ["ye"],
"my": ["yem"],
"mine": ["yeme"],
"are": ["sia"]
}
english_dictionary = defaultdict(list)
for k, v in tuc_dictionary.items():
for word in v:
english_dictionary[word].append(k)
english_dictionary = dict(english_dictionary)
print "ENG"
print english_dictionary
print "TUC"
print tuc_dictionary
phrase = True
reverseLookup = False
while phrase == True:
translation = str(raw_input("Enter content for translation.\n").lower())
input_list = translation.split()
for word in input_list:
# English to Tuccin
if word in tuc_dictionary:
print("".join(tuc_dictionary[word])) + " *English>>Tuccin"
# Tuccin to English
elif word in english_dictionary:
print("".join(english_dictionary[word])) + " *Tuccin>>English"
else:
print word + " *Word Not Stored"