首页 > 解决方案 > 元组列表中的Python时间戳差异

问题描述

我在元组列表中有 FROM Timestamp 和 TO Timestamp 。我想获得元组列表中存在的时间戳差异(以分钟为单位)。

例如:

Timestamp_list = [
    (Timestamp("2021-09-13 07:00:00"), Timestamp("2021-09-13 10:00:00")),
    (Timestamp("2021-09-13 11:00:00"), Timestamp("2021-09-13 12:00:00")),
]

在上面的列表中,和之间的区别Timestamp('2021-09-13 07:00:00')Timestamp('2021-09-13 10:00:00')分钟,和180之间的区别是分钟。我总共有几分钟。列表中可能有 N 个 FROM 和 TO 时间戳。Timestamp('2021-09-13 11:00:00')Timestamp('2021-09-13 12:00:00')60240

预期的结果应该是240分钟。您能帮我用 Python 完成这项工作吗?谢谢你。

标签: python-3.xlisttuples

解决方案


尝试:

out = sum((b - a).seconds for a, b in Timestamp_list) // 60
print(out)

印刷:

240

推荐阅读