Python - 列表映射

发布于 2024-09-28 03:39:12 字数 1559 浏览 1 评论 0原文

我有几个列表想要一起映射,但我无法完全弄清楚如何去做。

我正在抓取赛马结果的实时直播。该提要仅列出一次路线/时间以及三匹马及其位置(前三名)或四匹马和空白(即“”)位置(如果比赛被放弃)。这些是我的清单:

course, time, abandoned, horses, position

清单按顺序排列。

coursetimeabandoned 都具有完全相同数量的元素(abandoned 是布尔值列表,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”获得第一名,“HorseA 2”获得第二名,“HorseA 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) ... ]

我不知道该怎么做,有建议吗?

干杯,

皮特

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和影子一齐双人舞 2024-10-05 03:39:12
>>> 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', '')
>>> 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', '')
无名指的心愿 2024-10-05 03:39:12

您想要使用 zip() 函数这。

如果没有更多关于的外观信息,我实际上无法给您一个例子。是[H1, h2, h3, "", h5, h6, h7, h8, h9, h10, ""]吗?

首先,您需要压缩相同长度的项目:

races = zip(course, time, abandoned)

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

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

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.

欲拥i 2024-10-05 03:39:12
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)

产量

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文