框架:机器人,语言: Python-3.7.1 专精:新手
我有下面的核心方法,它取代了匹配的动态位置,并且在我现有的自动化脚本中得到了广泛的应用。
def querybuilder(self, str, symbol, *args):
count = str.count(symbol)
str = list(str)
i = 0
j = 0
if (count == (len(args))):
while (i < len(str)):
if (str[i] == symbol):
str[i] = args[j]
j = j + 1
i = i + 1
else:
return ("Number of passed arguments are either more or lesser than required.")
return ''.join(str)如果像下面这样发送的参数,这会很好。
def querybuilder("~test~123", "~", "foo","boo")但是,如果我想作为一个列表发送替代可选参数,它将每个list/tuple/array作为一个参数,因此不会进入If条件。
例如,:-
i = ["foo", "boo"] -- Optional argument consider it as ('foo, boo',)显然,由于在现有框架中的广泛使用,由于-ve的影响,我无法对方法(Querybuilder)进行任何更改。
我想要摆脱的东西:-
1, ", ".join("{}".format(a) for a in i,
2, tuple(i),
3, list(i),
4, numpy.array(i) part of import numpy是否有根据要求转换参数的可能解决方案?
发布于 2019-05-25 14:35:56
以*['foo', 'boo']的形式传递列表
def querybuilder(s, symbol, *args):
count = s.count(symbol)
st = list(s)
i = 0
j = 0
if (count == (len(args))):
while (i < len(st)):
if (st[i] == symbol):
st[i] = args[j]
j = j + 1
i = i + 1
else:
return ("Number of passed arguments are either more or lesser than required.")
return ''.join(st)
print(querybuilder("~test~123", "~", "foo","boo"))
# footestboo123
print(querybuilder("~test~123", "~", *["foo","boo"]))
# footestboo123https://stackoverflow.com/questions/56305697
复制相似问题