Python - 列表映射
我有几个列表想要一起映射,但我无法完全弄清楚如何去做。
我正在抓取赛马结果的实时直播。该提要仅列出一次路线/时间以及三匹马及其位置(前三名)或四匹马和空白(即“”)位置(如果比赛被放弃)。这些是我的清单:
course, time, abandoned, horses, position
清单按顺序排列。
course
、time
和 abandoned
都具有完全相同数量的元素(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要使用
zip()
函数这。如果没有更多关于
马
的外观信息,我实际上无法给您一个例子。是[H1, h2, h3, "", h5, h6, h7, h8, h9, h10, ""]
吗?首先,您需要压缩相同长度的项目:
然后(取决于您不清楚的马匹结构)您将需要使用列表理解将一个比赛项目附加到每匹马结果中。首先将马匹列表划分为
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:
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.
产量
yields