2

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!

3
  • 2
    What do you want to replace it to? can you give us your desired output? Commented Jun 20, 2019 at 16:02
  • 3
    Please provide a minimal reproducible example with what you've tried and your desired output. Are the elements in your list (note that this is a list, not an array) actually the word "time", or are they actual timestamps? Commented Jun 20, 2019 at 16:03
  • 1
    @G.Anderson no time is just a string that has to be before the number in my array that I want to look for so I can apply my function Commented Jun 21, 2019 at 8:36

2 Answers 2

3

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 ']
Sign up to request clarification or add additional context in comments.

1 Comment

Can you join me here: chat.stackoverflow.com/rooms/195177/maya
2

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.