【发布时间】: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