Find all occurrences of a substring in Python

2018-12-31 04:02发布

Python has string.find() and string.rfind() to get the index of a substring in string.

I wonder, maybe there is something like string.find_all() which can return all founded indexes (not only first from beginning or first from end)?

For example:

string = "test test test test"

print string.find('test') # 0
print string.rfind('test') # 15

#that's the goal
print string.find_all('test') # [0,5,10,15]

16条回答
零度萤火
2楼-- · 2018-12-31 04:26

You can try :

>>> string = "test test test test"
>>> for index,value in enumerate(string):
    if string[index:index+(len("test"))] == "test":
        print index

0
5
10
15
查看更多
孤独总比滥情好
3楼-- · 2018-12-31 04:27

Here's a (very inefficient) way to get all (i.e. even overlapping) matches:

>>> string = "test test test test"
>>> [i for i in range(len(string)) if string.startswith('test', i)]
[0, 5, 10, 15]
查看更多
ら面具成の殇う
4楼-- · 2018-12-31 04:27

If you're just looking for a single character, this would work:

string = "dooobiedoobiedoobie"
match = 'o'
reduce(lambda count, char: count + 1 if char == match else count, string, 0)
# produces 7

Also,

string = "test test test test"
match = "test"
len(string.split(match)) - 1
# produces 4

My hunch is that neither of these (especially #2) is terribly performant.

查看更多
美炸的是我
5楼-- · 2018-12-31 04:29

please look at below code

#!/usr/bin/env python
# coding:utf-8
'''黄哥Python'''


def get_substring_indices(text, s):
    result = [i for i in range(len(text)) if text.startswith(s, i)]
    return result


if __name__ == '__main__':
    text = "How much wood would a wood chuck chuck if a wood chuck could chuck wood?"
    s = 'wood'
    print get_substring_indices(text, s)
查看更多
长期被迫恋爱
6楼-- · 2018-12-31 04:31

You can easily use:

string.count('test')!

https://www.programiz.com/python-programming/methods/string/count

Cheers!

查看更多
步步皆殇っ
7楼-- · 2018-12-31 04:35

You can use re.finditer() for non-overlapping matches.

>>> import re
>>> aString = 'this is a string where the substring "is" is repeated several times'
>>> print [(a.start(), a.end()) for a in list(re.finditer('is', aString))]
[(2, 4), (5, 7), (38, 40), (42, 44)]

but won't work for:

In [1]: aString="ababa"

In [2]: print [(a.start(), a.end()) for a in list(re.finditer('aba', aString))]
Output: [(0, 3)]
查看更多
登录 后发表回答