Home > Software engineering >  How can i write a function to create a list of indexes of selected characters in a string
How can i write a function to create a list of indexes of selected characters in a string

Time:10-25

I've been given a string and I need to create a function of a list containing the indexes of the spaces between the words.

CodePudding user response:

To do this with efficiency, you can use regular expression library:

import re
[match.start() for match in re.finditer(' ', 'my string is here')]
# [2, 9, 12]

re.finditer will search for you all occurences of first parameter, here a space, in your second string parameter
Then you can find start index of your match in match object with match.start()

This is to find all spaces and will not work if you want first index even if there are several spaces between two words, you may want to clarify your question

To compare to another solution timing:

import re
from time import time

text = "long text my test text was about 20000 chars"
start = time()
indices = [match.start() for match in re.finditer(' ', text)]
print(f'regex time :: {time() - start}')

start = time()
indices = []
text_length = len(text)
index = 0
while index < text_length:
    index = text.find(' ', index)
    if index == -1:
        break
    indices.append(index)
    index  = 1
print(f'str.find :: {time() - start}')

start = time()
indices = [i for i, c in enumerate(text) if c == " "]
print(f'enumerate :: {time() - start}')

# regex time :: 0.0004987716674804688
# str.find :: 0.0010783672332763672
# enumerate :: 0.0011782646179199219

On this example first solution is about two times faster (20000 chars text)

CodePudding user response:

Your question is not fully clear.

Assuming you have a string "abcdefgh" and a list [2,3,6] and you want to insert spaces before each position in the list, you could do:

s = 'abcdefgh'
idx = [2,3,6]
start = 0
out = []
for stop in idx [len(s)]:
    out.append(s[start:stop])
    start = stop
' '.join(out)

output: 'ab c def gh'

CodePudding user response:

You could use enumerate in a list comprehension:

s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"

[i for i,c in enumerate(s) if c==" "]

[5, 11, 17, 21, 27, 39, 50]
  • Related