0

So say I have a list like so:

runtimes = ['24 min per ep',
     '1 hr 55 min',
     '24 min per ep',
     '25 min per ep',
     '23 min per ep',
     '23 min per ep',
     '23 min per ep',
     '27 min per ep',
     '24 min per ep',
     '23 min per ep',
     '24 min',
     '22 min per ep',
     '25 min per ep',
     '24 min per ep',
     '23 min per ep',
     '24 min per ep',
     '24 min per ep',
     '24 min per ep',
     '24 min per ep',
     '1 hr 41 min',
     '1 hr 27 min',
     '25 min per ep',
     '1 hr 22 min',
     '30 min per ep',
     '25 min per ep',
     '1 hr 31 min',
     '2 hr 4 min',
     '24 min per ep',
     '24 min per ep']

I want to convert these values into purely minutes:

I first thought it would be best to use two regex expressions like the ones below to replace the "1 hr" with "60 min" and then remove all the non-numerics, but this does not seem ideal.

re.sub("1 hr", "60 min", runtimes))

re.sub("\D", "", runtimes)

If someone knows a better way to do this it would be greatly appreciated.

Please let me know if you have any more questions.

Thank you for your help.

1
  • 1
    If you search in your browser for "Python time handling", you'll find references that can explain this much better than we can manage here. You'll want the datetime module and perhaps a simple input parser. Commented Sep 27, 2019 at 21:10

2 Answers 2

2

This is fairly simple just to code without using any special kind of parser:

def parse_runtime(runtime):
    mins = 0
    fields = runtime.split()
    for idx in range(0, len(fields)-2):
        if fields[idx+1] in ('min', 'mins', 'minutes'):
            mins += int(fields[idx])
        elif fields[idx+1] in ('hr', 'hrs', 'hours'):
            mins += int(fields[idx]) * 60

    return mins

runtime_mins = []
for runtime in runtimes:
    try:
        mins = parse_runtime(runtime)
        runtime_mins.append(mins)
    except ValueError:
        print('Bad runtime: ' + runtime)

print(runtime_mins)
Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't this say for idx in range(0, len(fields)-1) rather than -2?
@sums22, nope, then idx+1 would wrap to the start on the last iteration.
1

In pure python you can try this:

runtimes_min=[]
for r in runtimes:
    r=r.replace('per ep', '')
    hour = 0
    times = r.split('hr')
    if len(times) > 1:
        hour = times[0]
        minutes = times[1].split('min')[0]
    else:
        minutes=r.split('min')[0].rstrip()
    runtimes_min.append(f"{int(hour)*60 + int(minutes)} min per ep")

Output

['24 min per ep',
 '115 min per ep',
 '24 min per ep',
 '25 min per ep',
 '23 min per ep',
 '23 min per ep',
 '23 min per ep',
 '27 min per ep',
 '24 min per ep',
 '23 min per ep',
 '24 min per ep',
 '22 min per ep',
 '25 min per ep',
 '24 min per ep',
 '23 min per ep',
 '24 min per ep',
 '24 min per ep',
 '24 min per ep',
 '24 min per ep',
 '101 min per ep',
 '87 min per ep',
 '25 min per ep',
 '82 min per ep',
 '30 min per ep',
 '25 min per ep',
 '91 min per ep',
 '124 min per ep',
 '24 min per ep',
 '24 min per ep']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.