简体   繁体   中英

[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'])

Sort the values as time ranges by time before printing them. The following function converts your string to a tuple of two time objects:

def str2timerange(timestring):
    timestring = timestring.replace("24:00", "00:00")
    return [datetime.time(*map(int, t.split(":"))) for t in timestring.split("-")]

Remember that "24:00" is not a valid time, it must be recorded as "00:00".

d = defaultdict(list)
d['Mon'].append("10:00-24:00") # WRONG, but str2timerange will fix it
d['Tue'].append("12:00-14:00")
d['Mon'].append("1:35-4:00")
for i in d.items():
    print(i[0],sorted(i[1],key=str2timerange))

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

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:') .

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