-3

I want to ask a question. If I have list date like this:

'2017-03-23'
'2017-04-10'
'2017-04-11'
'2017-04-12'
'2017-04-13'
'2017-05-26'
'2017-05-27'
'2017-05-28'

then I want wrote those date to be like this:

'2017-03-23','2017-04-10 - 2017-04-13','2017-05-26 - 2017-05-28'

Please give an advise

thank you

Thomas
  • 130
  • 15
halim hamadi
  • 99
  • 3
  • 12
  • 1
    Possible duplicate of [Creating a range of dates in Python](http://stackoverflow.com/questions/993358/creating-a-range-of-dates-in-python) – Jainish Kapadia Mar 23 '17 at 08:05
  • 2
    @jainishkapadia: Having similar words in the title doesn't mean those questions are duplicate. In this case, at all. – Eric Duminil Mar 23 '17 at 09:01

1 Answers1

1

sort all date first and merge it. for example....

def merge(dates):
    ans = []
    dates.sort()
    for date in dates:
        if len(ans) == 0:
            ans.append(date)
        elif len(ans[-1]) == len('2017-04-10'):
            if ans[-1][:7] == date[:7]:
                ans[-1] += " - " + date
            else:
                ans.append(date)
        else:
            if ans[-1][-10:-3] == date[:7]:
                ans[-1] = ans[-1][:-2] + date[-2:]
            else:
                ans.append(date)
    return ans
Thomas
  • 130
  • 15