I have several lists that I would like to map together, but I can't quite work my head around how to do it.
I am scraping a live feed of Horse Racing results. The feed only lists the course/time once and three horses and their positions (top three) OR four horses and blank (i.e. "") positions IF the race was abandoned. These are the lists I have:
course, time, abandoned, horses, position
The lists are in order.
course, time and abandoned all have exactly the same number of elements (abandoned is a list of booleans, True meaning that race was abandoned).
horses is a list of the (3 * number of non abandoned races) + (4 * number of abandoned races) horses.
position is a list of the place positions of the horses. If a race was abandoned the position will be "", otherwise it is "1", "2", "3" (strings!).
Example Lists:
Where no race was abandoned
course = ["Course A", "Course A", "Course B"] #there were two races at course A
times = ["00:00", "01:00", "15:00"] #Race 1 at Course A was at 00:00, race 2 at course A was at 01:00
horses = ["HorseA 1", "HorseA 2", "HorseA 3", "HorseA 4", "HorseA 5", "HorseA 6", "HorseB 1", "HorseB 2", "HorseB 3"] #There are three horses per race
positions = ["1","2","3","1","2","3","1","2","3"]
So, at Course A in the race at 00:00 "HorseA 1" came 1st, "HorseA 2" came 2nd and "HorseA 3" came 3rd.
Where there was an abandoned race
courses = ["CourseX", "CourseX", "CourseY"]
times = ["01:00", "02:00", "01:00"]
abandoned = [False, False, True]
horses = ["X1", "X2", "X3", "X4", "X5", "X6", "Y1", "Y2", "Y3", "Y4"]
positions = ["1","2","3","1","2","3","","","",""]
So, there were two races at CourseX but the race at CourseY was abandoned.
What I want to end up with is a list of tuples like so:
[(A Race Course, 00:00, False, Horsey, 1), (A Race Course, 00:00, False, Horsey 2, 2) ... ]
I'm not sure how I can do so, suggestions?
Cheers,
Pete
>>> class s:
courses = ["CourseX", "CourseX", "CourseY"]
times = ["01:00", "02:00", "01:00"]
abandoned = [False, False, True]
horses = ["X1", "X2", "X3", "X4", "X5", "X6", "Y1", "Y2", "Y3", "Y4"]
positions = ["1","2","3","1","2","3","","","",""]
>>> def races(courses, times, abandoned, horses, positions):
z = zip(horses, positions)
for course, time, stopped in zip(courses, times, abandoned):
for _ in range(4 if stopped else 3):
horse, pos = next(z)
yield course, time, stopped, horse, pos
>>> print(*races(s.courses, s.times, s.abandoned, s.horses, s.positions), sep='\n')
('CourseX', '01:00', False, 'X1', '1')
('CourseX', '01:00', False, 'X2', '2')
('CourseX', '01:00', False, 'X3', '3')
('CourseX', '02:00', False, 'X4', '1')
('CourseX', '02:00', False, 'X5', '2')
('CourseX', '02:00', False, 'X6', '3')
('CourseY', '01:00', True, 'Y1', '')
('CourseY', '01:00', True, 'Y2', '')
('CourseY', '01:00', True, 'Y3', '')
('CourseY', '01:00', True, 'Y4', '')
You want to use the zip() function for this.
I can't actually give you an example without a little more info about what horses looks like. Is it [H1, h2, h3, "", h5, h6, h7, h8, h9, h10, ""]?
To get you started, you'll want to zip the items which are the same length:
races = zip(course, time, abandoned)
Then (depending on your unclear horses structure) you'll want to use a list comprehension to append one race item to each horse result. It might be easier for you to partition the horses list into a horses_in_race list first, and then use that with your zip and a list comp.
If the question was more complete, I could provide a better answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With