如何在Python中比较列表中的多个日期?

glh*_*e13 2 python list beautifulsoup web-scraping

我想知道如何比较列表中的日期。我想提取“最早”的日期。(我做了一个 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)
Run Code Online (Sandbox Code Playgroud)

输出:

['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']
Run Code Online (Sandbox Code Playgroud)

我想提取最早的日期:

例如。

'2018-04-01'

Sio*_*Goh 5

只需使用 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))
Run Code Online (Sandbox Code Playgroud)

产生

2018-04-01
Run Code Online (Sandbox Code Playgroud)