如何使用python计算字符串中单词的所有出现次数 [英] How to count all occurrences of a word in a string using python

查看:66
本文介绍了如何使用python计算字符串中单词的所有出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出像abdebobdfhbobob"这样的字符串中bob"出现的次数.

我的代码(我通过另一个 stackoverflow 问题找到的)是:

s = 'abdebobdfhbobob'print 'bob 出现的次数是:' + str(s.count('bob'))

此代码打印出:bob 出现的次数是:2 这不符合我的需要,因为答案应该是 3.

问题是这段代码没有将abdebobdfhbobob"算作两个不同的鲍勃,这正是我想要的.

如何修复代码以将字符串的 bobob 部分计算为两个单独的 bobos?

解决方案

基于 documentation, str.count() 返回 [start, end] 范围内子字符串 sub 的非重叠出现次数.您可以使用基于 positive lookahead 的正则表达式来查找重叠的字符串:<预><代码>>>>进口重新>>>s = 'abdebobdfhbobob'>>>len(re.findall(r'(?=bob)', s))3

如果您不想使用正则表达式,您可以在 sum() 函数中使用一个生成器表达式,该表达式将遍历所有长度为 3 的子字符串并计算那些子字符串的数量等于鲍勃":

<预><代码>>>>sum(s[i:i+3] == 'bob' for i in range(len(s)-2))3

I'm trying to find the number of times 'bob' occurs in a string of characters like 'abdebobdfhbobob'.

My code (that I found through another stackoverflow question) is:

s = 'abdebobdfhbobob'  
print 'The number of times bob occurs is: ' + str(s.count('bob'))

This code prints out: The number of times bob occurs is: 2 which is not correct for what I need as the answer should be 3.

The issue is that this code does not count 'abdebobdfhbobob' as two different bobs, which is what I want.

How can I fix the code to count the bobob part of the string as two separate bobs?

解决方案

Based on documentation, str.count() return the number of non-overlapping occurrences of substring sub in the range [start, end]. You can use a positive lookahead based regular expression in order to find the overlapped strings:

>>> import re
>>> s = 'abdebobdfhbobob'
>>> len(re.findall(r'(?=bob)', s))
3

If you don't want to use regex you can use a generator expression within the sum() function that will iterate over the all sub-strings with length 3 and count the number of those that are equal to 'bob':

>>> sum(s[i:i+3] == 'bob' for i in range(len(s)-2))
3

这篇关于如何使用python计算字符串中单词的所有出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆