Digamos que tengo un montón de fechas como tales:

2018-11-19 08:00:03
2018-11-19 12:00:02
2018-
0
BlahZayBlahZay 10 oct. 2019 a las 05:36

3 respuestas

La mejor respuesta

Puede ordenar la lista y hacer una búsqueda binaria para encontrar la que esté más cerca de "dentro de una semana".

Consulte esta respuesta sobre cómo comparar objetos de fecha y hora.

Consulte también esta respuesta sobre cómo convertir cadenas a fecha y hora.

Para realizar la búsqueda binaria, puede crear una función como esta y usar el {{X0} } módulo:

import bisect

def find_next_week(dates, current_date):
    return dates[bisect.bisect(dates, current_date + datetime.timedelta(weeks=1)) - 1]

(1)

>>> find_next_week(dates, datetime.datetime.strptime('2018-11-19 08:00:03', '%Y-%m-%d %H:%M:%S'))
datetime.datetime(2018, 11, 26, 4, 0, 7)

Para convertir este objeto de fecha y hora al nombre de archivo original, puede hacer esto:

>>> datetime.datetime(2018, 11, 26, 4, 0, 7).strftime('%Y-%m-%d %H:%M:%S')
'2018-11-26 04:00:07'

(1) Recuerde ordenar dates antes de pasarlo a la función porque la función espera una lista ordenada.

1
smac89 10 oct. 2019 a las 03:12

[Editar: si el volumen de sus datos es grande, entonces recomiendo la solución que utiliza bisect - para obtener resultados más rápidos.]

datestrings = '''2018-11-19 08:00:03
... 2018-11-19 12:00:02
... 2018-11-19 16:00:01
... 2018-11-19 20:00:10
... 2018-11-20 00:00:09
... 2018-11-20 04:00:08
... 2018-11-20 08:00:07
... 2018-11-20 12:00:06
... 2018-11-20 16:00:04
... 2018-11-20 20:00:03
... 2018-11-21 00:00:02
... 2018-11-21 04:00:01
... 2018-11-21 08:00:10
... 2018-11-21 12:00:09
... 2018-11-21 16:00:08
... 2018-11-21 20:00:07
... 2018-11-22 00:00:06
... 2018-11-22 04:00:04
... 2018-11-22 08:00:03
... 2018-11-22 12:00:02
... 2018-11-22 16:00:01
... 2018-11-22 20:00:10
... 2018-11-23 00:00:09
... 2018-11-23 04:00:08
... 2018-11-23 08:00:07
... 2018-11-23 12:00:05
... 2018-11-23 16:00:05
... 2018-11-23 20:00:04
... 2018-11-24 00:00:03
... 2018-11-24 04:00:01
... 2018-11-24 08:00:10
... 2018-11-24 12:00:09
... 2018-11-24 16:00:08
... 2018-11-24 20:00:08
... 2018-11-25 00:00:06
... 2018-11-25 04:00:04
... 2018-11-25 08:00:03
... 2018-11-25 12:00:02
... 2018-11-25 16:00:01
... 2018-11-25 20:00:10
... 2018-11-26 00:00:08
... 2018-11-26 04:00:07
... 2018-11-26 08:00:06
... 2018-11-26 12:00:05
... 2018-11-26 16:00:04
... 2018-11-26 20:00:02
... 2018-11-27 00:00:01
... 2018-11-27 04:00:10
... 2018-11-27 08:00:10
... 2018-11-27 12:00:08
... 2018-11-27 16:00:06
... 2018-11-27 20:00:05
... 2018-11-28 00:00:04
... 2018-11-28 04:00:03
... 2018-11-28 08:00:02
... 2018-11-28 12:00:11
... 2018-11-28 16:00:09
... 2018-11-28 20:00:08
... 2018-11-29 00:00:07
... 2018-11-29 04:00:06
... 2018-11-29 08:00:05
... 2018-11-29 12:00:04
... 2018-11-29 16:00:03
... 2018-11-29 20:00:02
... 2018-11-30 00:00:01
... 2018-11-30 04:00:10
... 2018-11-30 08:00:09
... 2018-11-30 12:00:08
... 2018-11-30 16:00:07
... 2018-11-30 20:00:06
... 2018-12-01 04:00:04
... 2018-12-01 12:00:02
... 2018-12-01 20:00:10
... 2018-12-02 04:00:08
... 2018-12-02 12:00:05
... 2018-12-02 20:00:04
... 2018-12-03 04:00:02
... 2018-12-03 12:00:10
... 2018-12-03 20:00:08
... 2018-12-04 04:00:01
... 2018-12-04 12:00:08
... 2018-12-04 20:00:05
... 2018-12-05 04:00:02
... 2018-12-05 12:00:09'''

Primero analice las cadenas de fecha en objetos datetime. Luego encuentre todas las fechas que son menos de una semana a partir de una fecha determinada. Luego encuentre la más grande de todas esas fechas.

from datetime import datetime, timedelta

dates = [datetime.strptime(d, '%Y-%m-%d %H:%M:%S') for d in datestrings.split('\n')]

first_date = datetime(2018, 11, 19, 8, 0, 3)

dates_in_week = [d for d in dates if d < first_date + timedelta(weeks = 1)]

last_date = max(dates_in_week)

last_date.strftime('%Y-%m-%d %H:%M:%S')

1
ba_ul 10 oct. 2019 a las 03:23

Básicamente, todas tus fechas son más de una semana a partir de ahora (desplázate un poco hacia abajo para ver el código):

>>> a = '''2018-11-19 08:00:03
... 2018-11-19 12:00:02
... 2018-11-19 16:00:01
... 2018-11-19 20:00:10
... 2018-11-20 00:00:09
... 2018-11-20 04:00:08
... 2018-11-20 08:00:07
... 2018-11-20 12:00:06
... 2018-11-20 16:00:04
... 2018-11-20 20:00:03
... 2018-11-21 00:00:02
... 2018-11-21 04:00:01
... 2018-11-21 08:00:10
... 2018-11-21 12:00:09
... 2018-11-21 16:00:08
... 2018-11-21 20:00:07
... 2018-11-22 00:00:06
... 2018-11-22 04:00:04
... 2018-11-22 08:00:03
... 2018-11-22 12:00:02
... 2018-11-22 16:00:01
... 2018-11-22 20:00:10
... 2018-11-23 00:00:09
... 2018-11-23 04:00:08
... 2018-11-23 08:00:07
... 2018-11-23 12:00:05
... 2018-11-23 16:00:05
... 2018-11-23 20:00:04
... 2018-11-24 00:00:03
... 2018-11-24 04:00:01
... 2018-11-24 08:00:10
... 2018-11-24 12:00:09
... 2018-11-24 16:00:08
... 2018-11-24 20:00:08
... 2018-11-25 00:00:06
... 2018-11-25 04:00:04
... 2018-11-25 08:00:03
... 2018-11-25 12:00:02
... 2018-11-25 16:00:01
... 2018-11-25 20:00:10
... 2018-11-26 00:00:08
... 2018-11-26 04:00:07
... 2018-11-26 08:00:06
... 2018-11-26 12:00:05
... 2018-11-26 16:00:04
... 2018-11-26 20:00:02
... 2018-11-27 00:00:01
... 2018-11-27 04:00:10
... 2018-11-27 08:00:10
... 2018-11-27 12:00:08
... 2018-11-27 16:00:06
... 2018-11-27 20:00:05
... 2018-11-28 00:00:04
... 2018-11-28 04:00:03
... 2018-11-28 08:00:02
... 2018-11-28 12:00:11
... 2018-11-28 16:00:09
... 2018-11-28 20:00:08
... 2018-11-29 00:00:07
... 2018-11-29 04:00:06
... 2018-11-29 08:00:05
... 2018-11-29 12:00:04
... 2018-11-29 16:00:03
... 2018-11-29 20:00:02
... 2018-11-30 00:00:01
... 2018-11-30 04:00:10
... 2018-11-30 08:00:09
... 2018-11-30 12:00:08
... 2018-11-30 16:00:07
... 2018-11-30 20:00:06
... 2018-12-01 04:00:04
... 2018-12-01 12:00:02
... 2018-12-01 20:00:10
... 2018-12-02 04:00:08
... 2018-12-02 12:00:05
... 2018-12-02 20:00:04
... 2018-12-03 04:00:02
... 2018-12-03 12:00:10
... 2018-12-03 20:00:08
... 2018-12-04 04:00:01
... 2018-12-04 12:00:08
... 2018-12-04 20:00:05
... 2018-12-05 04:00:02
... 2018-12-05 12:00:09'''
>>> import datetime
>>> dates = [datetime.datetime.strptime( d, '%Y-%m-%d %H:%M:%S') for d in a.split('\n')]
>>> now = datetime.datetime.now()
>>> for d in dates :
...     print d,
...     if now - d > datetime.timedelta( days=7 ) :
...         print '*'
...     else :
...         print
... 
2018-11-19 08:00:03 *
2018-11-19 12:00:02 *
2018-11-19 16:00:01 *
2018-11-19 20:00:10 *
2018-11-20 00:00:09 *
2018-11-20 04:00:08 *
2018-11-20 08:00:07 *
2018-11-20 12:00:06 *
2018-11-20 16:00:04 *
2018-11-20 20:00:03 *
2018-11-21 00:00:02 *
2018-11-21 04:00:01 *
2018-11-21 08:00:10 *
2018-11-21 12:00:09 *
2018-11-21 16:00:08 *
2018-11-21 20:00:07 *
2018-11-22 00:00:06 *
2018-11-22 04:00:04 *
2018-11-22 08:00:03 *
2018-11-22 12:00:02 *
2018-11-22 16:00:01 *
2018-11-22 20:00:10 *
2018-11-23 00:00:09 *
2018-11-23 04:00:08 *
2018-11-23 08:00:07 *
2018-11-23 12:00:05 *
2018-11-23 16:00:05 *
2018-11-23 20:00:04 *
2018-11-24 00:00:03 *
2018-11-24 04:00:01 *
2018-11-24 08:00:10 *
2018-11-24 12:00:09 *
2018-11-24 16:00:08 *
2018-11-24 20:00:08 *
2018-11-25 00:00:06 *
2018-11-25 04:00:04 *
2018-11-25 08:00:03 *
2018-11-25 12:00:02 *
2018-11-25 16:00:01 *
2018-11-25 20:00:10 *
2018-11-26 00:00:08 *
2018-11-26 04:00:07 *
2018-11-26 08:00:06 *
2018-11-26 12:00:05 *
2018-11-26 16:00:04 *
2018-11-26 20:00:02 *
2018-11-27 00:00:01 *
2018-11-27 04:00:10 *
2018-11-27 08:00:10 *
2018-11-27 12:00:08 *
2018-11-27 16:00:06 *
2018-11-27 20:00:05 *
2018-11-28 00:00:04 *
2018-11-28 04:00:03 *
2018-11-28 08:00:02 *
2018-11-28 12:00:11 *
2018-11-28 16:00:09 *
2018-11-28 20:00:08 *
2018-11-29 00:00:07 *
2018-11-29 04:00:06 *
2018-11-29 08:00:05 *
2018-11-29 12:00:04 *
2018-11-29 16:00:03 *
2018-11-29 20:00:02 *
2018-11-30 00:00:01 *
2018-11-30 04:00:10 *
2018-11-30 08:00:09 *
2018-11-30 12:00:08 *
2018-11-30 16:00:07 *
2018-11-30 20:00:06 *
2018-12-01 04:00:04 *
2018-12-01 12:00:02 *
2018-12-01 20:00:10 *
2018-12-02 04:00:08 *
2018-12-02 12:00:05 *
2018-12-02 20:00:04 *
2018-12-03 04:00:02 *
2018-12-03 12:00:10 *
2018-12-03 20:00:08 *
2018-12-04 04:00:01 *
2018-12-04 12:00:08 *
2018-12-04 20:00:05 *
2018-12-05 04:00:02 *
2018-12-05 12:00:09 *
>>> 
1
lenik 10 oct. 2019 a las 03:00
58314672