【问题标题】:How to seperate list of hours:minutes:seconds in python?如何在 python 中分隔小时列表:分钟:秒?
【发布时间】:2023-01-07 00:06:24
【问题描述】:

你好,我想分开一个看起来像这样的列表:

04:05:43.0
04:05:44.0
04:05:45.0
04:05:46.0
04:05:47.0
04:05:48.0
04:05:49.0
04:05:50.0
04:05:51.0
04:05:52.0
04:05:53.0
04:05:54.0
04:05:55.0
04:05:56.0
04:05:57.0
04:05:58.0
04:05:59.0
04:06:00.0
04:06:01.0
04:06:02.0
04:06:03.0
04:06:08.0

到 3 个单独的列表,包含小时/分钟和秒

预期结果:

hrs = [4, 4, 4, 4, 4, 4, ......]
mins = [5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,.... ]
secs = [1, 2, 3, 4, 5, 6,.....]

【问题讨论】:

  • 只需使用 your_string.split(":") 并用 0、1 和 2 对其进行索引

标签: python loops datetime


【解决方案1】:

这段代码正是你想要的!

list_ = [
    '04:05:43.0',
    '04:05:44.0',
    '04:05:45.0'
]

# create 3 different lists, seperating hrs mins and secs
hrs = []
mins = []
secs = []

# loop through the list and split the strings into 3 different lists
for i in list_:
    hrs.append(int(i.split(':')[0]))
    mins.append(int(i.split(':')[1]))
    secs.append(int(i.split(':')[2].split('.')[0]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多