如果您尝试将日期作为字符串处理,我建议您使用datetime。
将字符串日期转换为datetime对象,然后将列表中的所有日期与开始日期和结束日期进行比较:from datetime import datetime
start_date = datetime.strptime('1/1/2014', '%m/%d/%Y')
end_date = datetime.strptime('12/31/2014', '%m/%d/%Y')
dates=['4/6/2013', '5/4/2013', '6/26/2013', '7/26/2013', '9/5/2013', '10/7/2013', '10/12/2013', '4/12/2014', '5/10/2014', '6/12/2014', '7/19/2014', '8/15/2014', '9/17/2014', '4/21/2015', '5/28/2015', '6/26/2015']
# this line creates a list of datetime objects from the given strings in list dates
dt_dates = [datetime.strptime(date, '%m/%d/%Y') for date in dates]
现在比较开始和结束:in_between_dates = []
for d in dt_dates:
if d >= start_date and d <= end_date:
in_between_dates.append(d)
在这里,我以给定的字符串格式打印输出:print [d.strftime('%m/%d/%Y') for d in in_between_dates]
# prints: ['04/12/2014', '05/10/2014', '06/12/2014', '07/19/2014', '08/15/2014', '09/17/2014']
这里使用的datetime中的两个主要方法是^{} and ^{},它们分别用于将字符串转换为datetime对象和将datetime转换为字符串。