gpt4 book ai didi

python - 使用python在字符串中插入字符

转载 作者:行者123 更新时间:2023-12-01 05:59:59 26 4
gpt4 key购买 nike

这是我想要实现的目标:我想替换

void some_function(int,  int *, float);

void some_function(int a, int *b, float c);

到目前为止,我在循环字符串时尝试用 chr(97+i) 替换“,”。

text = len("void some_function(int,  int *, float);")
i = 0
for j in range(0, length)
if text[j] == ",":
rep = " " + chr(97+i) + " .,"
text = text.replace("," rep)
i = i + 1
j = 0 # resetting the index, but this time I want to find the next occurrence of "," which I am not sure

但这并不能完成任务。如果有更好的方法,请告诉我。

最佳答案

import re

i = 96
def replace(matches):
global i
i += 1
return " " + chr(i) + matches.group(0)

re.sub(',|\)', replace, "void some_function(int, int *, float);")

关于python - 使用python在字符串中插入字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10936699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com