Sorry if the title was confusing. The exercise I'm working on is to write a function which takes two arguments: a dictionary which maps Swedish words to English, and a string corresponding to a Swedish sentence. The function should return a translation of the sentence into English
My function works well regarding the translation, but the output it produces is in the wrong order.
For example, the following argument, consisting of a dictionary and a string in Swedish (Eng: "A fine monkey")
translate_sentence({'en': 'a', 'fin': 'fine', 'apa': 'monkey'}, 'en fin apa')
should return
'a fine monkey'
but instead it returns
'a monkey fine'
Here is the script for the function:
def translate_sentence(d, sentence):
result = []
delimiter = ' '
s = sentence.split()
for word in d:
if word in sentence and word in d:
d_word = (d[word])
result.append(d_word)
for word in s:
if word in s and word not in d:
s_word = word
result.append(s_word)
return delimiter.join(result)
As I said before, the problem is that the function returns the translated string with the words in the wrong order. I know that python dictionaries aren't ordered, isn't there some way to get the output to be in the same order as sentence?