1

我有一个字符串列表,我需要组合一些项目。要组合的每个项目都按连续顺序列出,如下所示:

info = [u'1', u'be/4', u'root', u'0.00', u'B', u'0.00', u'B', u'0.00', u'%', u'0.00', u'%', u'init']

info[3] = info[3] + info[4]

info.pop(4)

info[4] = info[4] + info[5]

info.pop(5)

info[5] = info[5] + info[6]

info.pop(6)

info[6] = info[6] + info[7]

info.pop(7)

>>> info
[u'1', u'be/4', u'root', u'0.00B', u'0.00B', u'0.00%', u'0.00%', u'init']

最后一行是所需的输出,但是,我想获得一些更好的方法,而不需要所有的弹出和分配?

更新:哇!这些都很棒,我想对它们进行测试和计时;)

4

4 回答 4

2

一种可能的解决方案

>>> info[:3] + [''.join(e) for e in zip(info[3::2],info[4::2])] + info[-1:]
[u'1', u'be/4', u'root', u'0.00B', u'0.00B', u'0.00%', u'0.00%', u'init']
于 2012-12-13T18:36:52.537 回答
2

解决此类问题的一个好方法是使用生成器函数,即您正在处理一系列项目并希望创建一个新序列。我认为这个会起作用:

def combine_items(seq):
    it = iter(seq) # get an iterator

    yield next(it) # yield the first three items unmodified
    yield next(it)
    yield next(it)

    while True: # run until there's an exception
        first = next(it) # grab the first item in a pair
        try:
            second = next(it) # grab the second
            yield first + second # yield the combined pair
        except StopIteration: # if the second was off the end of the sequence
            yield first  # yield the first item unmodified
            raise # and reraise the exception to exit

循环一直运行,while直到序列被完全消耗。当没有更多项目时,next引发StopIteration我们将传播出去的 a,因为这是生成器完成的信号。

要在列表中获取输出,只需在构造函数中包装对生成器的调用list

>>> list(combine_items(info))
['1', 'be/4', 'root', '0.00B', '0.00B', '0.00%', '0.00%', 'init']
于 2012-12-13T18:37:24.687 回答
0

好吧,一个相当简单的方法就是:

>>> lst[:] = lst[:3] + [lst[3] + lst[4], lst[5] + lst[6], lst[7] + lst[8], lst[9] + lst[10], lst[11]] + [lst[11]]
[u'1', u'be/4', u'root', u'0.00B', u'0.00B', u'0.00%', u'0.00%', u'init']

这只是从现有列表中构造一个新列表,然后成为lst该新列表。

可以编写一些处理配对的时髦函数......

为了更清楚,您可以将代码格式化为,

lst[:] = (
    lst[:3] +
    [
        lst[3] + lst[4],
        lst[5] + lst[6],
        lst[7] + lst[8],
        lst[9] + lst[10]
    ] +
    [lst[11]]
)

这使得切片、配对和结尾的位置相当清楚......

移动单个字符字段但未经过广泛测试的另一个选项:

>>> text = '\t'.join(lst)
>>> re.sub('\t(.)\t', r'\1\t', text).split('\t') # or use ([B%]) instead maybe
[u'1', u'be/4', u'root', u'0.00B', u'0.00B', u'0.00%', u'0.00%', u'init']
于 2012-12-13T18:35:01.823 回答
0

怎么样

>>> to_join = iter(info[3:-1])

>>> info[:3] + [x + next(to_join) for x in to_join] + info[-1:]
[u'1', u'be/4', u'root', u'0.00B', u'0.00B', u'0.00%', u'0.00%', u'init']
于 2012-12-13T18:36:33.050 回答