我输入了这样的时间:
09:00 12:00
10:00 13:00
11:00 12:30
12:02 15:00
09:00 10:30
我正在尝试将它构建到一个元组列表中,转换为分钟:
[(540, 720), (600, 780), (660, 750), (722, 900), (540, 630)]
我想要一个更干净,更像Python的转化方式。我现在有个笨拙的方法:
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)
只是感觉我在这里没有正确地使用Python。你知道吗
避免重复像
str.split
这样昂贵的操作。下面是一个简单的答案:使用功能:
下面是另一个选项,使用正则表达式:
相关问题 更多 >
编程相关推荐