-1

を使用して単語を別の単語に置き換えるにはどうすればよいですかloop。たとえば、function" " という名前があり、この関数でcan't、 should't 、 don't to cannot 、should not do not3changeWords単語を変更したいとします。そのため、関数に入ると、「戻る必要があります」changeWords("I don't know how to do this")'I do not know how to do this".

明確にするために:

changeWords(“I can't eat") -> “I can not eat"
changeWords(“I don't like swimming.”) -> “I do not like swimming.”
changeWords(“I shouldn't do that.”) -> “I should not do that.”

私の試み:

def stringChange(a):
a = ""
for line in stringChange("a"):
    line = text.replace("a","can't","can not")
    if not line: break
    return line
4

1 に答える 1

4
>>> def changeWords(s):
        for old, new in (
                ("can't", "can not"),
                ("shouldn't", "should not"),
                ("don't", "do not"),
            ):    
            s = s.replace(old, new)
        return s

>>> changeWords("I don't know how to do this")
'I do not know how to do this'
于 2012-09-13T05:27:23.873 に答える