这个简单的代码只是试图用冒号替换分号(在i指定的位置)不起作用:
for i in range(0,len(line)):
if (line[i]==";" and i in rightindexarray):
line[i]=":"
Run Code Online (Sandbox Code Playgroud)
它给出了错误
line[i]=":"
TypeError: 'str' object does not support item assignment
Run Code Online (Sandbox Code Playgroud)
我如何解决这个问题用冒号代替分号?使用replace不起作用,因为该函数不带索引 - 可能有一些我不想替换的分号.
例
在字符串中我可能有任意数量的分号,例如"Hei der !; Hello there;!;"
我知道我想要替换哪些(我在字符串中有他们的索引).使用replace不起作用,因为我无法使用索引.