【问题标题】:Merge two annotated results into one dictionary将两个带注释的结果合并到一个字典中
【发布时间】:2012-09-17 13:47:31
【问题描述】:

我必须注释结果:

cred_rec = Disponibilidad.objects.values('mac__mac', 'int_mes').annotate(tramites=Count('fuar'), recibidas=Count('fecha_disp_mac')
cred14   = Disponibilidad.objects.filter(int_disponible__lte=14).values('mac__mac', 'int_mes').annotate(en14=Count('fuar'))

两者都有相同的键'mac__mac''int_mes',我想要创建一个新字典,其中的键为cred_red,加上来自cred14en14

我尝试了一些answers found here,但我错过了一些东西。

谢谢。

编辑。经过一些尝试和错误,我得到了这个:

for linea in cred_rec:
  clave = (linea['mac__mac'], linea['int_mes'])
  for linea2 in cred14:
    clave2 = (linea2['mac__mac'], linea2['int_mes'])
    if clave2 == clave:
      linea['en14'] = linea2['en14']
      linea['disp'] = float(linea2['en14'])/linea['recibidas']*100

现在,我必须寻求更好的解决方案。再次感谢。

======= 编辑 这是输入的样子:

fuar, mac_id, int_mes, int_disponible, int_exitoso, fecha_tramite, fecha_actualiza_pe, fecha_disp_mac
1229012106349,1,7,21,14,2012-07-02 08:33:54.0,2012-07-16 17:33:21.0,2012-07-23 08:01:22.0
1229012106350,1,7,25,17,2012-07-02 09:01:25.0,2012-07-19 17:45:57.0,2012-07-27 17:45:59.0
1229012106351,1,7,21,14,2012-07-02 09:15:12.0,2012-07-16 19:14:35.0,2012-07-23 08:01:22.0
1229012106352,1,7,24,16,2012-07-02 09:25:19.0,2012-07-18 07:52:18.0,2012-07-26 16:04:11.0
... a few  thousand lines dropped ...

fuar 就像一个 order_id; mac__mac 就像site_id,mes 是monthint_disponiblefecha_tramitefecha_disp_mac 之间的时间差; int_exitosofecha_tramitefecha_actualiza_pe 之间的时间差。

输出是这样的:

mac, mes, tramites, cred_rec, cred14,  % rec,  % en 14
1, 7, 2023, 2006, 1313, 99.1596638655, 65.4536390828
1, 8, 1748, 1182, 1150, 67.6201372998, 97.2927241963
2, 8, 731, 471, 441, 64.4322845417, 93.6305732484
3, 8, 1352, 840, 784, 62.1301775148, 93.3333333333
  • tramites 是一个月内所有订单 (fuar) 的总和
  • cred_rec cred是我们的产品,理论上每一个fuar都有一个credcred_rec是一个月内产生的所有cred的总和
  • cred_14 是 14 天内所有 cred 的总和
  • % rec 收到的fuar和产生的cred之间的关系,单位为%
  • % en 14是产生的cred和及时产生的cred的关系

我将在 Annotated Time Line 图表或来自 Google Charts 的 Combo Chart 中使用此表来展示我们制造过程的性能。

感谢您的宝贵时间。

【问题讨论】:

  • 您的输入是什么样的,您希望您的输出是什么样的?
  • 使用信息编辑的问题。
  • 输出涉及“一个月”和“14 天”。我们应该如何处理?
  • 14 days 是 14 天内生产的产品数量。我们在一个月内有一定数量的订单,我们必须在 14 天或更短的时间内完成它们,这是我们的质量目标。我们必须知道我们在 14 天内完成了多少订单以及占多少百分比。

标签: django dictionary django-queryset annotate


【解决方案1】:

对当前代码的一个直接改进是预先计算 en14 和 disp 值并按键索引。这将减少对 cred14 列表的扫描,但会使用内存来存储预先计算的值。

def line_key(line):
      return (line['mac__mac'], line['int_mes'])

cred14_calcs = {}
for line in cred14:
  cred14_calcs[line_key(line)] = {
      'en14': line['en14'],
      'disp': float(line['en14'])/line['recibidas']*100
    }

for line in cred_rec:
  calc = cred14_calcs.get(line_key(line))
  if calc:
    line.update(calc)

【讨论】:

    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 2018-05-13
    • 2019-05-16
    • 2021-09-03
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多