If I make a list of time deltas, the average is larger than if I average the microsecond values from these deltas. Why is that?
如果我列一个时间增量的列表,平均值比我对这些增量的微秒值求平均时要大。为什么会这样呢?
import time
import numpy as np
l = []
l_m = []
for _ in range(5):
start = datetime.datetime.now()
time.sleep(1)
l.append((datetime.datetime.now() - start))
l_m.append((datetime.datetime.now() - start).microseconds / 1000000)
print(l)
print(np.mean(l), np.mean(l_m))
gives
赠送
[datetime.timedelta(seconds=1, microseconds=846), datetime.timedelta(seconds=1, microseconds=1017), datetime.timedelta(seconds=1, microseconds=1010), datetime.timedelta(seconds=1, microseconds=1013), datetime.timedelta(seconds=1, microseconds=887)]
0:00:01.000955 0.9639999999999999
This is Python 3.8.10 on Linux.
这是Linux上的Python3.8.10。
更多回答
.microseconds
only gets the microseconds
part of the delta, which does not include the seconds nor minutes nor anything else…
…仅获取增量的微秒部分,不包括秒、分钟或任何其他微秒
优秀答案推荐
Reading the docs, I see that ‘timedelta’ objects can be added. It can also be divided by a scalar so the average of the ‘timedelta’ is correct.
阅读文档后,我发现‘time Delta’对象可以添加。它还可以被标量除以,因此‘time Delta’的平均值是正确的。
Thanks to the comments, I see that the ‘microsecond’ attribute is just the microsecond portion of the ‘timedelta’ and does not include other components such as the seconds values.
多亏了这些评论,我看到‘micro秒’属性只是‘time Delta’的微秒部分,不包括其他组件,如秒值。
更多回答
我是一名优秀的程序员,十分优秀!