我的主要要求是将模板添加到字符串列表并将它们作为单个字符串加入。
def give_str(input_list, template, delimiter="()", joiner=""):
#Some operation happens here
return output_string
Input: give_str(["first", "second", "third"], ["count", "rank"], delimiter="()", joiner=",")
Output: "count(rank(first)),count(rank(second)),count(rank(third))"
现在,我正在做这样的事情:
def give_str(input_list, template, delimiter="()", joiner=","):
output_string = ""
template_string = delimiter[0].join(template) + delimiter[0]
item_close = delimiter[-1] * len(template)
output_string = joiner.join(template_string+item+item_close for item in input_list if item)
return output_string
我对多个字符串添加不满意,尽管这很简单直接。是否有任何内置库(仅内置,因为我无法安装任何 3rd 方包)可以简化此过程?简化意义上的
暗示的另一件事是分隔符是单个字符或双字符。可能的分隔符:“,”,“|”,“()”,“[]”,...
注意:如果您投反对票,请评论为什么您认为这是无用的。我和将来可能会提出这个问题的人可能会从您的观点中学到一两件事。