简体   繁体   中英

Get the last date of the month from a given list of dates

I have a list of dates in a csv file which I would like to extract the last date of each month using python.

For example, 03/08/2019 20/08/2019 30/12/2020 31/12/2020

The output should be 20/08/2019 & 31/12/2020

You can sort the list of dates based on year, month and date and then convert it to a dict with key as the month and year, which would retain only the last date for a given month and year and then get dict's values as a list

>>> lst = ['03/08/2019', '20/08/2019', '30/12/2020', '31/12/2020']
>>> list({date.split('/', 1)[-1]:date for date in sorted(lst, key=lambda date: date.split('/')[::-1])}.values())
['20/08/2019', '31/12/2020']

Or using datetime

>>> from datetime import datetime
>>> to_date = lambda d: datetime.strptime(d, '%d/%m/%Y')
>>> list({to_date(d).replace(day=1):d for d in sorted(lst, key=to_date)}.values())
['20/08/2019', '31/12/2020']

@PythonNovice. I'd suggest starting with itertools.groupBy which is discussed here . You've got to open that file, parse the lines, sort the data, group it by months and take the last element of each month. Is there a specific part of that with which you're having a problem?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM