The first problem is how to efficiently cycle through the weekdays. A clean solution is to use itertools.cycle()
.
The following will generate an infinite sequence of repeating weekdays.
import itertools
weekdays = ['Sat','Sun','Mon','Tue','Wed','Thu','Fri']
itertools.cycle(weekdays)
The zip()
function will stop when the shortest input iterable is exhausted. Now that we have an infinite sequence of weekdays, the shortest input will be the eight thousand lines of data.
>>> import itertools
>>> weekdays = ['Sat','Sun','Mon','Tue','Wed','Thu','Fri']
>>> data = [(2000, 1, 1, 1, 336), (2000, 1, 1, 2, 335), (2000, 1, 1, 1, 334),
(2000, 1, 1, 2, 333), (2000, 1, 1, 1, 332), (2000, 1, 1, 2, 331),
(2000, 1, 1, 1, 330), (2000, 1, 1, 2, 329), (2000, 1, 1, 1, 328),
(2000, 1, 1, 2, 327)]
>>> list(zip(data, itertools.cycle(weekdays)))
[((2000, 1, 1, 1, 336), 'Sat'), ((2000, 1, 1, 2, 335), 'Sun'),
((2000, 1, 1, 1, 334), 'Mon'), ((2000, 1, 1, 2, 333), 'Tue'),
((2000, 1, 1, 1, 332), 'Wed'), ((2000, 1, 1, 2, 331), 'Thu'),
((2000, 1, 1, 1, 330), 'Fri'), ((2000, 1, 1, 2, 329), 'Sat'),
((2000, 1, 1, 1, 328), 'Sun'), ((2000, 1, 1, 2, 327), 'Mon')]
Lastly, to get the weekday inside the inner tuples, we can create new tuples within a list comprehension.
>>> [t + (day,) for t, day in zip(data, itertools.cycle(weekdays))]
[(2000, 1, 1, 1, 336, 'Sat'), (2000, 1, 1, 2, 335, 'Sun'),
(2000, 1, 1, 1, 334, 'Mon'), (2000, 1, 1, 2, 333, 'Tue'),
(2000, 1, 1, 1, 332, 'Wed'), (2000, 1, 1, 2, 331, 'Thu'),
(2000, 1, 1, 1, 330, 'Fri'), (2000, 1, 1, 2, 329, 'Sat'),
(2000, 1, 1, 1, 328, 'Sun'), (2000, 1, 1, 2, 327, 'Mon')]
Here, t
is the tuple associated with each line of data, day
is the weekday, and the syntax t + (day,)
creates a new tuple formed by combining the data tuple with the weekday.