社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

使用python从一系列日期中检查项目列表

Martin Bouhier • 5 年前 • 1595 次点击  

我需要打印所有在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']
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38565
 
1595 次点击  
文章 [ 5 ]  |  最新文章 5 年前
DYZ
Reply   •   1 楼
DYZ    6 年前

将日期转换为 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']
RMass
Reply   •   2 楼
RMass    6 年前

如果这是你想要的,一个稍微容易理解的解决方案,没有依赖性。

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']
Alexander
Reply   •   3 楼
Alexander    6 年前

您可以使用条件列表理解:

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']
jpp
Reply   •   4 楼
jpp    6 年前

如果您愿意使用第三方图书馆,您可以使用熊猫:

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')]
Ajax1234
Reply   •   5 楼
Ajax1234    6 年前

您可以使用 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