How to find all the substrings that appear?

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

I want to know if there's something like string. Find'all() that returns all the indexes found (not only the first index at the beginning, but also the first index at the end).

For example:

string = "test test test test"

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

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

#1 building

Come on, let's recurse.

def locations_of_substring(string, substring):
    """Return a list of locations of a substring."""

    substring_length = len(substring)    
    def recurse(locations_found, start):
        location = string.find(substring, start)
        if location != -1:
            return recurse(locations_found + [location], location+substring_length)
        else:
            return locations_found

    return recurse([], 0)

print(locations_of_substring('this is a test for finding this and this', 'this'))
# prints [0, 27, 36]

This eliminates the need for regular expressions.

#2 building

This thread is a bit old, but it works for me:

numberString = "onetwothreefourfivesixseveneightninefiveten"
testString = "five"

marker = 0
while marker < len(numberString):
    try:
        print(numberString.index("five",marker))
        marker = numberString.index("five", marker) + 1
    except ValueError:
        print("String not found")
        marker = len(numberString)

#3 building

If you're just looking for a character, this works:

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 intuition is that none of these (especially second place) did well.

#4 building

It's an old topic, but I'm interested in sharing my solutions.

def find_all(a_string, sub):
    result = []
    k = 0
    while k < len(a_string):
        k = a_string.find(sub, k)
        if k == -1:
            return result
        else:
            result.append(k)
            k += 1 #change to k += len(sub) to not search overlapping results
    return result

It should return a list of where to find the substring. If you find errors or areas for improvement, please comment.

#5 building

It's still an old thread, but this is my use of a generator and a pure str.find solution.

def findall(p, s):
    '''Yields all the positions of
    the pattern p in the string s.'''
    i = s.find(p)
    while i != -1:
        yield i
        i = s.find(p, i+1)

example

x = 'banananassantana'
[(i, x[i:i+2]) for i in findall('na', x)]

Return goods

[(2, 'na'), (4, 'na'), (6, 'na'), (14, 'na')]

Tags: Python Lambda

Posted on Fri, 13 Mar 2020 10:43:24 -0400 by c4onastick