Cycle through list starting at a certain element

自作多情 提交于 2019-11-30 04:48:42

Look at itertools module. It provides all the necessary functionality.

from itertools import cycle, islice, dropwhile

L = [1, 2, 3, 4]

cycled = cycle(L)  # cycle thorugh the list 'L'
skipped = dropwhile(lambda x: x != 4, cycled)  # drop the values until x==4
sliced = islice(skipped, None, 10)  # take the first 10 values

result = list(sliced)  # create a list from iterator
print(result)

Output:

[4, 1, 2, 3, 4, 1, 2, 3, 4, 1]
0605002

Use the arithmetic mod operator. Suppose you're starting from position k, then k should be updated like this:

k = (k + 1) % len(l)

If you want to start from a certain element, not index, you can always look it up like k = l.index(x) where x is the desired item.

I'm not such a big fan of importing modules when you can do things by your own in a couple of lines. Here's my solution without imports:

def cycle(my_list, start_at=None):
    start_at = 0 if start_at is None else my_list.index(start_at)
    while True:
        yield my_list[start_at]
        start_at = (start_at + 1) % len(my_list)

This will return an (infinite) iterator looping your list. To get the next element in the cycle you must use the next statement:

>>> it1 = cycle([101,102,103,104])
>>> next(it1), next(it1), next(it1), next(it1), next(it1)
(101, 102, 103, 104, 101) # and so on ...
>>> it1 = cycle([101,102,103,104], start_at=103)
>>> next(it1), next(it1), next(it1), next(it1), next(it1)
(103, 104, 101, 102, 103) # and so on ...
import itertools as it
l = [1, 2, 3, 4]
list(it.islice(it.dropwhile(lambda x: x != 4, it.cycle(l)),  10))
# returns: [4, 1, 2, 3, 4, 1, 2, 3, 4, 1]

so the iterator you want is:

it.dropwhile(lambda x: x != 4, it.cycle(l))

Hm, http://docs.python.org/library/itertools.html#itertools.cycle doesn't have such a start element.

Maybe you just start the cycle anyway and drop the first elements that you don't like.

Another weird option is that cycling through lists can be accomplished backwards. For instance:

# Run this once
myList = ['foo', 'bar', 'baz', 'boom']
myItem = 'baz'

# Run this repeatedly to cycle through the list
if myItem in myList:
    myItem = myList[myList.index(myItem)-1]
    print myItem
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!