Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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_Algorithm - Fatal编程技术网

Python 为猫计算字符串中的单词

Python 为猫计算字符串中的单词,python,algorithm,Python,Algorithm,我试图计算单词或字符串cat在给定变量字符串p中出现的次数。然而,我的代码是不正确的,尽管我认为我的逻辑是正确的。我需要输出只打印一次“cat发生次数:2”。任何帮助或建议都将不胜感激 p = 'thecatisnapingandtheothercatiseating' count = 0 word = 'cat' for i in range(0, len(p)+1): if p[i:i+3] == word: count += 1 print('Num

我试图计算单词或字符串cat在给定变量字符串p中出现的次数。然而,我的代码是不正确的,尽管我认为我的逻辑是正确的。我需要输出只打印一次“cat发生次数:2”。任何帮助或建议都将不胜感激

p = 'thecatisnapingandtheothercatiseating'
count = 0
word = 'cat'
for i in range(0, len(p)+1):
    if p[i:i+3] == word:
        count += 1
        print('Number of times cat occurs: ' + str(count))

您只需将打印移动到for循环的外部:

p = 'thecatisnapingandtheothercatiseating'
count = 0
word = 'cat'
for i in range(0, len(p)+1):
    if p[i:i+3] == word:
        count += 1


print('Number of times cat occurs: ' + str(count))
>>>Number of times cat occurs: 2
看@Tim,补充和扩展@pythonic833-answer,你的for循环语句应该是范围为0的i,lenp-2:,要做到这一点,i=0,1,2,…,lenp-3, 因为例如,取p='thecastinap',lenp=11,您将比较如下字符串:

'the'=='cat', 'hec'=='cat', 'eca'=='cat', ... , p[i:i+3] == 'cat'
正如@pythonic833所说,您只需要迭代到最后的第三个字母, 因为在那之后,你将比较少于三个字母的单词:

#iterarion 
i=len(p)-3=8
p[i:i+3]='nap'

Compare 'nap'=='cat'

#iterarion 
i=len(p)-2=9
p[i:i+3]='ap'

Compare 'ap'=='cat'

#iterarion
i=len(p)-1=10
p[i:i+3]='p'

Compare 'p'=='cat'

#iteration 
i=len(p)=11
p[i:i+3]=''
Compare ''=='cat'

因此,在迭代之后,当i=lenp-2时,继续比较是没有意义的。

您只需将打印移到for循环之外:

p = 'thecatisnapingandtheothercatiseating'
count = 0
word = 'cat'
for i in range(0, len(p)+1):
    if p[i:i+3] == word:
        count += 1


print('Number of times cat occurs: ' + str(count))
>>>Number of times cat occurs: 2
看@Tim,补充和扩展@pythonic833-answer,你的for循环语句应该是范围为0的i,lenp-2:,要做到这一点,i=0,1,2,…,lenp-3, 因为例如,取p='thecastinap',lenp=11,您将比较如下字符串:

'the'=='cat', 'hec'=='cat', 'eca'=='cat', ... , p[i:i+3] == 'cat'
正如@pythonic833所说,您只需要迭代到最后的第三个字母, 因为在那之后,你将比较少于三个字母的单词:

#iterarion 
i=len(p)-3=8
p[i:i+3]='nap'

Compare 'nap'=='cat'

#iterarion 
i=len(p)-2=9
p[i:i+3]='ap'

Compare 'ap'=='cat'

#iterarion
i=len(p)-1=10
p[i:i+3]='p'

Compare 'p'=='cat'

#iteration 
i=len(p)=11
p[i:i+3]=''
Compare ''=='cat'
因此,在迭代之后,当i=lenp-2时,继续比较是没有意义的。

您也可以使用re.findall:

印刷品

Number of times cat occurs: 2
或者更具可读性的版本:

import re

p = 'thecatisnapingandtheothercatiseating'
word = 'cat'
count = len(re.findall(word, p))
print(f'Number of times cat occurs: {count}')
您自己的代码也很好,需要稍加修改:

p = 'thecatisnapingandtheothercatiseatingcat'
count = 0
word = 'cat'
for i in range(-~len(p) - len(word)):
    if p[i:i + len(word)] == word:
        count += 1
print(f'Number of times cat occurs: {count}')

或:

-~lenp是一种逐位递增操作-~我只是说我+1

您也可以使用re.findall:

印刷品

Number of times cat occurs: 2
或者更具可读性的版本:

import re

p = 'thecatisnapingandtheothercatiseating'
word = 'cat'
count = len(re.findall(word, p))
print(f'Number of times cat occurs: {count}')
您自己的代码也很好,需要稍加修改:

p = 'thecatisnapingandtheothercatiseatingcat'
count = 0
word = 'cat'
for i in range(-~len(p) - len(word)):
    if p[i:i + len(word)] == word:
        count += 1
print(f'Number of times cat occurs: {count}')

或:

-~lenp是一种逐位递增操作-~我只是说我+1

始终有可使用的计数方法:

p = 'thecatisnapingandtheothercatiseating'
word = 'cat'
print(f'Number of times cat occurs: {p.count(word)}')
输出:

Number of times cat occurs: 2
始终有可使用的计数方法:

p = 'thecatisnapingandtheothercatiseating'
word = 'cat'
print(f'Number of times cat occurs: {p.count(word)}')
输出:

Number of times cat occurs: 2

问问你自己什么是s?你会看到问题我修正了输入错误,但我仍然得到一个错误仅供参考,得到一个你不期望的结果并不是一个错误信息,以防有人需要在非分配代码中这样做:p.count'cat'for I in range 0,lenp-2:因为在下面的一行中,你要从做p[I:I+3]开始问自己是什么?你会看到问题我修正了输入错误,但我仍然得到一个错误仅供参考,得到一个你不期望的结果并不是一个错误消息,以防有人需要在非赋值代码中这样做:p.count'cat'for I in range0,lenp-2:因为在下面的行中,你要从做p[I:I+3]非常感谢你澄清这一点并向我解释!很高兴它能帮助你@Tim。谢谢你的支持非常感谢你澄清这一点并向我解释!很高兴它能帮助你@Tim。谢谢你的支持