gpt4 book ai didi

python - 嵌套字典中的平均值

转载 作者:太空宇宙 更新时间:2023-11-04 07:18:59 27 4
gpt4 key购买 nike

我想创建一个新的值列表,my_qty,其中每个项目等于 d[key]['qty'] 中所有值的平均值其中 d[key]['start date'] 匹配 my_dates 中的值。我想我已经很接近了,但是我被嵌套部分挂断了。

import datetime
import numpy as np
my_dates = [datetime.datetime(2014, 10, 12, 0, 0), datetime.datetime(2014, 10, 13, 0, 0), datetime.datetime(2014, 10, 14, 0, 0)]

d = {
'ID1' : {'start date': datetime.datetime(2014, 10, 12, 0, 0) , 'qty': 12},
'ID2' : {'start date': datetime.datetime(2014, 10, 13, 0, 0) , 'qty': 34},
'ID3' : {'start date': datetime.datetime(2014, 10, 12, 0, 0) , 'qty': 35},
'ID4' : {'start date': datetime.datetime(2014, 10, 11, 0, 0) , 'qty': 40},
}

my_qty = []
for item in my_dates:
my_qty.append([np.mean(x for x in d[key]['qty']) if d[key]['start date'] == my_dates[item]])

print my_qty

期望的输出:

[23.5,34,0]

阐明每个请求的输出:

[average of d[key]['qty'] where d[key]['start date '] == my_dates[0], average of d[key]['qty'] where d[key]['start date '] == my_dates[1], average of d[key]['qty'] where d[key]['start date '] == my_dates[2],]

最佳答案

使用纯 python

简单的方法是将数量按日期分组到字典中:

import collections

quantities = collections.defaultdict(lambda: [])

for k,v in d.iteritems():
quantities[v["start date"]].append(v["qty"])

然后运行该字典以计算均值:

means = {k: float(sum(q))/len(q) for k,q in quantities.iteritems()}

给予:

>>> means
{datetime.datetime(2014, 10, 11, 0, 0): 40.0,
datetime.datetime(2014, 10, 12, 0, 0): 23.5,
datetime.datetime(2014, 10, 13, 0, 0): 34.0}

如果您想变得更聪明,可以通过保持当前平均值和您所看到的值的总数来一次计算平均值。您甚至可以在类中对此进行抽象:

class RunningMean(object):
def __init__(self, mean=None, n=0):
self.mean = mean
self.n = n

def insert(self, other):
if self.mean is None:
self.mean = 0.0
self.mean = (self.mean * self.n + other) / (self.n + 1)
self.n += 1

def __repr__(self):
args = (self.__class__.__name__, self.mean, self.n)
return "{}(mean={}, n={})".format(*args)

一次通过你的数据就会给你答案:

import collections
means = collections.defaultdict(lambda: RunningMean())
for k,v in d.iteritems():
means[v["start date"]].insert(v["qty"])

与 Pandas

真正简单的方法是使用 pandas图书馆,因为它是为这样的事情而制作的。这是一些代码:

import pandas as pd
df = pd.DataFrame.from_dict(d, orient="index")
means = df.groupby("start date").aggregate(np.mean)

给予:

>>> means
qty
start date
2014-10-11 40.0
2014-10-12 23.5
2014-10-13 34.0

关于python - 嵌套字典中的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28102433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com