我需要打印a到b范围内的c的所有项目
01/08/2017, 02/08/2017, 03/08/2017, 04/08/2017, 05/08/2017, 06/08/2017, 07/08/2017, 08/08/2017, 09/08/2017, 10/08/2017, 11/08/2017, 12/08/2017, 13/08/2017, 14/08/2017, 15/08/2017, 16/08/2017, 17/08/2017, 18/08/2017, 19/08/2017, 20/08/2017, 21/08/2017, 22/08/2017, 23/08/2017, 24/08/2017, 25/08/2017, 26/08/2017, 27/08/2017, 28/08/2017
a = '01/08/2017'
b = '28/08/2017'
c = ['01/08/2017', '20/08/2017', '21/08/2017', '22/08/2017', '23/08/2017', '24/08/2017', '25/08/2020', '26/08/2020', '27/08/2020', '28/08/2020']
我的答案应该是:
['01/08/2017', '20/08/2017', '21/08/2017', '22/08/2017', '23/08/2017', '24/08/2017']
答案 0 :(得分:3)
您可以使用datetime
模块:
import datetime
def to_datetime(d):
day, month, year = map(int, d.split('/'))
return datetime.datetime(year, month, day, 0, 0, 0)
a = '01/08/2017'
b = '28/08/2017'
_a = to_datetime(a)
_b = to_datetime(b)
c = ['01/08/2017', '20/08/2017', '21/08/2017', '22/08/2017', '23/08/2017', '24/08/2017', '25/08/2017', '26/08/2017', '27/08/2017', '28/08/2017']
for i in c:
if _a <= to_datetime(i) <= _b:
print(i)
输出:
01/08/2017
20/08/2017
21/08/2017
22/08/2017
23/08/2017
24/08/2017
25/08/2017
26/08/2017
27/08/2017
28/08/2017
答案 1 :(得分:1)
如果您愿意使用第三方库,则可以使用熊猫:
import pandas as pd
s = pd.to_datetime(pd.Series(c))
res = s[s.between(a, b)].tolist()
print(res)
[Timestamp('2017-01-08 00:00:00'),
Timestamp('2017-08-20 00:00:00'),
Timestamp('2017-08-21 00:00:00'),
Timestamp('2017-08-22 00:00:00'),
Timestamp('2017-08-23 00:00:00'),
Timestamp('2017-08-24 00:00:00')]
答案 2 :(得分:0)
将日期转换为datetime
个对象并进行直接比较:
from datetime import datetime as dt
FMT = "%d/%m/%Y"
[date for date in c
if dt.strptime(a, FMT)
<= dt.strptime(date, FMT)
<= dt.strptime(b, FMT)]
#['01/08/2017', '20/08/2017', '21/08/2017', '22/08/2017', '23/08/2017',
# '24/08/2017', '25/08/2017', '26/08/2017', '27/08/2017', '28/08/2017']
答案 3 :(得分:0)
您可以使用条件列表理解:
import datetime as dt
f = '%d/%m/%Y' # Date format.
>>> [date for date in c
if dt.datetime.strptime(a, f)
<= dt.datetime.strptime(date, f)
<= dt.datetime.strptime(b, f)]
['01/08/2017',
'20/08/2017',
'21/08/2017',
'22/08/2017',
'23/08/2017',
'24/08/2017',
'25/08/2017',
'26/08/2017',
'27/08/2017',
'28/08/2017']
答案 4 :(得分:0)
如果您要的是某种解决方案,并且没有依赖项,那么该解决方案会更容易理解。
a = '01/08/2017'
b = '28/08/2017'
c = ['01/08/2017', '20/08/2017', '21/08/2017', '22/08/2017', '23/08/2017', '24/08/2017', '25/08/2020', '26/08/2020', '27/08/2020', '28/08/2020']
a = a.split("/") #break requirements into components
b = b.split("/")
answerList = [] # form the final list structure
for i in range(0, len(c)): #iterate through all of c
cHolder = c[i].split("/") #break c into components
componentValidator = 0 # simple all-or-nothing counter
for j in range(3): # go through every component
if (int(a[j]) <= int(cHolder[j]) <= int(b[j])): # check ranges
componentValidator = componentValidator + 1 # for each component
if componentValidator == 3: # if all correct
answerList.append(c[i]) # add to final answers
print(answerList) # print final answers
输出:
['01/08/2017', '20/08/2017', '21/08/2017', '22/08/2017', '23/08/2017', '24/08/2017']