Skip to content

Order list number from given number

An answer to this question on Stack Overflow.

Question

In detail, ex: I have a list weekday:

list_week_day = [0,2,4,5,6]

if today.weekday() = 3 , then order list_week_day = [4,5,6,0,2]

So, how to do that ???

Answer

As suggested here, you can use numpy's roll command, choosing a suitable value to roll by:

>>> import numpy
>>> a=numpy.arange(1,10) #Generate some data
>>> numpy.roll(a,1)
array([9, 1, 2, 3, 4, 5, 6, 7, 8])
>>> numpy.roll(a,-1)
array([2, 3, 4, 5, 6, 7, 8, 9, 1])
>>> numpy.roll(a,5)
array([5, 6, 7, 8, 9, 1, 2, 3, 4])
>>> numpy.roll(a,9)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])