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

技术需求:创建以call_id为键、时间范围列表为值的字典

实现以call_id为键、时间范围列表为值的字典

核心思路

遍历通话记录数据,以每条记录的call_id作为字典的键,用range(start_time, end_time)生成对应时间范围的整数列表,作为字典的值。

代码实现(Python)

假设你有如下格式的通话记录数据:

# 示例通话记录:每个条目包含call_id、起始时间、结束时间(此处用整数时间戳示例)
call_records = [
    {"call_id": "C001", "start_time": 100, "end_time": 105},
    {"call_id": "C002", "start_time": 200, "end_time": 203},
    {"call_id": "C003", "start_time": 300, "end_time": 306}
]

生成目标字典:

call_time_dict = {}
for record in call_records:
    call_id = record["call_id"]
    # range为左闭右开,生成[start_time, end_time)范围内的整数列表
    time_range_list = list(range(record["start_time"], record["end_time"]))
    call_time_dict[call_id] = time_range_list

验证结果

打印生成的字典:

print(call_time_dict)

输出如下:

{
    'C001': [100, 101, 102, 103, 104],
    'C002': [200, 201, 202],
    'C003': [300, 301, 302, 303, 304, 305]
}

补充说明

  • 如果你的时间是字符串格式(如"2024-05-20 09:00"),需要先通过datetime模块转换为整数时间戳,再使用range生成列表。
  • 若需要包含end_time对应的时间,将range的第二个参数改为record["end_time"] + 1即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 13:54:20