计数直到字典列表中的特定条件提供值的平均值

Count until specific condition in list of dictionary provide the mean of the value

我正在尝试计算特定代码之前的类型数,同时使用 for 循环。我得到了一个字典列表,我能够计算每种类型,但我还需要计算到代码 65016502。就像我们在这个特定代码之前的类型总数。

group=[
    {'id': '123ed45','type': 'C','code':6500,'class':1,'title':'orange'},
    {'id': '123ed45','type': 'B','code':6515,'class':1,'title':'lemon'},
    {'id': '123ed45','type': 'A','code':6501,'class':1,'title':'RedApple'},
    {'id': '123ed45','type': 'C','code':6555,'class':1,'title':'banana'},
    {'id': '123ed45','type': 'B','code':6502,'class':1,'title':'Redcarrot'},
    {'id': '123ed45','type': 'A','code':6523,'class':1,'title':'pomo'}]

    [{'id': '123ed45','type': 'B','code':6555,'class':2,'title':'banana'},
    {'id': '123ed45','type': 'A','code':6501,'class':2,'title':'RedApple'},
    {'id': '123ed45','type': 'C','code':6546,'class':2,'title':'dragonfruit'},
    {'id': '123ed45','type': 'A','code':6502,'class':2,'title':'carrot'},
    {'id': '123ed45','type': 'A','code':6501,'class':2,'title':'RedApple'},
    {'id': '123ed45','type': 'B','code':6511,'class':2,'title':'pineapple'}]

    [{'id': '123ed45','type': 'A','code':6502,'class':3,'title':'Redcarrot'},
    {'id': '123ed45','type': 'C','code':6545,'class':3,'title':'lime'},
    {'id': '123ed45','type': 'B','code':6502,'class':3,'title':'carrot'},
    {'id': '123ed45','type': 'A','code':6501,'class':3,'title':'GreenApple'},
    {'id': '123ed45','type': 'C','code':6534,'class':3,'title':'grape'},
    {'id': '123ed45','type': 'A','code':6502,'class':3,'title':'carrot'}
]

同一组数据可用于不同的 id,我们主要在 class 上分开。我在每个 class 中得到以下类型总数,我只关心代码 6501 和 6502。

已经收到了 table:

此外,我需要计算代码 6502 和 6501 之前的类型数,并需要通过排除这些代码来计算它的平均值,所需的输出如下所示:

我尝试使用 more-itertoolsspilt_at。这是一个很好的尝试还是有其他方法可以做到这一点?

我是 more-itertools 的谦虚参与者并且喜欢这个库,但你在这里不需要它。使用 enumerate 保持当前组中的当前索引相当容易:

import collections
from pprint import pprint

d = {}

for group in groups:
    for i, e in enumerate(group):
        # ('id', 'class') is the identifier of a line
        columns = d.setdefault(e['id'], {}).setdefault(e['class'], collections.Counter())
        columns[e['type']] += 1         # store the number of A, B, C
        s = "{}_{}".format(e['code'], e['title']) # build "6502_carrot" string
        if "before_"+s not in columns:  # if "before_6502_carrot" is met for the first time
            columns["before_"+s] = i    # store the current index.
        columns[s] += 1                 # store the number of "6502_carrot"

pprint(d)

输出:

{'123ed45': {1: Counter({'before_6523_pomo': 5,
                         'before_6502_Redcarrot': 4,
                         'before_6555_banana': 3,
                         'C': 2,
                         'B': 2,
                         'A': 2,
                         'before_6501_RedApple': 2,
                         '6500_orange': 1,
                         'before_6515_lemon': 1,
                         '6515_lemon': 1,
                         '6501_RedApple': 1,
                         '6555_banana': 1,
                         '6502_Redcarrot': 1,
                         '6523_pomo': 1,
                         'before_6500_orange': 0}),
             2: Counter({'before_6511_pineapple': 5,
                         'A': 3,
                         'before_6502_carrot': 3,
                         'B': 2,
                         '6501_RedApple': 2,
                         'before_6546_dragonfruit': 2,
                         '6555_banana': 1,
                         'before_6501_RedApple': 1,
                         'C': 1,
                         '6546_dragonfruit': 1,
                         '6502_carrot': 1,
                         '6511_pineapple': 1,
                         'before_6555_banana': 0}),
             3: Counter({'before_6534_grape': 4,
                         'A': 3,
                         'before_6501_GreenApple': 3,
                         'C': 2,
                         'before_6502_carrot': 2,
                         '6502_carrot': 2,
                         '6502_Redcarrot': 1,
                         'before_6545_lime': 1,
                         '6545_lime': 1,
                         'B': 1,
                         '6501_GreenApple': 1,
                         '6534_grape': 1,
                         'before_6502_Redcarrot': 0})}}