本文介绍了如何从timedelta对象中删除微秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了平均时间的计算,我希望显示的结果平均值不以微秒为单位.

I do a calculation of average time, and I would like to display the resulted average without microseconds.

avg = sum(datetimes, datetime.timedelta(0)) / len(datetimes)

推荐答案

获取时间增量并删除其自己的微秒,即微秒和只读属性:

Take the timedelta and remove its own microseconds, as microseconds and read-only attribute:

avg = sum(datetimes, datetime.timedelta(0)) / len(datetimes)
avg = avg - datetime.timedelta(microseconds=avg.microseconds)

如果经常需要,您可以创建自己的小功能:

You can make your own little function if it is a recurring need:

import datetime

def chop_microseconds(delta):
    return delta - datetime.timedelta(microseconds=delta.microseconds)

我还没有找到更好的解决方案.

I have not found a better solution.

这篇关于如何从timedelta对象中删除微秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 19:55