将字符串列表转换为模板化字符串列表

时间:2013-10-08 08:50:50

标签: python string list

我的主要要求是将一个模板添加到字符串列表中,并将它们作为单个字符串连接起来。

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

我对多个字符串添加不满意,即使这很简单直接。是否有任何内置库(仅内置,因为我无法安装任何第三方软件包)可以简化此过程?

意义上的简化

隐含的另一件事是分隔符是单个字符或双字符。可能的分隔符:“,”,“|”,“()”,“[]”,...

注意:如果你投票不好,请评论为什么你认为这是无用的。我和我今后可能会提出这个问题的人可能会根据你的观点学到一两件事。

3 个答案:

答案 0 :(得分:4)

更简洁的方法是使用string.template

>>> from string import Template
>>> def give_str(input_list, template, delimiter="()", joiner=""):
    s = Template("$temp$left$inner$right")
    data = []
    for elem in input_list:
        for t in reversed(template):
            elem = s.substitute(
                temp=t,
                left = delimiter[0],
                right = delimiter[-1],
                inner = elem)
        data.append(elem)
    return joiner.join(data)

>>> give_str(["first", "second", "third"], ["count", "rank"], delimiter="()", joiner=",")
'count(rank(first)),count(rank(second)),count(rank(third))'
>>> give_str(["first", "second", "third"], ["count", "rank"], delimiter="|", joiner=",")
'count|rank|first||,count|rank|second||,count|rank|third||'

答案 1 :(得分:1)

可读性当然是主观的。这是我可以获得的最“可读”的版本:

def give_str(input_list, template, delimiter="()", joiner=","):
    if len(delimiter) == 2:
        pattern = "TEMPLATE{0}INPUT{1}".format(delimiter[0], delimiter[1])
    else:
        pattern = "TEMPLATE{0}INPUT{0}".format(delimiter)
    final_list = input_list
    for tmp in reversed(template):
        final_list = [pattern.replace("TEMPLATE", tmp).replace("INPUT", x) for x in final_list]
    return joiner.join(final_list)

答案 2 :(得分:0)

我不完全理解你想要做什么,而且我不知道任何核心库可以做你想要的...如果你想要它非常有效你当然可以在C和C中编写函数将它暴露给python API(它比你想象的容易得多)......

对于具有1和2长度的分隔符,这是你想要的:

def give_str(input_list, template, delimiter="()", joiner=","):
    output_string =  item_close = ""
    template_string = delimiter[0].join(template) + delimiter[0]
    if len(delimiter) == 2:
        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

In [10]: give_str(["first", "second", "third"], ["count", "rank"], delimiter="()", joiner=",")
Out[10]: 'count(rank(first)),count(rank(second)),count(rank(third))'

In [11]: give_str(["first", "second", "third"], ["count", "rank"], delimiter=";", joiner=",")
Out[11]: 'count;rank;first,count;rank;second,count;rank;third'

进行速度辩论(见评论)

In [70]: d = 'd' * (10 **8)
In [71]: c = 'c' * (10 **8)
In [72]: b = 'b' * (10 **8)
In [73]: a = 'a' * (10 **8)


In [92]: %timeit ''.join((a,b,c,d,a,b,c,d,a,b,c,d))
1 loops, best of 3: 778 ms per loop

In [93]: %timeit a+b+c+d+a+b+c+d+a+b+c+d
1 loops, best of 3: 662 ms per loop

我希望''.join的速度提高10倍......但似乎不是