[英]Iterate Through a Dictionary in Python
I have a function that accepts a string parameter that represents a Spanish-language sentence and returns a new string that is, respectively, a translation of the English-language sentence.我有一个函数,它接受一个表示西班牙语句子的字符串参数并返回一个新字符串,该字符串分别是英语句子的翻译。
According to the exercise I have, I have to translate every word in a sentence using the dictionary words that appear in the translate function.根据我的练习,我必须使用翻译功能中出现的词典词来翻译句子中的每个词。
def translate(sentence): # the function start here
words = {'esta': 'is', 'la': 'the', 'en': 'in', 'gato': 'cat', 'casa': 'house', 'el': 'the'}
This is the way to call a function, calling a function has the value of the sentence to be translated :这是调用函数的方式,调用函数具有要翻译的句子的值:
print(translate("el gato esta en la casa"))打印(翻译(“el gato esta en la casa”))
Your ideas on how I approach the problem I tried alone without success你对我如何解决我独自尝试但没有成功的问题的想法
You are supposed to iterate over the sentence, not the dictionary.你应该遍历句子,而不是字典。 In most cases, if you need to iterate a dictionary you are probably doing something wrong.
在大多数情况下,如果您需要迭代字典,您可能做错了什么。
def translate(sentence): # the function start here
words = {'esta': 'is', 'la': 'the', 'en': 'in', 'gato': 'cat', 'casa': 'house',
'el': 'the'}
return ' '.join(words[english_word] for english_word in sentence.split())
This will take in passed in Spanish sentence, split it to a list of words (splitting on whitespace), look up each word in the dict then put back everything together into a string using whitespace as a delimiter.这将接收传入的西班牙语句子,将其拆分为单词列表(在空格上拆分),在字典中查找每个单词,然后使用空格作为分隔符将所有内容放回一个字符串中。
Of course this is a naive solution that will not care about correct grammar.当然,这是一个天真的解决方案,不会关心正确的语法。 or about missing words (hint: use
try-except
or dict.get
to handle the latter).或关于遗漏的单词(提示:使用
try-except
或dict.get
来处理后者)。
print(translate("el gato esta en la casa"))
# the cat is in the house
How about something like this?这样的事情怎么样?
words = {'esta': 'is', 'la': 'the', 'en': 'in', 'gato': 'cat', 'casa': 'house', 'el': 'the'}
def translate(sentence):
splitted_sentence = sentence.split()
return ' '.join([words[word] for word in splitted_sentence])
print(translate("el gato esta en la casa"))
>> the cat is in the house
You can use a simple dictionary lookup using get
so, you can handle KeyError
:您可以使用
get
使用简单的字典查找,因此您可以处理KeyError
:
def translate(sentence): # the function start here
words = {'esta': 'is', 'la': 'the', 'en': 'in', 'gato': 'cat', 'casa': 'house', 'el': 'the'}
return ' '.join(words.get(x, '') for x in sentence.split())
Python dictionary works this way : dictionary["key"] = value
. Python 字典的工作方式是:
dictionary["key"] = value
。
for your code I would suggest using the words in your sentence as keys.对于您的代码,我建议您使用句子中的单词作为关键字。 you will then get the values.
然后你会得到这些值。
for example : words["el"] = "the"
例如:
words["el"] = "the"
def translate(sentence): # the function start here
ret = ''
words = {'esta': 'is', 'la': 'the', 'en': 'in', 'gato': 'cat', 'casa': 'house', 'el' : 'the'}
for w in words:
ret += words[w]+" "
return ret
translate("el gato esta en la casa")
def translate(sentence): # the function start here
words = {'esta': 'is', 'la': 'the', 'en': 'in', 'gato': 'cat', 'casa': 'house', 'el': 'the'}
english_words = sentence.split()
for word in english_words:
yield words[word]
result = translate("el gato esta en la casa")
for res in result:
print (res,end= " ")
I don't get why do you need a generator for this but whatever我不明白你为什么需要一个发电机,但不管怎样
this prints: the cat is in the house
这打印:
the cat is in the house
just a small note since I see you asked some people if a generator needs the 'yield' part, the answer is no.只是一个小笔记,因为我看到你问一些人发电机是否需要“产量”部分,答案是否定的。
def translate(sentence): # the function start here
words = {'esta': 'is', 'la': 'the', 'en': 'in', 'gato': 'cat', 'casa': 'house', 'el': 'the'}
english_sentence = (words[word] for word in sentence.split())
return type(english_sentence)
print(translate("el gato esta en la casa"))
this prints out: <class 'generator'>
这打印出:
<class 'generator'>
basically if you " ".join(english_words) you will get the sentence you want基本上如果你" ".join(english_words) 你会得到你想要的句子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.