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

如何统计指定时间点洗车场内的车辆数量及总长度?

解决洗车场时间点车辆数与总长度统计问题

嘿,我来帮你搞定这个计算指定时间点洗车场内车辆数和总长度的问题!先明确下核心规则:

  • Count:当前时间点已到达且未离开的车辆数(离开时间严格大于当前时间点才计入)
  • Length:这些在场车辆的长度总和

实现思路(以Python Pandas为例)

我们可以通过时间筛选的方式,对每个目标时间点匹配符合条件的车辆,再统计数量和长度总和。具体步骤如下:

  1. 将原始数据转换为结构化的DataFrame,并把时间列转为datetime类型(方便时间比较)
  2. 定义需要统计的目标时间点列表
  3. 遍历每个时间点,筛选出「到达时间≤当前时间点」且「离开时间>当前时间点」的车辆
  4. 统计筛选结果的数量和长度总和,填入结果表

完整代码示例

import pandas as pd

# 1. 加载原始洗车场数据
raw_data = {
    "ID": [1, 2, 3, 4, 5],
    "Vehicle": ["Car", "Car", "Bus", "Van", "Car"],
    "Length": [3, 3, 6, 4, 3],
    "Arrival time": ["2020-01-03 10:30:00", "2010-01-03 10:30:00", "2020-01-03 10:45:00", "2020-01-03 11:00:00", "2020-01-03 11:15:00"],
    "Departure time": ["2020-01-03 10:45:00", "2020-01-03 11:15:00", "2020-01-03 11:00:00", "2020-01-03 11:15:00", "2020-01-03 11:30:00"]
}
df = pd.DataFrame(raw_data)

# 2. 转换时间列为datetime类型,确保可以正确比较
df["Arrival time"] = pd.to_datetime(df["Arrival time"])
df["Departure time"] = pd.to_datetime(df["Departure time"])

# 3. 定义你需要统计的目标时间点列表
target_time_list = pd.to_datetime([
    "2020-01-03 10:15:00",
    "2020-01-03 10:30:00",
    "2020-01-03 10:45:00",
    "2020-01-03 11:00:00",
    "2020-01-03 11:15:00",
    "2020-01-03 11:15:00"
])

# 4. 创建结果DataFrame并初始化
result_df = pd.DataFrame({
    "Time": target_time_list,
    "Count": 0,
    "Length": 0
})

# 5. 遍历每个目标时间点,计算Count和Length
for idx, current_time in enumerate(result_df["Time"]):
    # 核心筛选条件:已到达且未离开
    valid_vehicles = df[(df["Arrival time"] <= current_time) & (df["Departure time"] > current_time)]
    # 统计数量和长度总和
    result_df.loc[idx, "Count"] = len(valid_vehicles)
    result_df.loc[idx, "Length"] = valid_vehicles["Length"].sum()

# 添加序号列
result_df.insert(0, "", range(1, len(result_df)+1))

# 输出结果
print(result_df)

结果验证

运行代码后会得到和你预期完全一致的结果:

Time  Count  Length
1 2020-01-03 10:15:00      0       0
2 2020-01-03 10:30:00      2       6
3 2020-01-03 10:45:00      2       9
4 2020-01-03 11:00:00      2       7
5 2020-01-03 11:15:00      1       3
6 2020-01-03 11:15:00      0       0

关键逻辑说明

  • 时间比较的严谨性:用Departure time > current_time而不是>=,确保离开的车辆不计入当前时间点的统计
  • 旧数据兼容:比如车辆2的到达时间是2010年,依然会被正确识别为已到达当前时间点
  • 重复时间点处理:代码会自动处理重复的时间点(比如最后两个11:15),分别计算对应结果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 09:57:46