1

我希望能够交错两个长度可能不相等的列表。我所拥有的是:

  def interleave(xs,ys):
    a=xs
    b=ys
    c=a+b
    c[::2]=a
    c[1::2]=b
    return c

这适用于长度相等或仅 +/-1 的列表。但是,如果假设 xs=[1,2,3] 和 ys= ["hi,"bye","no","yes","why"] 出现此消息:

c[::2]=a
ValueError: attempt to assign sequence of size 3 to extended slice of size 4

如何使用索引解决此问题?还是我必须使用 for 循环?编辑:我想要的是让额外的值出现在最后。

4

4 回答 4

7

你可以itertools.izip_longest在这里使用:

>>> from itertools import izip_longest
>>> xs = [1,2,3]
>>> ys = ["hi","bye","no","yes","why"]
>>> s = object()
>>> [y for x in izip_longest(xs, ys, fillvalue=s) for y in x if y is not s]
[1, 'hi', 2, 'bye', 3, 'no', 'yes', 'why']

使用来自itertoolsroundrobin的配方,这里不需要哨兵值:

from itertools import *
def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    # Recipe credited to George Sakkis
    pending = len(iterables)
    nexts = cycle(iter(it).next for it in iterables)
    while pending:
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            pending -= 1
            nexts = cycle(islice(nexts, pending))

演示:

>>> list(roundrobin(xs, ys))
[1, 'hi', 2, 'bye', 3, 'no', 'yes', 'why']
>>> list(roundrobin(ys, xs))
['hi', 1, 'bye', 2, 'no', 3, 'yes', 'why']
于 2013-11-09T22:32:18.280 回答
7

您可以使用heapq.merge

xs = [1, 2, 3]
ys = ['hi', 'bye', 'no', 'yes', 'why']

import heapq
interleaved = [v for i, v in heapq.merge(*[enumerate(el) for el in (xs, ys)])]
# [1, 'hi', 2, 'bye', 3, 'no', 'yes', 'why']

这避免了对哨兵值和展平的需要。

使用轮询配方来更有效地实现这一点,而无需让项目具有可比性。

于 2013-11-09T22:33:35.397 回答
2

好的,这是我的条目:

>>> from itertools import chain
>>> xs = [1,2,3]
>>> ys = ["hi","bye","no","yes","why"]
>>> xi, yi = iter(xs), iter(ys)
>>> list(chain.from_iterable(zip(xi, yi))) + list(xi) + list(yi)
[1, 'hi', 2, 'bye', 3, 'no', 'yes', 'why']

或者,

>>> [i for row in zip(xi, yi) for i in row] + list(xi) + list(yi)

也会起作用(这只是@hcwhsa 使用的扁平化的listcomp 习惯用法)。我的第一个想法是

>>> list(zip(*sorted(list(enumerate(xs)) + list(enumerate(ys)))))[1]
(1, 'hi', 2, 'bye', 3, 'no', 'yes', 'why')

但这只是@Jon Clements 的效率低得多的版本(我使用了低效的排序,他使用了高效的堆队列。)

[我一直在尝试使用cycle工作来获得一些东西,但它似乎并不像我希望的那么容易:事实证明,我只是在努力重新实现 @hcwsha 发布的循环配方,所以有完成那没有意义。:^)]

于 2013-11-09T23:06:14.937 回答
0

保持简单:

def interleave(xs,ys):
    stop = min(len(xs), len(ys))
    suffix = max(xs, ys, key = len)[stop:]
    out = list()
    for pair in zip(xs, ys):
        out.extend(pair)
    out.extend(suffix)
    return out

警告:
Python 2.7
假设列表作为参数传递。

于 2013-11-09T23:20:26.357 回答