Bearing in mind that partidos
is a list with sublists containing tuples. Ex: partidos = [[(datetime.date(2000, 9, 9), 'FC Barcelona', 2, 'Málaga CF', 1)], [(datetime.date(2000, 9, 9), 'RC Deportivo', 2, 'Athletic Club', 0)], [(datetime.date(2000, 9, 9), 'Real Sociedad', 2, 'R. Racing C.', 2)]]
I tried to build the following function so that it does what it asks:
def partidos_por_fecha(partidos, inicio=None, fin=None):
''' Filtra los partidos jugados en un rango de fechas
ENTRADA:
- partidos: lista de partidos -> [Partido(datetime.date, str, int, str, int)]
- inicio: fecha inicial del rango -> datetime.date
- fin: fecha final del rango -> datetime.date
SALIDA:
- lista de partidos seleccionados -> [Partido(datetime.date, str, int, str, int)]
Se devuelven aquellos partidos que se han jugado entre las fechas inicio
y fin. Ambos parámetros serán objetos date del módulo datetime.
Si inicio es None, se incluirán los partidos desde el principio de
la serie, y si fin es None se inlcuirán los partidos hasta el último de
la serie.
'''
if inicio == None:
inicio = partidos[0][0][0]
elif fin == None:
fin = partidos[-1][-1][0]
elif inicio == None and fin == None:
inicio = partidos[0][0][0]
fin = partidos[-1][-1][0]
else:
inicio == inicio
fin == fin
result = []
for i in partidos:
for fecha, local, goles_local, visitante, goles_visitante in i:
if fecha >= inicio and fecha <= fin:
result.append((fecha, local, goles_local, visitante, goles_visitante))
return result
This function is executed by the following code:
def test_partidos_por_fechas(partidos):
inicio = datetime(2007, 9, 15).date()
fin = datetime(2008, 7, 1).date()
print(len(partidos_por_fecha(partidos, inicio, fin)))
print(len(partidos_por_fecha(partidos, inicio, None)))
print(len(partidos_por_fecha(partidos, None, fin)))
print(len(partidos_por_fecha(partidos, None, None)))
test_partidos_por_fechas(partidos)
The error that I get after executing the function is:
TypeError: '<=' not supported between instances of 'datetime.date' and 'NoneType'
And it tells me that it is in the if
that is inside the loops for
. I do not understand how after filtering the arguments inicio
and fin
when they have None
before loops for
, I keep entering variables None
to compare with datetimes
I would like to get some solution for the function to work. I'm sorry if there are errors of indentation in the code to have passed it here and thanks in advance.