擅长:python、mysql、java
<pre><code>english_italian = {"hey": "ciao", "my": "mio", "name": "nome"}
s = "hey my name is Mario"
# split s into a list of words, and search the dict for each.
# if the word is not found, keep the original word
translated = " ".join(english_italian.get(word, word) for word in s.split(" "))
print(translated)
</code></pre>
<p>输出:</p>
<pre><code>'ciao mio nome is Mario'
</code></pre>