Python Forum
Start loop from different points
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Start loop from different points
#1
Hi everybody,

I am new to python and have been coming up with different little projects to develop, and currently working of a shift calculator.

The program looks at 10 teams which work a ten week shift pattern.

I have managed to write most of the code, but was wondering is there a way that I can enter a list at any point and produce a cycle of all the elements??

For example the program works out that Team 1(T1) is starting the shift pattern on week 1 so the output will be:
T1 T2 T3 T4 T5 T6 T7 T8 T9 T10

The following week it works out that Team 2(T2) is now starting the shift pattern on week 1 so the output will be:
T2 T3 T4 T5 T6 T7 T8 T9 T10 T1

and so on.

Would the loop method be best or is there another way I could generate the same required outcome??

Thank you
Reply
#2
you could use collections deque rotate method
from collections import deque

NORMAL_SHIFT = ('T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10')
shifts = deque(NORMAL_SHIFT)

print(f'week one: {",".join(shifts)}')
shifts.rotate(-1)
print(f'week two: {",".join(shifts)}')
shifts = deque(NORMAL_SHIFT)
shifts.rotate(-4)
print(f'week five: {",".join(shifts)}')
Output:
week one: T1,T2,T3,T4,T5,T6,T7,T8,T9,T10 week two: T2,T3,T4,T5,T6,T7,T8,T9,T10,T1 week five: T5,T6,T7,T8,T9,T10,T1,T2,T3,T4
TheHolyPyGrenade likes this post
Reply
#3
Here's another way to go about it:

shifts = ['T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10']
 
print(f'week one: {shifts}')
shifts = shifts [1:] + shifts [:1]
print(f'week two: {shifts}')
shifts = shifts [1:] + shifts [:1]
print(f'week three: {shifts}')
Output:
week one: ['T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10'] week two: ['T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10', 'T1'] week three: ['T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10', 'T1', 'T2']
TheHolyPyGrenade likes this post
Reply
#4
Thank you Thumbs Up
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop do not start for letter matching algoritm phob0s 3 2,174 Jul-29-2019, 04:31 PM
Last Post: ThomasL
  What's the difference b/w assigning start=None and start=" " Madara 1 2,277 Aug-06-2018, 08:23 AM
Last Post: buran
  Can I Control loop with Keyboad key (start/stop) Lyperion 2 3,272 Jul-28-2018, 10:19 AM
Last Post: Lyperion
  Python- Help with try: input() except ValueError: Loop code to start of sequence Aldi 2 6,197 Mar-08-2018, 03:46 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020