为了账号安全,请及时绑定邮箱和手机立即绑定

检查字符串中的(仅整个)单词

检查字符串中的(仅整个)单词

qq_遁去的一_1 2021-06-14 08:18:25
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"请问可以帮忙吗?请以这个相当幼稚的代码为基础。
查看完整描述

2 回答

?
繁华开满天机

TA贡献1816条经验 获得超4个赞

你最好拆分你的句子,然后计算单词,而不是子字符串:


textt="When I was One I had just begun When I was Two I was nearly new"

wwords=['i', 'was', 'three', 'near']

text_words = textt.lower().split()

result = {w:text_words.count(w) for w in wwords}


print(result)

印刷:


{'three': 0, 'i': 4, 'near': 0, 'was': 3}

如果文本现在有标点符号,最好使用正则表达式根据非字母数字拆分字符串:


import re


textt="When I was One, I had just begun.I was Two when I was nearly new"


wwords=['i', 'was', 'three', 'near']

text_words = re.split("\W+",textt.lower())

result = {w:text_words.count(w) for w in wwords}

结果:


{'was': 3, 'near': 0, 'three': 0, 'i': 4}

(另一种替代方法是使用findall在字字符:text_words = re.findall(r"\w+",textt.lower()))


现在,如果您的“重要”单词列表很大,也许最好计算所有单词,然后使用经典过滤器进行过滤collections.Counter:


text_words = collections.Counter(re.split("\W+",textt.lower()))

result = {w:text_words.get(w) for w in wwords}


查看完整回答
反对 回复 2021-06-16
?
守着一只汪

TA贡献1872条经验 获得超3个赞

您的简单解决方案是:


from collections import Counter


textt="When I was One I had just begun When I was Two I was nearly new".lower()

wwords=['i', 'was', 'three', 'near']


txt = textt.split()


keys = Counter(txt)


for i in wwords:

    print(i + ' : ' + str(keys[i]))


查看完整回答
反对 回复 2021-06-16
  • 2 回答
  • 0 关注
  • 175 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信