0

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 ???

khue bui
  • 1,366
  • 3
  • 22
  • 30
  • 1
    Does this only have to solve this specific problem or are you going to use repurpose this algorithm? – EDD Dec 22 '16 at 20:28

4 Answers4

6
new = old[n:] + old[:n]

You append the front part of the list to the back part. Can you finish after that hint? n is your weekday.

Prune
  • 76,765
  • 14
  • 60
  • 81
2

Did you try the following? I suspect it is not the most efficient way to get what you desire, but it certainly is a way to get it.

list_week_day[today.weekday() : ] + list_week_day[ : today.weekday()]
ilim
  • 4,477
  • 7
  • 27
  • 46
2

Could also try:

wday = 3
[(x + wday) % 7 for x in list_week_day]

# [3, 4, 5, 6, 0, 1, 2]
Psidom
  • 209,562
  • 33
  • 339
  • 356
1

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])
Richard
  • 56,349
  • 34
  • 180
  • 251
  • 2
    numpy's a pretty heavy dependency for something that can be done with vanilla lists... – nrlakin Dec 22 '16 at 20:29
  • @nrlakin: It's a good point. Though I think the clarity of the syntax is a definite plus, so it's worth thinking about if you'll be using numpy anyway. – Richard Dec 22 '16 at 20:35
  • 1
    Yeah, I think that's the key. If it's running on a machine with an existing numpy installation, it's a nice solution. – nrlakin Dec 22 '16 at 20:46