1

I have this string:

strg = "01|15|59, 1|47|16, 01|17|20, 1|32|34, 2|17|17" 
#they are hours, minutes, seconds for different results

I will proceed by creating a list of sub-lists in which the first element is a list of h,m,s and the second element is the amount of total seconds. This is the code:

def stat(strg):
    z = strg.split(",")                            #creates the lists with h,m,s
    x = [i.split('|') for i in z]                  #remove the "|"
    y = [[int(i) for i in q] for q in x]           #transform the list elements into integers
    w = []                                         #ready the new list (of lists)
    for i in range(0, len(y)):                     #for lopp to fill "w"
        k = []
        z = ((y[i][0]*3600)+(y[i][1]*60)+y[i][2])  #transforms into total seconds amount
        k.append(y[i])
        k.append(z)
        w.append(k)
    return max(w)

The result will be:

[[2, 17, 17], 8237]

(FYI, without max, "return w" will bring:)

[[[1, 15, 59], 4559], [[1, 47, 16], 6436], [[1, 17, 20], 4640], [[1, 32, 34], 5554], [[2, 17, 17], 8237]]

Now my questions are:
1: How is the max() function working in this case? Which value has the programme considered?
2: Suppose we have a random number instead of the total amount of seconds as second element for each sub-list, how would I be able choose the sub-list with the max random value?

post_lupy
  • 41
  • 5
  • 2
    For question1: [Comparing Sequences and Other Types](https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types). – Mechanic Pig Oct 03 '22 at 16:10
  • 1
    For question 2, notice that `max` takes a `key` parameter. – Mark Ransom Oct 03 '22 at 16:15
  • 1
    Get out of the habit of using `for index in range(len(list)):`. Use `for item in list:` or `for index, item in enumerate(list):` – Barmar Oct 03 '22 at 16:18
  • 1
    "How is the max() function working in this case? Which value has the programme considered?" it is considering the list `w`, which you pass to it, `max(w)`. Are you asking how the sublists are ordered? Lists are ordered lexicographically – juanpa.arrivillaga Oct 03 '22 at 18:11

0 Answers0