Issue
I have a python list of strings which define some time interval. Something like:
intervals = ["1 days", "7 days", "30 days"]
I need to find the index of the maximum in this interval list. is there any library in python that would allow me to do this?
Solution
Try the parse_timespan
function from humanfriendly
to convert each of them, then find the maximum in the usual way?
>>> from humanfriendly import parse_timespan
>>> intervals = ["1 days", "7 days", "30 days"]
>>> parsed_intervals = [parse_timespan(interval) for interval in intervals]
>>> max(parsed_intervals)
2592000.0
If you want the original format (rather than the amount of time for further processing), you can also use:
>>> from humanfriendly import parse_timespan
>>> intervals = ["1 days", "7 days", "30 days"]
>>> max(intervals, key=parse_timespan)
'30 days'
Answered By – Jiří Baum
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0