Search code examples
pythonsortingdefaultdict

[Python]: sorting defaultdict's values


I want to sort values in defaultdict's item according to their time period. How can I sort them?

from collections import defaultdict
d = defaultdict(list)
d['Mon'].append("10:00-24:00")
d['Tue'].append("12:00-14:00")
d['Mon'].append("1:35-4:00")
for i in d.items():
    print i

if I do this above I would get:

('Mon', ['10:00-24:00', '1:35-4:00'])
('Tue', ['12:00-14:00'])

How can I sort the values in the defaultdict according to the time order, so I can have:

('Mon', ['1:35-4:00','10:00-24:00'])
('Tue', ['12:00-14:00'])

Solution

  • First your times should be in a format that is easier to sort. As you have it now '10:00' is lexicographical smaller than '1:35', which is not what you want. One way to fix this is to add leading zeros to times in the single-digit hours.

    from collections import defaultdict
    d = defaultdict(list)
    d['Mon'].append("10:00-24:00")
    d['Tue'].append("12:00-14:00")
    d['Mon'].append("01:35-04:00")
    
    for i in d.items(): i[1].sort()
    
    >>> print d
    defaultdict(<class 'list'>, {'Mon': ['01:35-04:00', '10:00-24:00'], 'Tue': ['12:00-14:00']})
    

    If you need to convert your time strings automatically you could do a regex replace:

    t = '1:35-4:00'
    t = re.sub(r'(^|-)(\d:\d\d)', r'\g<1>0\2', t)
    >>> print t
    '01:35-04:00'
    

    Also check out DYZ's solution which converts your strings to datetime tuples. He also points out that '24:00' is not a valid time, and you could take care of those with t = t.replace('24:', '00:').