My data file contains eight thousand lines, where each line is a tuple. The first two lines are:
(2000, 1, 1, 1, 336)
(2000, 1, 1, 2, 335)
I would like to repeatedly cycle through the weekdays and append a weekday to each data tuple. The list of lines should look like:
[(2000, 1, 1, 1, 336, 'Sat'), (2000, 1, 1, 2, 335, 'Sun'), ...]
I've tried different approaches, and have managed to get almost every possible combination except the desired one. I can't quite wrap my head around what I've misunderstood.
Code
weekdays = ['Sat','Sun','Mon','Tue','Wed','Thu','Fri']
a = read_file('VIK_sealevel_2000.txt')
zipp = list(zip(a, weekdays))
print(zipp[0:2])
Output
[((2000, 1, 1, 1, 335), 'Sat'), ((2000, 1, 1, 2, 336), 'Sun')]
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.
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.Lastly, to get the weekday inside the inner tuples, we can create new tuples within a list comprehension.
Here,
t
is the tuple associated with each line of data,day
is the weekday, and the syntaxt + (day,)
creates a new tuple formed by combining the data tuple with the weekday.