Start index for iterating Python list

前端 未结 8 1943
走了就别回头了
走了就别回头了 2020-12-23 02:52

What is the best way to set a start index when iterating a list in Python. For example, I have a list of the days of the week - Sunday, Monday, Tuesday, ... Saturday - but I

8条回答
  •  半阙折子戏
    2020-12-23 03:11

    stdlib will hook you up son!

    deque.rotate():

    #!/usr/local/bin/python2.7
    
    from collections import deque
    
    a = deque('Monday Tuesday Wednesday Thursday Friday Saturday Sunday'.split(' '))
    a.rotate(3)
    deque(['Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday'])
    

提交回复
热议问题