it4Astuces
it4Astuces

Reputation: 439

Replace specified text from index to another index in array python

I want to replace a searched text in an array from a specified element to another specified element in this array. I know there is a “replace” function but it will replace all the occurrences of that searched field. So I want to know if there is another function or another trick that can do what I want Like this:

myarray = ["time (1)",
"the text to replace ",
"time (2)",
"the text to replace ",
"time (3)",
"the text to replace ",
"time (4)",
"the text to replace ",
"time (5)",
"the text to replace ",
"time (6)",
"the text to replace ",
"time (7)",
"the text to replace ",
"time (8)",
"the text to replace ",
"time (9)",
"the text to replace ",
"time (10)",
"the text to replace "]

myfunc(4,8)

def myfunc(fromtime, totime):
    for line in myarray
    #find the time from (fromtime) to (totime) and replace 'text' with 'string' for example
    print myarray

Can any one help me? Please! Thank you!

Upvotes: 2

Views: 669

Answers (2)

Sebastien D
Sebastien D

Reputation: 4482

You could look for indexes of time (4) and time(8) but using the myarray.index() from there make the changes in the strings included into those limits

myarray = ["time (1)","the text to replace ","time (2)","the text to replace ","time (3)","the text to replace ","time (4)","the text to replace ","time (5)","the text to replace ","time (6)","the text to replace ","time (7)","the text to replace ","time (8)","the text to replace ","time (9)","the text to replace ","time (10)","the text to replace "] 

def myfunc(myarray, fromtime, totime):
    original_string , replace_string = 'text', 'string'
    start_index = myarray.index("time ({})".format(fromtime))
    end_index = myarray.index("time ({})".format(totime)) + 2 # + 2 because you want to also change value for the outbound limit
    myarray[start_index : end_index] = [value if idx%2 == 0 else value.replace(original_string, replace_string) for idx, value in enumerate(myarray[start_index : end_index]) ]
    return myarray

myfunc(myarray, 4,8)

Output

['time (1)',
 'the text to replace ',
 'time (2)',
 'the text to replace ',
 'time (3)',
 'the text to replace ',
 'time (4)',
 'the string to replace ',
 'time (5)',
 'the string to replace ',
 'time (6)',
 'the string to replace ',
 'time (7)',
 'the string to replace ',
 'time (8)',
 'the string to replace ',
 'time (9)',
 'the text to replace ',
 'time (10)',
 'the text to replace ']

Upvotes: 3

Charles Dupont
Charles Dupont

Reputation: 51

Assuming that myarray has the given format, you could write something like:

def myfunc (fromtime, totime):
    i = fromtime*2 - 1
    while i <= (totime*2 - 1):
        myarray[i] = myarray[i].replace('text', 'string')
        i+=2

Output of myfunc(4, 8) is:

['time (1)',
 'the text to replace ',
 'time (2)',
 'the text to replace ',
 'time (3)',
 'the text to replace ',
 'time (4)',
 'the string to replace ',
 'time (5)',
 'the string to replace ',
 'time (6)',
 'the string to replace ',
 'time (7)',
 'the string to replace ',
 'time (8)',
 'the string to replace ',
 'time (9)',
 'the text to replace ',
 'time (10)',
 'the text to replace ']

Is this what you are after?

Upvotes: 2

Related Questions