Python - 列表映射

时间:2010-10-19 16:32:15

标签: python map list-comprehension tuples

我有几个列表,我想要一起映射,但我不能完全了解如何做到这一点。

我正在抓一个赛马结果的现场直播。如果比赛被放弃,饲料仅列出一次和三匹马及其位置(前三名)或四匹马和空白(即“”)位置的饲料/时间。这些是我的名单:

course, time, abandoned, horses, position

列表是有序的。

coursetimeabandoned都有完全相同数量的元素(弃用是布尔列表,True表示种族被放弃)。

horses是(3 *未弃置的种族数)+(4 *被遗弃的种族数)马的列表。

position是马匹的位置列表。如果比赛被放弃,则位置为“”,否则为“1”,“2”,“3”(字符串!)。

示例列表:

没有放弃种族

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"]

因此,在00:00比赛的A球场,“HorseA 1”排在第1位,“HorseA 2”排在第2位,“HorseA 3”排在第3位。

哪里有遗弃的种族

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","","","",""]

所以,在CourseX举行了两场比赛,但是在CourseY的比赛被放弃了。

我最终想要的是一个像这样的元组列表:

[(A Race Course, 00:00, False, Horsey, 1), (A Race Course, 00:00, False, Horsey 2, 2) ... ]

我不确定我怎么能这样做,建议?

干杯,

皮特

3 个答案:

答案 0 :(得分:5)

>>> 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', '')

答案 1 :(得分:2)

您想使用zip() function来实现此目的。

如果没有关于horses看起来像什么的更多信息,我实际上无法给你一个例子。是[H1, h2, h3, "", h5, h6, h7, h8, h9, h10, ""]吗?

为了让您入门,您需要压缩长度相同的项目:

races = zip(course, time, abandoned)

然后(取决于你不清楚的马匹结构)你会想要使用列表理解来为每个马匹结果附加一个种族项目。您可能更容易将马匹列表首先划分为horses_in_race列表,然后将其与您的zip和列表组合一起使用。

如果问题更完整,我可以提供更好的答案。

答案 2 :(得分:2)

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)

产量

  

[('A Race Course','00:00',False,   'Horsey-0','1'),('A Race Course',   '00:00',错误,'Horsey-1','2'),('A   赛马场','00:00',错误,   'Horsey-2','3'),('B赛马场',   '01:00',是的,'Horsey-3',''),''B   赛马场','01:00',是的,   'Horsey-4',''),('B赛马场',   '01:00',是的,'Horsey-5',''),''B   赛马场','01:00',是的,   'Horsey-6','')]