Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get highest duration from a list of strings

I have a list of durations like below

['5d', '20h', '1h', '7m', '14d', '1m']

where d stands for days, h stands for hours and m stands for minutes.

I want to get the highest duration from this list(14d in this case). How can I get that from this list of strings?

like image 864
Rafiul Sabbir Avatar asked Jan 17 '20 14:01

Rafiul Sabbir


People also ask

Does Max function work on string?

max() is an inbuilt function in Python programming language that returns the highest alphabetical character in a string.

How do I find the second longest string in a list Python?

sort(key=len) will sort your list of strings by their length. The longest would be a[-1], and the second longest would be a[-2]. `

How do you find the longest string in a list?

In a traditional approach we will capture the first element in the list length setting it to a variable named longestString. Next we will iterate over a list checking each element and comparing it to the longestString length. If the length is greater than the previous we will set the longestString element to the current element.

How to get the maximum length of a string in Python?

In this, we run a loop to keep a memory of longest string length and return the string which has max length in list. This method can also be used to solve this problem. In this, we use inbuilt max () with “len” as key argument to extract the string with the maximum length. Attention geek!

How can I get max length record from list in Java?

How can I get max length record from the List? List<string> strings = new List<string> (); strings.Add ("001"); strings.Add ("00121"); strings.Add ("001123123"); strings.Add ("00144"); string longest = strings.OrderByDescending (s => s.Length).First ();

How to find the maximum length string in a collection using Groovy?

In a comparable example we demonstrate how to find the maximum length string in a collection using groovy. In a traditional approach we will capture the first element in the list length setting it to a variable named longestString. Next we will iterate over a list checking each element and comparing it to the longestString length.


Video Answer


2 Answers

np.argmax on pd.to_timedelta:

import numpy as np
import pandas as pd

durations = ['5d', '20h', '1h', '7m', '14d', '1m']

durations[np.argmax(pd.to_timedelta(durations))]
Out[24]: '14d'

pd.to_timedelta turns a string into a duration (source), and np.argmax returns the index of the highest element.

like image 67
Nicolas Gervais Avatar answered Oct 04 '22 12:10

Nicolas Gervais


Pure python solution. We could store mapping between our time extensions (m, h, d) and minutes (here time_map), to find highest duration. Here we're using max() with key argument to apply our mapping.

inp = ['5d', '20h', '1h', '7m', '14d', '1m']
time_map = {'m': 1, 'h': 60, 'd': 24*60}

print(max(inp, key=lambda x:int(x[:-1])*time_map[x[-1]]))  # -> 14d
like image 43
Filip Młynarski Avatar answered Oct 04 '22 12:10

Filip Młynarski