gpt4 book ai didi

python - 在Python中动态替换字符串

转载 作者:行者123 更新时间:2023-11-30 23:23:17 24 4
gpt4 key购买 nike

我有用字符串编写的多个变量的数学函数;这些自变量具有以下符号约定:

示例:

f_sym_st="(sin(x0))**2+exp(x1)*cos(x2)"

我在不同的计算中使用它们。为了使用 scipy 的最小化例程如下需要 x 的约定

f_opt_st="(sin(x[0]))**2+exp(x[1])*cos(x[2])"

我用

f_opt_st=f_sym_st.replace("x0","x[0]").replace("x1","x[1]").replace("x2","x[2]")

这可行,但我正在寻找更有活力的东西。如果 f_sym 来自另一个脚本并具有例如 21 个自变量怎么办?

“想法”:

for i in range(0,21,1):#end of loop should also be variable
f_sym_st=f_sym_st.replace("xi","x[i]")# obviously iteration does not work in strings

f_opt_st=f_sym_st

这是一个示例 - 基本上我想知道是否有一种方法可以动态替换字符串?

最佳答案

您可以使用sub来自 re 的函数图书馆:

In [10]: import re

In [11]: f_sym_st = "(sin(x0))**2+exp(x1)*cos(x2)"

In [12]: f_opt_st = re.sub(r"\bx(\d+)", r"x[\1]", f_sym_st)

In [13]: f_opt_st
Out[13]: '(sin(x[0]))**2+exp(x[1])*cos(x[2])'

模式r"\bx(\d+)" 匹配单词边界,后跟字母x,然后是数字序列。 \d+ 两边的括号会将数字保存为一组。该组在替换字符串中被引用为 \1

如果您想将索引降低 1,可以将替换字符串更改为 r"x[\1-1]"。例如

In [56]: s = "x1*sin(x2) + x10"

In [57]: re.sub(r"\bx(\d+)", r"x[\1-1]", s)
Out[57]: 'x[1-1]*sin(x[2-1]) + x[10-1]'

因为这只是字符串替换,所以它不会将 1-1 简化为 0

如果您不希望其中包含所有这些 -1,您可能不会使用 re.sub。相反,您可以执行类似以下操作,其中使用 re.findall 查找表达式中使用的索引:

In [132]: s = "x1*sin(x2) + x10*cos(x2)"

In [133]: indices = sorted(set(int(n) for n in re.findall(r'\bx(\d+)', s)), reverse=True)

In [134]: indices
Out[134]: [10, 2, 1]

In [135]: t = s[:] # Make a copy of s.

In [136]: for ind in indices:
.....: t = t.replace('x' + str(ind), 'x[' + str(ind-1) + ']')
.....:

In [137]: t
Out[137]: 'x[0]*sin(x[1]) + x[9]*cos(x[1])'

如果您愿意,可以将 for 循环更改为使用 format 方法:

In [140]: for ind in indices:
.....: t = t.replace("x{0}".format(ind), "x[{0}]".format(ind-1))
.....:

关于python - 在Python中动态替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24062929/

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