B
B
Brad_baf2020-06-18 19:38:32
Python
Brad_baf, 2020-06-18 19:38:32

How to sort text by value?

How to sort the date so that after changing the date to another day, indent or "\n"

06-18, 18:00:
06-18, 21:00:
06-19, 00:00:
06-19, 03:00:
06-19, 06:00:
06-19, 09:
00:06-19, 12:00:
06-19, 15: 00:
06-19 , 18:00:06-19, 21:00
:
06- 20, 00:00:
06-20, 03:00:
06-20, 06:00:
06-20, 09:00:
06-20, 12:00:
06-20, 15:00:
06-20, 18:00:
06-20, 21:00:

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-06-18
@Brad_baf

import re


dates = '''06-18, 18:00:
06-18, 21:00:
06-19, 00:00:
06-19, 03:00:
06-19, 06:00:
06-19, 09:00:
06-19, 12:00:
06-19, 15:00:
06-19, 18:00:
06-19, 21:00:
06-20, 00:00:
06-20, 03:00:
06-20, 06:00:
06-20, 09:00:
06-20, 12:00:
06-20, 15:00:
06-20, 18:00:
06-20, 21:00:'''


result = ''


last_day = None
for date in dates.split('\n'):

    day = re.search(r'\d+-(\d+)', date).group(1)

    if day != last_day:
        result += '\n{}\n'.format(date)
        last_day = day
    else:
        result += '{}\n'.format(date)


print(result)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question