Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查字符串中的(仅整)字_Python_String_Count_Find - Fatal编程技术网

Python 检查字符串中的(仅整)字

Python 检查字符串中的(仅整)字,python,string,count,find,Python,String,Count,Find,关于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

关于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"
我可以请你帮忙吗?请以这段相当幼稚的代码为基础。

你最好把句子分开,然后数一数单词,而不是子字符串:

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\u words=re.findall(r“\w+”,textt.lower())

现在,如果您的“重要”单词列表很大,那么最好先统计所有单词,然后使用经典的
集合进行过滤。Counter

text_words = collections.Counter(re.split("\W+",textt.lower()))
result = {w:text_words.get(w) for w in wwords}

您的简单解决方案如下:

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]))

嘿,你想让你的代码做什么?要计算单词出现的次数吗?任务是返回一个字典,其中单词作为键,出现的次数作为值。就像现在一样。只忽略单词中的单词。值得注意的是,collections模块有一个内置的计数器类:建议您阅读感谢!我应该自己来的。很好用(当教程给了我一个包含撇号的单词,这个单词不能正确计算,但是我会在早上解决)。无论如何谢谢你!