2

我的主要要求是将模板添加到字符串列表并将它们作为单个字符串加入。

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 方包)可以简化此过程?简化意义上的

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

注意:如果您投反对票,请评论为什么您认为这是无用的。我和将来可能会提出这个问题的人可能会从您的观点中学到一两件事。

4

3 回答 3

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||'
于 2013-10-08T09:08:42.303 回答
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)
于 2013-10-08T09:15:05.070 回答
0

我不完全理解你想要做什么,我不知道有任何核心库可以做你想做的事情......如果你想让它非常高效,你当然可以用 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 倍......但似乎并非如此

于 2013-10-08T09:06:40.360 回答