简体   繁体   English

Python:在到达结尾时循环切换列表中的元素

[英]Python: cycle through elements in list reversing when the end is reached

I have a list that looks like: 我有一个看起来像这样的列表:

a = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10']

I need to cycle through this list one element at a time but when the end of the list is reached, the cycle needs to be reversed . 我需要一次循环遍历此列表中的一个元素,但是当到达列表的末尾时,需要反转循环。

For example, using itertools.cycle : 例如,使用itertools.cycle

from itertools import cycle
a_cycle = cycle(a)
for _ in range(30):
    print a_cycle.next()

I get: 我明白了:

01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10

but what I need is: 但我需要的是:

01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 10, 09, 08, 07, 06, 05, 04, 03, 02, 01, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10

I need to cycle through a for a fixed number of times, say 200. 我需要循环通过a固定数量的时间,比如200。

You can cycle the chain of your a and reversed a , eg: 你可以cyclechain您的areversed a ,如:

from itertools import cycle, islice, chain

a = range(1, 11)
b = reversed(a)
c = cycle(chain(a, b))
d = list(islice(c, 100)) # `c` is infinite - hence the `islice` to stop at some point...

Which gives you: 哪个给你:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Note : If a is an exhaustable iterator, you will need to make a copy of a first. 注意 :如果a是exhaustable迭代器,你将需要的副本a第一。 But given your example, this will be fine. 但是举个例子,这很好。

Do you actually need to cycle through the list, as in go forward and backwards forever? 你是否真的需要在列表中循环 ,如前进和后退永远? Or just .reverse() the list? 或者只是.reverse()列表?

print a + a[::-1]

Will do what you describe. 会做你所描述的。 The reversed() built-in also works, but you need to chain() it, as it returns an iterator, eg: reversed()内置也可以,但你需要chain()它,因为它返回一个迭代器,例如:

print list(itertools.chain(a, reversed(a)))

You can call itertools.cycle() on either result to get an infinite iterator of the list concatenated with its reverse. 您可以对任一结果调用itertools.cycle()以获得与其反向连接的列表的无限迭代器。

def forwardback(lst):
    tot = len(lst)
    while 1:
        for i in xrange(tot):
            yield lst[i]
        for i in xrange(tot-1,-1,-1):
            yield lst[i]

or (using cycle 's approach, which works for all iterators) 或(使用cycle方法,适用于所有迭代器)

def forwardback(lst):
    saved = []
    for elem in lst:
        yield elem
        saved.append(elem)
    while saved:
        for elem in reversed(saved):
            yield elem
        for elem in saved:
            yield elem

Make a copy of list a, reverse it, then append it. 复制列表a,反转它,然后追加它。

a = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
b = a[:]
b.reverse()
a = a + b

or based on a comment suggestion. 或基于评论建议。

a = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
b = a[::-1]
a = a + b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM