【问题标题】:Count until specific condition in list of dictionary provide the mean of the value计数直到字典列表中的特定条件提供值的平均值
【发布时间】:2020-01-01 20:10:42
【问题描述】:

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

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,我们主要根据类进行区分。我得到了每个类中的以下类型总数,我关心的是只有代码 6501 和 6502。

已经拿到这张桌子了:

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

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

【问题讨论】:

    标签: python dictionary-comprehension


    【解决方案1】:

    我是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})}}
    

    【讨论】:

    • 非常感谢它有效,你能解释一下` d.setdefault(e['id'], {}).setdefault(e['class'], collections.Counter())`这行代码以及 double setdefault 在这里如何工作..
    • d.setdefault(e['id'], {}) 表示d[e'id']] 如果e['id'] 是d 的键,否则设置d[e'id']] = {} 并返回新创建的字典。这个表达式是一个字典X,如果e['class']是X的一个键,你可以将它与另一个X.setdefault(e['class'], collections.Counter())链接,即X[e['class']],否则设置X[e['class']] = collections.Counter()并返回新创建的计数器。基本上,defaultdict 在 dict 中创建一个新条目并返回值。
    猜你喜欢
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多