我要将数组中的搜索文本从指定元素替换为此数组中的另一个指定元素。 我知道有一个“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具有给定的格式,您可以编写如下内容:
myfunc(4, 8)
的输出是:这就是你要找的吗?你知道吗
您可以查找
time (4)
和time(8)
的索引,但是使用其中的myarray.index()
对包含在这些限制中的字符串进行更改输出
相关问题 更多 >
编程相关推荐