简体   繁体   中英

How to change the value of a string with a dictionary value in Python

I have the following dictionary in Python:

myDict = {"how":"como", "you?":"tu?", "goodbye":"adios", "where":"donde"}

and with a string like : "How are you?" I wish to have the following result once compared to myDict :

"como are tu?"

as you can see If a word doesn't appear in myDict like "are" in the result appears as it.

This is my code until now:

myDict = {"how":"como", "you?":"tu?", "goodbye":"adios", "where":"donde"}

def translate(word):
    word = word.lower()
    word = word.split()

    for letter in word:
        if letter in myDict:
           return myDict[letter]

print(translate("How are you?"))

As a result only gets the first letter : como , so what am I doing wrong for not getting the entire sentence?

Thanks for your help in advanced!

The problem is that you are returning the first word that is mapped in your dictionary, so you can use this (I have changed some variable names because is kind of confusing):

myDict = {"how":"como", "you?":"tu?", "goodbye":"adios", "where":"donde"}

def translate(string):
    string = string.lower()
    words = string.split()
    translation = ''

    for word in words:
        if word in myDict:
           translation += myDict[word]
        else:
           translation += word
        translation += ' ' # add a space between words

    return translation[:-1] #remove last space

print(translate("How are you?"))

Output:

'como are tu?'

The function returns (exits) the first time it hits a return statement. In this instance, that will always be the first word.

What you should do is make a list of words, and where you see the return currently, you should add to the list.

Once you have added each word, you can then return the list at the end.

PS: your terminology is confusing. What you have are phrases, each made up of words. "This is a phrase" is a phrase of 4 words: "This", "is", "a", "phrase". A letter would be the individual part of the word, for example "T" in "This".

When you call return , the method that is currently being executed is terminated, which is why yours stops after finding one word. For your method to work properly, you would have to append to a String that is stored as a local variable within the method.

Here's a function that uses list comprehension to translate a String if its exists in the dictionary :

def translate(myDict, string):
    return ' '.join([myDict[x.lower()] if x.lower() in myDict.keys() else x for x in string.split()])

Example:

myDict = {"how": "como", "you?": "tu?", "goodbye": "adios", "where": "donde"}

print(translate(myDict, 'How are you?'))

>> como are tu?

myDict = {"how":"como", "you?":"tu?", "goodbye":"adios", "where":"donde"}
s = "How are you?"

newString =''

for word in s.lower().split():
  newWord = word
  if word in myDict:
    newWord = myDict[word]
  newString = newString+' '+newWord

print(newString)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM