分割处理存放连续时间字符串的列表

最近写代码遇到这样的问题:一个列表中存放着多个“时间字符串”,将这个列表按照连续时间进行分割,最终切割成一个个只有开始时间与结束时间的小列表:

# 初始的的列表
lst: ['2019-02-01', '2019-02-02', '2019-02-03', '2020-01-02', '2020-01-03',"2020-01-04","2020-02-01","2020-02-02"]
# 进行第一次处理,按照连续的时间分割 return1: [['2019-02-01', '2019-02-02', '2019-02-03'], ['2020-01-02', '2020-01-03', '2020-01-04'], ['2020-02-01', '2020-02-02']]
# 第二次处理,只有开始时间与结束时间 return2:[['2019-02-01', '2019-02-03'], ['2020-01-02', '2020-01-04'], ['2020-02-01', '2020-02-02']]

为存放时间字符串的列表排序

lst = [2,3,4,1,6]

lst = sorted(lst) # 升序

print(lst)


dic = {"123":["2020-11-12","2020-10-1","2019-2-1"],"456":["2018-12-3","2000-12-12"],"666":["2011-12-12"]}
dic = {k:sorted(dic[k]) for k in dic}
print(dic)

# 字典生成式
# dic = {k:[dic[k][0],dic[k][-1]] for k in dic} # print(dic) for k in dic: if len(dic[k]) == 1:continue dic[k] = [dic[k][0],dic[k][-1]] print(dic) # {'123': ['2019-2-1', '2020-11-12'], '456': ['2000-12-12', '2018-12-3'], '666': ['2011-12-12']}

连续时间分段的处理与列表

# 连续时间分段的处理与列表
from datetime import datetime,timedelta


NOW_STR = datetime.strftime(datetime.now(),"%Y-%m-%d")
lst = ['2019-02-01', '2019-02-02', '2019-02-03', '2020-01-02', '2020-01-03',"2020-01-04","2020-02-01","2020-02-02"]

# 将时间列表按照“时间连续”分割
def split_time_lst(lst:list)->list:
    """
    lst: ['2019-02-01', '2019-02-02', '2019-02-03', '2020-01-02', '2020-01-03',"2020-01-04","2020-02-01","2020-02-02"]
    return: [['2019-02-01', '2019-02-02', '2019-02-03'], ['2020-01-02', '2020-01-03', '2020-01-04'], ['2020-02-01', '2020-02-02']]
    """
    lis = []
    # 上一次分割的下标
    index = 0
    for k,v in enumerate(lst):
        # print(k,v)
        if k < len(lst)-1 and datetime.strptime(v,"%Y-%m-%d") + timedelta(days=1) != datetime.strptime(lst[k+1],"%Y-%m-%d"):
            # print("分割的下标:",k)
            lis.append(lst[index:k+1])
            index = k+1
    # 把最后剩下的也append进去
    lis.append(lst[index:])
    # 去掉空列表
    if [] in lis:
        lis.pop(lis.index([]))
    return lis
lst = split_time_lst(lst)
print(lst) # [['2019-02-01', '2019-02-02', '2019-02-03'], ['2020-01-02', '2020-01-03', '2020-01-04'], ['2020-02-01', '2020-02-02']]

# 进一步处理一下分割后的时间列表,每一段只有开始时间与结束时间
def handle_split_time_lst(lst:list,start_time="2010-01-01",end_time=NOW_STR)->list:
    """
    lst: [['2019-02-01', '2019-02-02', '2019-02-03'], ['2020-01-02', '2020-01-03', '2020-01-04'], ['2020-02-01', '2020-02-02']]
    return: [['2019-02-01', '2019-02-03'], ['2020-01-02', '2020-01-04'], ['2020-02-01', '2020-02-02']] 
    """
    for k,v in enumerate(lst):
        if len(v) == 1:
            s_t = v[0] if v[0] > start_time else start_time
            s_e = end_time
            lst[k] = [s_t,s_e]
        else:
            lst[k] = [v[0],v[-1]]
    return lst

ls = handle_split_time_lst(lst)
print(ls) # [['2019-02-01', '2019-02-03'], ['2020-01-02', '2020-01-04'], ['2020-02-01', '2020-02-02']]


### 接口测试
dic = {"123_1":["2019-02-02","2019-02-03","2019-02-01","2020-01-02","2020-01-03"],"123_2":["2018-12-3","2000-12-12"],"222_1":["2011-12-12"]}
# 拍一下序 —— 注意这样排序的前提是“所有的时间字符串都是按照统一的格式来写的”
dic = {k:sorted(dic[k]) for k in dic}
# 调用之前写的接口测试
for key in dic:
    dic[key] = split_time_lst(dic[key])
    dic[key] = handle_split_time_lst(dic[key])
print(dic)

~~~

 

posted on 2020-02-20 01:06  江湖乄夜雨  阅读(321)  评论(0编辑  收藏  举报