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

如何对比本地与FTP服务器的日历版本数组获取待更新文件列表

日历版本对比实现方案

核心思路

  • 先将所有日历版本字符串拆分为「日期段」(如2020-2021)和「版本号」(整数格式)两个核心字段
  • 预遍历FTP日历数组,按日期段分组,仅保留每个日期段的最高版本条目,减少后续重复对比开销
  • 结合mostRecentCalender的最高日期规则过滤无效条目,最终输出待更新数组

代码实现(Python)

def parse_cal_string(cal_str):
    # 拆分日历字符串,返回(日期段, 版本号, 原始字符串)
    parts = cal_str.split('-')
    date_part = f"{parts[0]}-{parts[1]}"
    version = int(parts[2])
    return (date_part, version, cal_str)

def get_need_update_cals(local_arr, ftp_arr, mostRecentCalender):
    # 解析最高版本日历的日期段
    most_recent_date = parse_cal_string(mostRecentCalender)[0]
    
    # 1. 预处理FTP数组,得到每个日期段的最高版本
    ftp_highest_map = {}
    for cal in ftp_arr:
        date_part, ver, full_str = parse_cal_string(cal)
        # 同日期段只保留最高版本
        if date_part not in ftp_highest_map or ver > ftp_highest_map[date_part][0]:
            ftp_highest_map[date_part] = (ver, full_str)
    
    # 2. 预处理本地数组,得到每个日期段的最高版本号
    local_highest_map = {}
    for cal in local_arr:
        date_part, ver, _ = parse_cal_string(cal)
        if date_part not in local_highest_map or ver > local_highest_map[date_part]:
            local_highest_map[date_part] = ver
    
    # 3. 按规则筛选待更新条目
    need_update = []
    for date_part, (ftp_ver, ftp_full_str) in ftp_highest_map.items():
        # 规则1:如果是最高日期段,直接加入更新列表
        if date_part == most_recent_date:
            need_update.append(ftp_full_str)
            continue
        # 规则2:本地存在该日期段,且FTP版本更高,加入更新列表
        if date_part in local_highest_map and ftp_ver > local_highest_map[date_part]:
            need_update.append(ftp_full_str)
        # 其余情况:本地不存在且非最高日期段,直接忽略
    return need_update

# 示例测试
if __name__ == "__main__":
    ftp_arr = ["2019-2020-1", "2019-2020-2", "2020-2021-1", "2020-2021-2", "2020-2021-3", "2020-2021-4", "2021-2022-1"]
    local_arr = ["2020-2021-1", "2020-2021-2"]
    mostRecentCalender = "2021-2022-1"
    print(get_need_update_cals(local_arr, ftp_arr, mostRecentCalender))
    # 输出:['2020-2021-4', '2021-2022-1'] 符合预期

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 11:54:03