将指定的文本从索引替换为python数组中的另一个索引
我想将数组中的搜索文本从指定元素替换为此数组中的另一个指定元素。 我知道有一个“替换”函数,但它将替换搜索字段中出现的所有内容。所以我想知道是否有其他函数或其他技巧可以满足我的要求 像这样:将指定的文本从索引替换为python数组中的另一个索引,python,arrays,Python,Arrays,我想将数组中的搜索文本从指定元素替换为此数组中的另一个指定元素。 我知道有一个“替换”函数,但它将替换搜索字段中出现的所有内容。所以我想知道是否有其他函数或其他技巧可以满足我的要求 像这样: 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 ",
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
有人能帮我吗?请谢谢大家! 假设myarray具有给定的格式,您可以编写如下内容:
def myfunc (fromtime, totime):
i = fromtime*2 - 1
while i <= (totime*2 - 1):
myarray[i] = myarray[i].replace('text', 'string')
i+=2
这就是您想要的吗?假设myarray具有给定的格式,您可以编写如下内容:
def myfunc (fromtime, totime):
i = fromtime*2 - 1
while i <= (totime*2 - 1):
myarray[i] = myarray[i].replace('text', 'string')
i+=2
这就是您要找的吗?您可以查找
time(4)
和time(8)
的索引,但使用myarray.index()
可以对包含在这些限制中的字符串进行更改
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)
输出
['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 ']
您可以查找
time(4)
和time(8)
的索引,但使用myarray.index()
可以对包含在这些限制中的字符串进行更改
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)
输出
['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 ']
你想把它换成什么?你能给我们你想要的输出吗?请提供一份你已经尝试过的和你想要的输出。列表中的元素(请注意,这是一个
列表
,不是一个数组
)实际上是单词“time”,还是它们是实际的时间戳?@G.Anderson no time只是一个字符串,必须位于我要查找的数组中的数字之前,以便我可以应用我的函数。您想将其替换为什么?你能给我们你想要的输出吗?请提供一份你已经尝试过的和你想要的输出。列表中的元素(请注意,这是一个列表
,而不是数组
)实际上是“时间”这个词吗,或者它们是实际的时间戳?@G.Anderson no time只是一个字符串,必须在我要查找的数组中的数字之前,这样我才能应用我的函数你能在这里加入我吗:chat.stackoverflow.com/rooms/195177/mayaCan你在这里加入我:chat.stackoverflow.com/rooms/195177/maya