Python Forum

Full Version: Start loop from different points
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
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']
Thank you Thumbs Up