Python - 每次修改时找到整个字典平均值的最快方法?
我正在尝试找到最快/最有效的方法来从字典中提取平均值。我正在处理的任务要求它执行数千次,因此每次简单地迭代字典中的所有值来查找平均值将是完全低效的。成百上千的新键、值对被添加到字典中,每次发生这种情况时我们都需要找到平均值。我们还需要在每次值更新时找到新的平均值,这会发生数千次。
预先感谢——这是一个很棒的地方。
I'm trying to find the fastest/most efficient way to extract the average value from a dict. The task I'm working on requires that it do this thousands of times, so simply iterating over all the values in the dict each time to find the average would be entirely inefficient. Hundreds and hundreds of new key,value pairs get added to the dict and we need to find the average value each time this occurs. We also need to find the new average value each time a value gets updated, which occurs thousands of times.
Thanks in advance--this is such an awesome place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建自己的 dict 子类来跟踪计数和总计,然后可以快速返回平均值:
Create your own dict subclass that tracks the count and total, and then can quickly return the average:
以下基于运行平均值,因此如果您知道之前的平均值:
如果您保留元素总和,则它的更简单的兄弟会起作用:
当删除值时,您可以执行类似的操作:
当更新值时:
The following is based on running average, so if you know the previous average:
Its simpler brother works if you keep tab of the sum of the elements:
When a value is deleted, you can do a similar thing:
And when a value is updated:
继承自
dict
,每次调用__setitem__
时计算平均值。由于您可以将先前的平均值存储在字典类中,并且仅对这个值和添加的新值进行平均,所以这应该非常快 - 第一次添加新项目时,平均值就是这个值的平均值。
Inherit from
dict
and calculate the average value each time__setitem__
is called.Since you can store the previous average in your dictionary class and only average this and the new value that is added, that should be pretty fast - the first time a new item is added, the average value is simply that of this value.