views:

111

answers:

3

Hey,

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

+2  A: 

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.

Paul McMillan
why don't you show this to us?
SilentGhost
I can't use zip because they differ in length.
Peter
+3  A: 
def expand(abandoned,seq):
    for was_abandoned,elt in zip(abandoned,seq):
        if was_abandoned:
            for _ in range(4): yield elt
        else:
            for _ in range(3): yield elt            

course=['A Race Course','B Race Course']
time=['00:00','01:00']
abandoned=[False,True]
horses=['Horsey-{0}'.format(n) for n in range(8)]
position=['1','2','3','','','','']

result=[(c,t,a,h,p) for (c,t,a),h,p in
        zip(expand(abandoned,zip(course,time,abandoned)),horses,position)]
print(result)

yields

[('A Race Course', '00:00', False, 'Horsey-0', '1'), ('A Race Course', '00:00', False, 'Horsey-1', '2'), ('A Race Course', '00:00', False, 'Horsey-2', '3'), ('B Race Course', '01:00', True, 'Horsey-3', ''), ('B Race Course', '01:00', True, 'Horsey-4', ''), ('B Race Course', '01:00', True, 'Horsey-5', ''), ('B Race Course', '01:00', True, 'Horsey-6', '')]

unutbu
That's pretty pythonic. You aren't going to come up with a nice 1-liner for this weird `3 or 4 horses` business.
Paul McMillan
I love it when people delete their comments so I look like a raving lunatic.
Paul McMillan
Yeah, the generator is really slick. Well done.
David Eyk
For the record I originally wrote a comment which said "Thanks, could this be more elegant?" but, after looking at the answer again, I realised that it is actually quite neat (a cursory glance made the ugly test data arrays stick in my head).
Peter
+5  A: 
>>> 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', '')
SilentGhost
Oh very nice. The `4 if abandoned else 3` part is slick.
Paul McMillan
Nifty to use print function with * also instead of '\n'.join
Tony Veijalainen
Posts like this are exactly why I read SO.
Robert Rossney