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

如何对格式为%H:%M:%S.f的时间列表执行秒级操作?

Hey there! Let's break down how to handle second-level operations on your time string list. Since your timestamps follow the %H:%M:%S.f format, the first step is converting them into a format we can work with natively (I’ll use Python for examples here, as it’s widely used for such time-processing tasks).

1. Convert timestamps to datetime objects (the foundation)

Before any second-level operations, we need to turn those strings into datetime objects—this lets us leverage built-in time manipulation tools:

from datetime import datetime

time_list = ['13:48:25.195', '13:48:25.994', '13:48:26.995', ...]
# Convert each string timestamp to a datetime object
dt_list = [datetime.strptime(timestamp, '%H:%M:%S.%f') for timestamp in time_list]

2. Calculate second-level time differences

If you need to measure gaps between timestamps or get relative seconds from a starting point:

# Get time differences between consecutive timestamps (in seconds, including milliseconds)
consecutive_diffs = [(dt_list[i] - dt_list[i-1]).total_seconds() for i in range(1, len(dt_list))]

# Calculate seconds elapsed since the first timestamp in the list
relative_seconds = [(dt - dt_list[0]).total_seconds() for dt in dt_list]

The total_seconds() method returns a float, so you’ll retain precision down to the millisecond from your original timestamps.

3. Round or truncate timestamps to whole seconds

If you need to strip milliseconds or round to the nearest full second:

# Truncate to whole seconds (drop all millisecond data)
truncated_to_seconds = [dt.replace(microsecond=0) for dt in dt_list]

# Round to the nearest whole second (round up if >=500ms, down otherwise)
rounded_to_seconds = []
for dt in dt_list:
    if dt.microsecond >= 500000:
        rounded = dt + datetime.timedelta(seconds=1)
        rounded = rounded.replace(microsecond=0)
    else:
        rounded = dt.replace(microsecond=0)
    rounded_to_seconds.append(rounded)

4. Filter timestamps by specific second values

Need to pull out all timestamps that fall within a certain second (or range of seconds)?

# Filter all timestamps where the second value is 25 (ignoring milliseconds)
filtered_second_25 = [timestamp for timestamp, dt in zip(time_list, dt_list) if dt.second == 25]

# Filter timestamps between the 26th and 30th second of their minute
filtered_second_range = [timestamp for timestamp, dt in zip(time_list, dt_list) if 26 <= dt.second <= 30]

5. Group timestamps by whole seconds

If you want to cluster all timestamps that belong to the same full second:

from collections import defaultdict

# Group timestamps by their (hour, minute, second) key
grouped_by_second = defaultdict(list)
for timestamp, dt in zip(time_list, dt_list):
    key = (dt.hour, dt.minute, dt.second)
    grouped_by_second[key].append(timestamp)

# Example: Retrieve all timestamps from 13:48:25
print(grouped_by_second[(13, 48, 25)])

If you have a specific second-level task in mind (like counting records per second, calculating total duration, etc.), feel free to refine the ask—these are just the most common starting points!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 10:05:23