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

问题描述

我已经创建了一个timedelta对象的列表,我需要得到这个列表的平均值。当我尝试做

  return(sum(delta_list)/(len(delta_list)-1))

我得到TypeError:不支持的操作数类型为+:'int'和'datetime.timedelta'



我是使用pythons datetime类的新功能。我也想知道如何把这个平均值变成一个格式,就像天:小时:分钟:我不需要任何小于分钟的数据。

解决方案

sum 需要一个起始值,即$ code> 0 默认情况下,但 0 不能添加到 timedelta ,以便您收到错误。



你只需要提供 sum a timedelta()作为起始值:

 #这是平均
返回总和(delta_list,timedelta())/ len (delta_list)

要打印出来,你可以这样做:

  print str(some_delta)

如果你想要一些自定义的东西,你可以得到 some_delta.days some_delta.seconds ,但你必须计算之间的一切。 p>

I have created a list of timedelta objects and i need to get the average of this list. when i try to do

return (sum(delta_list)/(len(delta_list)-1))

i get TypeError: unsupported operand type(s) for +: 'int' and 'datetime.timedelta'

i am new at working with pythons datetime classes. I also would like to know how to get this average into a format like days: hours: mins: i don't need anything smaller than mins.

解决方案

sum wants a starting value, which is 0 by default, but 0 can't be added to a timedelta so you get the error.

You just have to give sum a timedelta() as the start value:

# this is the average
return sum(delta_list, timedelta()) / len(delta_list)

To print it out you can do this:

print str(some_delta)

If you want something customized you can get some_delta.days and some_delta.seconds but you have to calculate everything between.

这篇关于需要从timedelta对象列表中的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 13:20