修改列表中的元组

zz3*_*599 2 python python-2.7

我有这样的时间输入:

09:00 12:00
10:00 13:00
11:00 12:30
12:02 15:00
09:00 10:30
Run Code Online (Sandbox Code Playgroud)

我试图将其构建为元组列表,转换为分钟:

[(540, 720), (600, 780), (660, 750), (722, 900), (540, 630)]
Run Code Online (Sandbox Code Playgroud)

我想要一种更干净,更Pythonic的转换方式.我目前有一种笨拙的方式:

def readline(): 
    return sys.stdin.readline().strip().split()

natimes = [tuple(readline()) for _ in xrange(linesofinput))]
for i, (a,b) in enumerate(natimes):
    c = int(a.split(':')[0])* 60 + int(a.split(':')[1])
    d = int(b.split(':')[0])* 60 + int(b.split(':')[1])
    natimes[i] = (c,d)
Run Code Online (Sandbox Code Playgroud)

只是不觉得我在这里正确使用Python.

mgi*_*son 5

使用功能:

def time_to_int(time):
    mins,secs = time.split(':')
    return int(mins)*60 + int(secs)

def line_to_tuple(line):
    return tuple(time_to_int(t) for t in line.split())

natimes = [line_to_tuple(line) for line in sys.stdin]
Run Code Online (Sandbox Code Playgroud)