This is what I have ended up with after suggestions were made it appears as if the Eng dictionary is identical to the Tuc one. This program will translate English words to Tuccin but I can not for the life of me get it to translate Tuccin to English Pleae tell me how to achieve this. In the event a non stored word is input I have it set to just print the word itself. But i don't even manage to get the elif to trigger it goes straight to the else condition if it's not a stored English word.
Tuc={"i":["o"],"love":["wau"],"you":["uo"],"me":["ye"],"my":["yem"],"mine":["yeme"],"are":["sia"]}
Eng = {t: e for t, e in Tuc.items()}
print "ENG"
print Eng
print "TUC"
print Tuc
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:
print ("".join(Tuc[word]))+" *English>>Tuccin"
#Tuccin to English
elif word in Eng:
print ("".join(Eng[word]))+" *Tuccin>>English"
else:
print word+" *Word Not Stored"
The way you have it currently set, Eng
and Tuc
are identical. It seems like what you want is
>>> 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']}
As a side note, there's not really a need to make the values in the hash lists since they all contain a single string, but that's not a big deal.
您在那里有错字:
Eng = {e[0]: [t] for t, e in Tuc.items()}
You ought to do
eng = {v[0]: [k] for k, v in tuc.items()}
Or iteritems()
with Python 2.
Note, instead of:
while phrase == True
You should write:
while phrase
You can clean up your code quite a bit while fixing this error (you copied the dictionary instead of reversing it):
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'))
This version assumes each word will have multiple translations (that's the reason you're using lists on each word):
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"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.