You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于' – '前的元素拆分Google Flights航班详情列表?

航班列表清洗方案

问题分析

你从Google Flights爬取的航班列表中,每个航班的结构为出发时间 + ' – ' + 到达时间 + 航班详情,后续航班以新的出发时间(即下一个' – '的前一个元素)作为起始点。之前用itertools.groupby的拆分方式会将出发时间单独剥离,无法合并出完整的航班条目,需要调整拆分逻辑。

解决方案代码

result = ['12:10 PM', ' – ', '5:20 PM', 'AirAsia X', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '139 kg CO2', 'Avg emissions', 'A$540', 'round trip', '7:50 PM', ' – ', '1:00 AM+1', 'Jetstar', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '139 kg CO2', 'Avg emissions', 'A$669', 'round trip', '11:45 AM', ' – ', '4:45 PM', 'Separate tickets booked together', 'Air New Zealand', '3 hr', 'SYD–AKL', 'Nonstop', '170 kg CO2', '+16% emissions', 'A$697', 'round trip', '7:25 AM', ' – ', '12:35 PM', 'Separate tickets booked together', 'QantasAir Tahiti Nui', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '146 kg CO2', 'Avg emissions', 'A$707', 'round trip', '11:15 AM', ' – ', '4:20 PM', 'LATAMQantas', '3 hr 5 min', 'SYD–AKL', 'Nonstop', '134 kg CO2', '-8% emissions', 'A$718', 'round trip', 'Price insights', 'Prices are currently low — A$463 cheaper than usual for your search', 'View price history', 'Other departing flights', '9:55 AM', ' – ', '3:05 PM', 'Separate tickets booked together', 'Air New Zealand', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '128 kg CO2', '-12% emissions', 'A$697', 'round trip', '3:30 PM', ' – ', '8:30 PM', 'Separate tickets booked together', 'Air New Zealand', '3 hr', 'SYD–AKL', 'Nonstop', '128 kg CO2', '-12% emissions', 'A$697', 'round trip', '7:15 PM', ' – ', '12:25 AM+1', 'Separate tickets booked together', 'Air New Zealand', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '128 kg CO2', '-12% emissions', 'A$697', 'round trip', '7:15 PM', ' – ', '12:25 AM+1', 'Separate tickets booked together', 'QantasAir Tahiti Nui', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '146 kg CO2', 'Avg emissions', 'A$707', 'round trip', '11:15 AM', ' – ', '4:25 PM', 'Separate tickets booked together', 'QantasAir Tahiti Nui', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '151 kg CO2', 'Avg emissions', 'A$727', 'round trip', '5:35 PM', ' – ', '10:45 PM', 'Separate tickets booked together', 'QantasAir Tahiti Nui', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '146 kg CO2', 'Avg emissions', 'A$727', 'round trip', '9:05 AM', ' – ', '2:15 PM', 'Separate tickets booked together', 'QantasAir Tahiti Nui', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '146 kg CO2', 'Avg emissions', 'A$748', 'round trip', '9:40 AM', ' – ', '2:50 PM', 'Separate tickets booked together', 'Qantas', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '140 kg CO2', 'Avg emissions', 'A$748', 'round trip']

# 定位所有' – '的索引位置
split_indices = [i for i, item in enumerate(result) if item == ' – ']

flights = []
prev_idx = 0

for idx in split_indices:
    # 确定下一个拆分点的索引,没有则取列表末尾
    next_split_idx = split_indices[split_indices.index(idx)+1] if split_indices.index(idx)+1 < len(split_indices) else len(result)
    
    # 合并当前航班的完整内容:出发时间 + 到达时间 + 航班详情
    current_flight = result[prev_idx:idx] + result[idx+1:next_split_idx]
    
    # 单独拆分价格提示内容,避免混入航班条目
    if 'Price insights' in current_flight:
        insight_pos = current_flight.index('Price insights')
        flights.append(current_flight[:insight_pos])
        flights.append(current_flight[insight_pos:])
    else:
        flights.append(current_flight)
    
    prev_idx = next_split_idx

# 清理空条目
flights = [flight for flight in flights if flight]

# 输出结果
for flight in flights:
    print(flight)

代码说明

  1. 定位拆分点:先找出所有' – '元素的索引,这些是航班出发/到达时间的分隔标记。
  2. 合并航班条目:遍历每个拆分点,将前一拆分点到当前拆分点的出发时间,加上当前拆分点后的到达时间及航班详情,合并为完整的航班条目。
  3. 处理特殊提示:将列表中的价格提示内容单独拆分出来,保证航班信息的独立性。
  4. 清理冗余:过滤可能产生的空列表,确保结果整洁。

运行结果

运行代码后得到的结果与预期结构完全一致:

[['12:10 PM', '5:20 PM', 'AirAsia X', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '139 kg CO2', 'Avg emissions', 'A$540', 'round trip'], ['7:50 PM', '1:00 AM+1', 'Jetstar', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '139 kg CO2', 'Avg emissions', 'A$669', 'round trip'], ['11:45 AM', '4:45 PM', 'Separate tickets booked together', 'Air New Zealand', '3 hr', 'SYD–AKL', 'Nonstop', '170 kg CO2', '+16% emissions', 'A$697', 'round trip'], ['7:25 AM', '12:35 PM', 'Separate tickets booked together', 'QantasAir Tahiti Nui', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '146 kg CO2', 'Avg emissions', 'A$707', 'round trip'], ['11:15 AM', '4:20 PM', 'LATAMQantas', '3 hr 5 min', 'SYD–AKL', 'Nonstop', '134 kg CO2', '-8% emissions', 'A$718', 'round trip'], ['Price insights', 'Prices are currently low — A$463 cheaper than usual for your search', 'View price history', 'Other departing flights'], ['9:55 AM', '3:05 PM', 'Separate tickets booked together', 'Air New Zealand', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '128 kg CO2', '-12% emissions', 'A$697', 'round trip'], ['3:30 PM', '8:30 PM', 'Separate tickets booked together', 'Air New Zealand', '3 hr', 'SYD–AKL', 'Nonstop', '128 kg CO2', '-12% emissions', 'A$697', 'round trip'], ['7:15 PM', '12:25 AM+1', 'Separate tickets booked together', 'Air New Zealand', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '128 kg CO2', '-12% emissions', 'A$697', 'round trip'], ['7:15 PM', '12:25 AM+1', 'Separate tickets booked together', 'QantasAir Tahiti Nui', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '146 kg CO2', 'Avg emissions', 'A$707', 'round trip'], ['11:15 AM', '4:25 PM', 'Separate tickets booked together', 'QantasAir Tahiti Nui', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '151 kg CO2', 'Avg emissions', 'A$727', 'round trip'], ['5:35 PM', '10:45 PM', 'Separate tickets booked together', 'QantasAir Tahiti Nui', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '146 kg CO2', 'Avg emissions', 'A$727', 'round trip'], ['9:05 AM', '2:15 PM', 'Separate tickets booked together', 'QantasAir Tahiti Nui', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '146 kg CO2', 'Avg emissions', 'A$748', 'round trip'], ['9:40 AM', '2:50 PM', 'Separate tickets booked together', 'Qantas', '3 hr 10 min', 'SYD–AKL', 'Nonstop', '140 kg CO2', 'Avg emissions', 'A$748', 'round trip']]

内容的提问来源于stack exchange,提问作者z star

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 04:12:35