vendredi 25 mars 2022

Take 2 random dates from list of dates based on days range condition

I would like to randomly choose 2 dates from the list of dates based on condition that if date range between 2 randomly choosen dates from the list is lower than 100 days we want to take them if not we look for other 2 random dates from the list.

Code below will not work but this is more or less what I would like to achieve:

dates = [date(2020, 1, 25), date(2020, 2, 23), date(2020, 3, 27)]

def get_2_random_dates_with_with_days_range_100():
   choosen_dates = random.choice([a, b for a, b in dates if a.days - b.days <= 100])

Also code above shows more or less what I've tried which is to use list comprehension with the condition but it would require me to unpack 2 values from the list

Thanks




Aucun commentaire:

Enregistrer un commentaire