Checkio 培训。该任务称为流行词。任务是从给定字符串的(字符串)列表中搜索单词。
例如:
textt="When I was One I had just begun When I was Two I was nearly new"
wwords=['i', 'was', 'three', 'near']
我的代码是这样的:
def popular_words(text: str, words: list) -> dict:
# your code here
occurence={}
text=text.lower()
for i in words:
occurence[i]=(text.count(i))
# incorrectly takes "nearly" as "near"
print(occurence)
return(occurence)
popular_words(textt,wwords)
几乎可以正常工作,返回
{'i': 4, 'was': 3, 'three': 0, 'near': 1}
因此将“接近”视为“接近”的一部分。这显然是作者的意图。但是,除了
"search for words that are not first (index 0) or last (last index) and for these that begin/end with whitespace"
请问可以帮忙吗?请以这个相当幼稚的代码为基础。
繁华开满天机
守着一只汪
相关分类