python - 如何比较 Python 列表中的多个日期?
问题描述
我想知道如何比较列表中的日期。我想提取“最早”的日期。(我做了一个for循环,因为我必须用'-'替换一些字符)
comment_list = comment_container.findAll("div", {"class" : "comment-date"})
D =[]
for commentDate in comment_list:
year, month, day = map(int, commentDate.split('-'))
date_object = datetime(year, month, day)
date_object = datetime.strptime(commentDate, '%Y-%m-%d').strftime('%Y-%m-%d')
D.append(date_object)
print(D)
输出:
['2018-06-26', '2018-04-01', '2018-07-19', '2018-04-23', '2018-08-25', '2018-06-08', '2018-06-14', '2018-07-08', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15']
我想提取最早的日期:
例如。
'2018-04-01'
解决方案
只需使用 min 函数:
A = ['2018-06-26', '2018-04-01', '2018-07-19', '2018-04-23', '2018-08-25', '2018-06-08', '2018-06-14', '2018-07-08', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15']
print(min(A))
生产
2018-04-01
推荐阅读
- python - 在pygame中以数组的形式获取屏幕某一部分的RGB像素数据
- cmd - 错误消息“错误:FUNCTION 'NUCLEUS_ASCII' 已存在。(状态 = X0Y68,代码 = 30000)”的原因是什么?
- numpy - Numpy:模糊“大于”运算符,处理值列表(请求对现有代码的建议)
- javascript - 如何从使用影子 DOM 的类访问/设置继承的属性?
- python - AttributeError:“str”对象没有属性“path”
- python - 组合框启用 ctrl+a 选择文本
- reactjs - 在 React Native Expo 中,整个屏幕被键盘压扁到一半
- ruby-on-rails - has_many 集合中未保存的默认值
- javascript - 根据 id 更新两层嵌套对象
- python - Python:替换不区分大小写的标志不起作用