简体   繁体   中英

How to find all occurrences of a list of strings in a string and return a list of list of int in python?

For example

def find_all_occurrences(a,b):
'''
>>>find_all_occurrences('wswwswwwswwwws', ['ws', 'wws'])
[[0,3,7,12], [2,6,11]]
'''

How can I return a list of lists that have all the occurrences without import any modules.

You can use regular expressions

import re
def all_occurrences(a, b):
    return [[occur.start() for occur in re.finditer(word, a)] for word in b]

Without imports it gets a little messy, but definitely still doable

def all_occurrences(a, b):
    result = []
    for word in b:
        word_res = []
        index = a.find(word)
        while index != -1:
           word_res.append(index)
           index = a.find(word, index+1)
        result.append(word_res)
    return result 

You can find all the occurrences by using the last found position as the start of the next search:

 str.find(...) S.find(sub [,start [,end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. 

A loop that calls haystack.find(needle, last_pos + 1) repeatedly until it returns -1 should work.

you can also have simple list comprehensions to help with problems like these

[[i for i in range(len(a)-len(strng)+1) if strng == a[i:i+len(strng)]] for strng in b]

where

>>> a
'wswwswwwswwwws'
>>> b
['ws', 'wws']

Solution with a recursive procedure. I used a nested/inner function to maintain the OP's function signature:

def find_all_occurrences(a,b):
    '''
    >>>find_all_occurrences('wswwswwwswwwws', ['ws', 'wws'])
    [[0,3,7,12], [2,6,11]]

    '''
    def r(a, b, count = 0, result = None):
        if not a:
            return result
        if result is None:
            # one sublist for each item in b
            result = [[] for _ in b]
        for i, thing in enumerate(b):
            if a.startswith(thing):
                result[i].append(count)
        return r(a[1:], b, count = count + 1, result = result)
    return r(a, b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM