如果子串重叠,如何计算python中的子串数? [英] How to count number of substrings in python, if substrings overlap?

查看:23
本文介绍了如果子串重叠,如何计算python中的子串数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

count() 函数返回子字符串在字符串中出现的次数,但在字符串重叠的情况下会失败.

假设我的输入是:

^_^_^-_-

我想找出^_^在字符串中出现的次数.

mystr=input()happy=mystr.count('^_^')悲伤=mystr.count('-_-')打印(快乐)打印(悲伤)

输出为:

11

我期待:

21

我怎样才能达到预期的效果?

解决方案

新版本

您无需使用正则表达式编写任何显式循环即可解决此问题.正如 @abhijith-pk 的回答 巧妙地暗示的那样,您可以只搜索第一个字符,其余的放在正数中前瞻,这将允许您进行重叠匹配:

def count_overlapping(string, pattern):正则表达式 = '{}(?={})'.format(re.escape(pattern[:1]), re.escape(pattern[1:]))# 使用迭代器,以最少的内存使用量获取计数return sum(1 for _ in re.finditer(regex, string))

[IDEOne 链接]

使用 [:1][1:] 作为索引允许函数处理空字符串而无需特殊处理,同时使用 [0][1:] 用于索引不会.

旧版本

您始终可以使用str.find 允许您指定起始索引.这个例程不会很有效,但它应该可以工作:

def count_overlapping(string, pattern):计数 = 0开始 = -1为真:start = string.find(pattern, start + 1)如果开始<0:返回计数计数 += 1

[IDEOne 链接]

使用

两个版本都返回相同的结果.示例用法是:

<预><代码>>>>mystr = '^_^_^-_-'>>>count_overlapping(mystr, '^_^')2>>>计数重叠(mystr,'-_-')1>>>count_overlapping(mystr, '')9>>>count_overlapping(mystr,'x')0

注意空字符串被找到 len(mystr) + 1 次.我认为这在直觉上是正确的,因为它有效地位于每个角色之间和周围.

The count() function returns the number of times a substring occurs in a string, but it fails in case of overlapping strings.

Let's say my input is:

^_^_^-_-

I want to find how many times ^_^ occurs in the string.

mystr=input()
happy=mystr.count('^_^')
sad=mystr.count('-_-')
print(happy)
print(sad)

Output is:

1
1

I am expecting:

2
1

How can I achieve the desired result?

解决方案

New Version

You can solve this problem without writing any explicit loops using regex. As @abhijith-pk's answer cleverly suggests, you can search for the first character only, with the remainder being placed in a positive lookahead, which will allow you to make the match with overlaps:

def count_overlapping(string, pattern):
    regex = '{}(?={})'.format(re.escape(pattern[:1]), re.escape(pattern[1:]))
    # Consume iterator, get count with minimal memory usage
    return sum(1 for _ in re.finditer(regex, string))

[IDEOne Link]

Using [:1] and [1:] for the indices allows the function to handle the empty string without special processing, while using [0] and [1:] for the indices would not.

Old Version

You can always write your own routine using the fact that str.find allows you to specify a starting index. This routine will not be very efficient, but it should work:

def count_overlapping(string, pattern):
    count = 0
    start = -1
    while True:
        start = string.find(pattern, start + 1)
        if start < 0:
            return count
        count += 1

[IDEOne Link]

Usage

Both versions return identical results. A sample usage would be:

>>> mystr = '^_^_^-_-'
>>> count_overlapping(mystr, '^_^')
2
>>> count_overlapping(mystr, '-_-')
1
>>> count_overlapping(mystr, '')
9
>>> count_overlapping(mystr, 'x')
0

Notice that the empty string is found len(mystr) + 1 times. I consider this to be intuitively correct because it is effectively between and around every character.

这篇关于如果子串重叠,如何计算python中的子串数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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