LoginSignup
2
0

More than 5 years have passed since last update.

マラソン記録を秒にする変換する関数

Last updated at Posted at 2017-12-13

マラソン等の記録データ(例:3:53:20、2:08:20など)を秒に変換するというニッチケースな関数を書いてみました。

def second(time):
    import re

    time_ref = re.split(':|:' , time)
    time_ref = list(filter(lambda n: n != '', time_ref))[::-1]

    second = 0
    for e ,i   in enumerate(time_ref) :
        second = second + int(i) * 60 ** int(e)

    return second

本当は日本語表記にありがちな「時分秒」の含まれる時間表記を数値化したかったのですが「2時間30秒」と「分」が抜けたケースをif文以外で回避できる方法を思いつかず。。

もっと良いやり方知っていましたら、ご教授いただけますと幸いです。

2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0