I always enjoy solving these problems from first principles. Even if there are provided modules and methods which could do it you get a good mental workout and can tailor results to exactly how you want.
It also allows the code to run under MicroPython on an RP2 where that might not have all the modules a desktop Python may have.
Half of this code is just producing pretty output -
It also allows the code to run under MicroPython on an RP2 where that might not have all the modules a desktop Python may have.
Half of this code is just producing pretty output -
Code:
class Tokenise(): def __init__(self, s, keys): self.tokens = {} start = 0 while start < len(s): while start < len(s) and s[start] != " ": start += 1 while start < len(s) and s[start] == " ": start += 1 if start < len(s): end = start + 1 while end < len(s) and s[end] != " ": end += 1 word = s[start:end] if word in keys: if not word in self.tokens : self.tokens[word] = [start] else : self.tokens[word].append(start) start = endline_in = "Emerson Lake and Palmer met on a Lake Swamp"keys = ['Stream', 'Lake','Pond','Swamp']tokens = Tokenise(line_in, keys).tokenst = " " * 9b = " " * 9for n in range(len(line_in)): t += str(n // 10) b += str(n % 10)print(t)print(b)print("Source : {}".format(line_in))if len(tokens) == 0: print("No matches")else: for n in range(len(line_in)): for key in tokens: if n in tokens[key]: print("{:>6} : {}^ {}".format(n, " " * n, key)) print("") for key in tokens: print(key, tokens[key])Code:
0000000000111111111122222222223333333333444 0123456789012345678901234567890123456789012Source : Emerson Lake and Palmer met on a Lake Swamp 8 : ^ Lake 33 : ^ Lake 38 : ^ SwampLake [8, 33]Swamp [38]Statistics: Posted by hippy — Sun Aug 17, 2025 4:37 pm