从某个索引开始遍历列表

时间:2018-12-28 03:29:54

标签: python arrays list iterator iteration

这里是一个例子:

list_ = [5, 'cat', 0xDEADBEEF, 4.0]

for offset in range(len(list_)):
    result = 0
    for elem in list_[offset:]:
        result = func(result, elem)
    return result

其中func是不可交换的。

在上面的代码中,list_[offset:]将创建一个新列表,但是我只需要查看list_。我该如何优化呢?

2 个答案:

答案 0 :(得分:1)

要复制切片,但每次迭代需要O(1)次,可以将collections.dequepopleft一起使用:

from collections import deque

dq = deque(list_)

for i in range(len(dq)):
    print(dq)
    dq.popleft()

结果:

deque([5, 'cat', 3735928559, 4.0])
deque(['cat', 3735928559, 4.0])
deque([3735928559, 4.0])
deque([4.0])

这应该比列表切片更有效:请参见deque.popleft() and list.pop(0). Is there performance difference?。还请注意列表切片在O( k )时间中进行,其中 k 是切片的长度。

答案 1 :(得分:0)

使用@jpp的答案所建议的collections.deque有时有时会更快。。 slice和deque解决方案的性能相似,并且比例如使用itertools.islice或仅使用list_上的普通索引。

我尝试使版本总体上大致相同,并使用了一个虚拟func来计数循环:

from __future__ import print_function
from collections import deque
from itertools import islice
from timeit import repeat

import numpy as np


list_ = [5, 'cat', 0xDEADBEEF, 4.0]
list_3k = list_ * 3000


def func(x, y):
    return x + 1


def f1():
    """list slice"""
    result = 0
    for offset in range(len(list_)):
        for elem in list_[offset:]:
            result = func(result, elem)
    return result


def f2():
    """deque"""
    dq = deque(list_)
    result = 0
    for i in range(len(dq)):
        for elem in dq:
            result = func(result, elem)
        dq.popleft()
    return result


def f3():
    """itertools slice"""
    result = 0
    for offset in range(len(list_)):
        for elem in islice(list_, offset, None):
            result = func(result, elem)
    return result


def f4():
    """basics"""
    result = 0
    n = len(list_)
    for offset in range(n):
        j = offset
        while j < n:
            result = func(result, list_[j])
            j += 1
    return result


def timeit(fn, number):
    print("{}: {} loops".format(fn.__name__, fn()))
    times = repeat(fn, repeat=3, number=number)
    print("{:.3f}s ± {:.3f}ms".format(np.mean(times), np.std(times)*1000))


if __name__ == "__main__":
    fs = [f1, f2, f3, f4]

    for f in fs:
        timeit(f, number=1000000)

    list_ = list_3k
    print()

    for f in fs:
        timeit(f, number=3)

结果:

bash-3.2$ python3 foo.py 
f1: 10 loops
2.161s ± 9.333ms
f2: 10 loops
2.134s ± 5.127ms
f3: 10 loops
2.340s ± 11.928ms
f4: 10 loops
2.315s ± 4.615ms

f1: 72006000 loops
23.073s ± 109.857ms
f2: 72006000 loops
23.495s ± 596.822ms
f3: 72006000 loops
24.432s ± 553.167ms
f4: 72006000 loops
40.509s ± 128.367ms