在Python中转移和合并列表元素的最有效方法(2048)

时间:2014-04-09 17:48:15

标签: python algorithm optimization python-3.x

我有一个基本上正确对齐列表的函数,但也将两个相等的元素合并为一个(列表将始终至少有一个元素):

def shift(sequence):
    for i in range(len(sequence)-1):
        current_value = sequence[i]
        next_value = sequence[i+1]
        if next_value == 0:
            sequence[i], sequence[i+1] = 0, current_value
        elif current_value == next_value:
            sequence[i], sequence[i+1] = 0, current_value*2
    return sequence

这是输入和输出的一些示例:

>>> shift([0, 0, 1, 0])
[0, 0, 0, 1]
>>> shift([1, 1, 0, 0])
[0, 0, 0, 2]
>>> shift([2, 0, 1, 0])
[0, 0, 2, 1]

最有效的方法是什么?如果对矩阵中的每一行进行此操作,那么是否有更有效的方法:

matrix = [shift(row) for row in matrix]

另外,如果我要在其他三个方向上移动矩阵(除了右边),是否有比这三个更有效的方法:

#Left
matrix = [shift(row[::-1])[::-1] for row in matrix]

#Down
matrix = map(list, zip(*[shift(row) for row in map(list, zip(*matrix))]))

#Up
matrix = map(list, zip(*[shift(row[::-1])[::-1] for row in map(list, zip(*matrix))]))

如果重复进行这些移位操作(以及每次更改矩阵中的一个值),我是否应该跟踪哪些内容以提高效率?

修改

我的功能并不总是有效:

>>> shift([1, 1, 1, 1])
[0, 2, 0, 2]

输出应为:

[0, 0, 2, 2]

更多预期的投入和产出:

[1, 1, 1]             --> [0, 1, 2]
[1, 2, 2, 3, 5, 5, 2] --> [0, 0, 1, 4, 3, 10, 2]

编辑2

它不必向右移动,也可以是另一种方式。

2 个答案:

答案 0 :(得分:1)

这是否更有效取决于timeit

def streaming_sum(sequence):
    values = reversed(sequence)
    last = next(values)
    for value in values:
        if value == last:
            yield last + value
            last = 0
        else:
            yield last
            last = value
    yield last


def shift(sequence):
    length = len(sequence)
    reduced = list(reversed(filter(None, streaming_sum(sequence))))
    return [0] * (length - len(reduced)) + reduced


for sequence, expected in [
    ([0, 0, 1, 0], [0, 0, 0, 1]),
    ([1, 1, 0, 0], [0, 0, 0, 2]),
    ([2, 0, 1, 0], [0, 0, 2, 1]),
    ([1, 1, 1, 1], [0, 0, 2, 2]),
    ([1, 1, 1], [0, 1, 2]),
    ([1, 2, 2, 3, 5, 5, 2], [0, 0, 1, 4, 3, 10, 2]),
]:
    actual = shift(sequence)
    assert actual == expected, (actual, expected)

答案 1 :(得分:0)

这是我目前的解决方案。它比Kirk Strauser的解决方案快了大约60%。

def shift(self, sequence, right=False):
    if right:
        sequence = sequence[::-1]
    values = []
    empty = 0
    for n in sequence:
        if values and n == values[-1]:
            values[-1] = 2*n
            empty += 1
        elif n:
            values.append(n)
        else:
            empty += 1
    values += [0]*empty
    if right:
        values = values[::-1]
    return values

我的效率更高:

def shift2(length, sequence, right=False):
    if right:
        sequence = sequence[::-1]
    values = [0]*length
    full = 0
    for n in sequence:
        if full and n == values[full]:
            values[full] = 2*n
        elif n:
            values[full] = n
            full += 1
    if right:
        values = values[::-1]
    return values

Kirk的解决方案:

def streaming_sum(sequence):
    values = reversed(sequence)
    last = next(values)
    for value in values:
        if value == last:
            yield last + value
            last = 0
        else:
            yield last
            last = value
    yield last

def shift2(sequence):
    length = len(sequence)
    reduced = list(reversed(list(filter(None, streaming_sum(sequence)))))
    return [0] * (length - len(reduced)) + reduced

我对Kirk功能的改进(40%加速):

def shift3(sequence):
    length = len(sequence)
    reduced = [n for n in filter(None, streaming_sum(sequence))][::-1]
    return [0] * (length - len(reduced)) + reduced

定时:

from timeit import Timer

tests = [[1000, 1000, 1000, 1000],
         [1000, 0, 0, 1000],
         [0, 1000],
         [1000, 1000, 0, 500, 0, 500, 1000, 0, 0, 100, 100, 100]]
t1, t2, t3 = 0, 0, 0

for test in tests:
    t1 += Timer(lambda: shift(test)).timeit()
    t2 += Timer(lambda: shift2(test)).timeit()
    t3 += Timer(lambda: shift3(test)).timeit()

>>> print(t1, t2, t3)
10.706327316242147 26.92895738572211 16.65189852514444

我的一个对付我的效率更高效的时间而没有右对齐而不是左对齐的选项:

10.502816527107633 8.503653343656246 8.15101370397509