2019.12.17基佬出的一道题
代码如下:
1 # -*- coding: utf-8 -*- 2 """ 3 @File:hz.py 4 @E-mail:chenshengkai@vip.qq.com 5 @Time:2019-12-17 10:28 6 @Author:Nobita 7 @Version:1.0 8 @Desciption:None 9 """ 10 11 from datetime import datetime 12 13 ''' 14 题目: 15 一个数据集合,存有多个这种字典{"time":"2019-04-16 08:23:40"} 16 对同一天时间的筛选出最早的一条,就是每天只有一条数据, 17 如果出现多条/就取当天最早的,筛选完后对数据从小到大排序, 18 数据自己伪造。 19 ''' 20 dict_data = { 21 "time1": "2019-04-01 06:22:41", 22 "time2": "2019-04-01 06:22:41", 23 "time3": "2019-04-01 09:23:40", 24 "time4": "2019-04-01 08:24:45", 25 "time5": "2019-04-02 08:23:40", 26 "time6": "2019-04-02 13:25:41", 27 "time7": "2019-04-03 08:23:40", 28 "time8": "2019-04-03 08:23:40", 29 "time9": "2019-04-04 08:23:40", 30 "time10": "2019-04-05 05:21:41", 31 "time11": "2019-04-05 11:23:40", 32 "time12": "2019-04-05 09:15:15", 33 "time13": "2019-04-06 08:12:12", 34 "time14": "2019-04-07 08:57:41", 35 "time15": "2019-04-11 09:21:11", 36 "time16": "2019-04-12 08:23:40" 37 } 38 39 if __name__ == '__main__': 40 dict_list = dict() 41 # 循环获取字符串,并进行初步的日期处理 42 for v in dict_data.values(): 43 data = datetime.strptime(v, '%Y-%m-%d %H:%M:%S') 44 data_str = str(data.date()) 45 if data_str not in dict_list: 46 dict_list[data_str] = list() 47 dict_list[data_str].append(v) 48 dict_list[data_str].sort(reverse=False) 49 # 获取每个日期第一个并排序 50 result = [v[0] for v in dict_list.values()] 51 result.sort(reverse=False) 52 print(result)