📜  Python – 元组列表中的时间字符串到秒

📅  最后修改于: 2022-05-13 01:55:51.419000             🧑  作者: Mango

Python – 元组列表中的时间字符串到秒

给定分钟字符串,转换为元组列表中的总秒数。

方法:使用循环+拆分()

在此,我们使用 split() 分隔分钟和秒组件,并执行数学计算以将值转换为所需的秒数,使用 int() 将字符串转换为整数。

Python3
# Python3 code to demonstrate working of 
# Time Strings to Seconds in Tuple List
# Using loop + split()
  
# initializing list
test_list = [("5:12", "9:45"), ("12:34", "4:50"), ("10:40", )]
  
# printing original list
print("The original list is : " + str(test_list))
  
  
res = []
for sub in test_list:
    tup = tuple()
      
    # iterating each tuple
    for ele in sub:
          
        # perform conversion
        min, sec = ele.split(":")
        secs = 60 * int(min) + int(sec)
        tup += (secs, )
    res.append(tup)
  
# printing result 
print("The corresponding seconds : " + str(res))


输出
The original list is : [('5:12', '9:45'), ('12:34', '4:50'), ('10:40', )]
The corresponding seconds : [(312, 585), (754, 290), (640, )]