最佳答案
这个简单的代码仅仅尝试用冒号替换分号(在 i 指定的位置)是不起作用的:
for i in range(0,len(line)):
if (line[i]==";" and i in rightindexarray):
line[i]=":"
它给出了错误
line[i]=":"
TypeError: 'str' object does not support item assignment
如何使用冒号替换分号?使用替换不起作用,因为该函数没有索引-可能有一些分号我不想替换。
例子
在字符串中,我可能有任意数量的分号,例如“黑德! ; 你好; ! ;”
我知道我想要替换哪一个(我在字符串中有它们的索引)。使用替换不起作用,因为我不能对它使用索引。