简体   繁体   中英

Python Average, Max, Min from list

so I have a list like this:

kesto = ['3m 24s', '45s', '1h 2m 40s']

I need to find an average, min and max of these. I have tried:

max_value = max(kesto)
min_value = min(kesto)
avg_value = sum(kesto)/len(kesto)

but of course it doesn't work because these are not numbers. and they have "m" and "s" behind them.

EDIT: This code works fine, but it only returns the first value it gets. for example if there is a "h" and a "m" it returns only the "h" so 1h 20min and 1h 15min would be "3600" both.

            def parse_time(s):
                    s = s.split()
                    total = 0
                    for cl in s:
                            if cl[-1] == 'd':
                                total += int(cl[:-1]) * 60 * 60 * 24
                            if cl[-1] == 'h':
                                total += int(cl[:-1]) * 60 * 60
                            if cl[-1] == 'm':
                                total += int(cl[:-1]) * 60
                            if cl[-1] == 's':
                                total += int(cl[:-1])
                            return total
            kesto2 = [parse_time(s) for s in kesto]

Create a function that converts those strings to numbers, then use this function:

def conv(x):
    (m, s) = x.replace('m', '').replace('s', '').split()
    return 60 * int(m) + int(s)

max_value = max(kesto, key=conv)
min_value = min(kesto, key=conv)
avg_value = sum(map(conv, kesto))/len(kesto)

I suggest you make a function that convert your values in seconds.
For example "3m 24s" is 204 seconds.
I help you with the function's signature:

def converter(value):

After you finished your function, use

max_value = max(kesto, key=converter)
min_value = min(kesto, key=converter)

and so on...

s=0
n = []
for l in li:
   s = (60*int(l[0]))+int(l[3:4])
   n.append(s)

n is your desired list.

you could parse those entries with strptime and then convert to timedelta objects. these can be added and divided by integers:

from datetime import datetime, timedelta

NULL_TIMEDELTA = timedelta()
kesto = ['3m 24s', '45s', '1h 2m 40s']

def to_timedelta(strg):
    if 'h' in strg:
        tme = datetime.strptime(strg, '%Hh %Mm %Ss').time()
    elif 'm' in strg:
        tme = datetime.strptime(strg, '%Mm %Ss').time()
    else:
        tme = datetime.strptime(strg, '%Ss').time()
    dte = datetime.combine(datetime.min, tme)  # need a datetime object
    td = dte - datetime.min                    # to create timedelta object
    return td

timedeltas = [to_timedelta(item) for item in kesto]
max_value = max(timedeltas)
min_value = min(timedeltas)
avg_value = sum(timedeltas, NULL_TIMEDELTA)/len(timedeltas)

print(max_value)  # 1:02:40
print(min_value)  # 0:00:45
print(avg_value)  # 0:22:16.333333

note you have to initialize the sum with NULL_TIMEDELTA = timedelta() . the default is 0 - but int + timedelta is not defined.

The code below converts the time to seconds and adds them to a new list called kestoS , since they are all the same units, now you can easily sort them.

First the minutes are separates by using split() which separates the list when an 'm' is seen. I only take the first value hence [0] . The seconds are obtained by taking the character that occur after a space up to an 's'. At the end I total the seconds and add them to kestoS .

This solution is not limited by the number of digits the seconds or the minutes have.

kestoS =[]
for time in kesto:
    minutes = time.split('m')[0]
    seconds = time.[time.find(' ') + 1: time.find('s')]
    kestoS.append((minutes*60)+seconds)
max_value = max(kestoS)
min_value = min(kestoS)
avg_value = sum(kestoS)/len(kestoS)

hope this will help

import re 
kesto = ['3m 24s', '1m 5s', '2m 40s']
kseto_total = []
for k in kesto:
    # here split the two values and remove the m and s from string
    minuite,second =k.replace('m','').replace('s','').split(' ')
    # convert the string into int value
    minuite=int(minuite)
    second = int(second)
    # calculating the total value and append into the new list
    kseto_total.append(minuite*60+second)
# applying max, min and calculate average
max_value = max(kseto_total)
min_value = min(kseto_total)
avg_value = sum(kseto_total)//len(kseto_total)
print(kesto)
# printing the result into converting minute and secondd
print(max_value//60,'m',max_value%60,'s\n',
      min_value//60,'m',min_value%60,'s\n',
      avg_value//60,'m',avg_value%60,'s')

Update

def format_string(time):
    k1 = ''
    k2 = ''
    k3 = ''
    if 'h' in time:
        k1 = time
        pass
    else:
        k1 = '0h ' + k
    if 'm' in k1:
        k2 = k1
        pass
    else:
        k2 = '0m ' + k1
    if 's' in k2:
        k3 = k2
        pass
    else:
        k3 = ' 0s' + k2
    return k3

Here you call the function for string formating. and then you can oparate as comment describe. string formatting can be done in efficient way. Here is the only a simple solution.

This is my solution for the first part of converting the strings to usable seconds values, think it works well

kesto = ['3m 24s', '45s', '1h 2m 40s']

def parse_time(s):
    s = s.split()
    total = 0
    for cl in s:
        if cl[-1] == 'h':
            total += int(cl[:-1]) * 60 * 60
        elif cl[-1] == 'm':     
            total += int(cl[:-1]) * 60
        elif cl[-1] == 's':
            total += int(cl[:-1])
    return total

kesto2 = [parse_time(s) for s in kesto] 

Results in kesto2 = [204, 45, 3760] which are the times in seconds. From there you can find your statistics pretty easily.

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