使用python将dict值分组为块

时间:2014-01-13 17:52:28

标签: python itertools

我正在尝试找出一种方法来将字典值分组为间隔,具体取决于密钥的值。

就我而言,我有两个键:'timestamp''value';我需要根据值按间隔对其进行分组。我的数据结构是这样的:

[{'timestamp': u'1389631816', 'value': u'0'},
 {'timestamp': u'1389633136', 'value': u'0'},
 {'timestamp': u'1389633256', 'value': u'1'},
 {'timestamp': u'1389633316', 'value': u'1'},
 {'timestamp': u'1389633196', 'value': u'0'},
 {'timestamp': u'1389633196', 'value': u'0'},
 {'timestamp': u'1389633196', 'value': u'0'},
 {'timestamp': u'1389633316', 'value': u'1'}]

在这种情况下,我应该有4组:

First Group: 2 items, based on value '0';
Second Group: 2 items, based on value '1';
Third Group: 3 items, based on value '0';
Fourth Group: 1 item, based on value '1'.

出于所有目的,我需要这些组之间的时间度量(在此示例中来自Zabbix的ICMP检查)来创建报告,但我真的被困在这里。

1 个答案:

答案 0 :(得分:4)

使用itertools.groupby()功能对这些进行分组:

from itertools import groupby
from operator import itemgetter

for value, group in groupby(list_of_dicts, key=itemgetter('value')):
    print 'Group for value {}'.format(value)
    for d in group:
        print d

演示:

>>> from itertools import groupby
>>> from operator import itemgetter
>>> list_of_dicts = [{'timestamp': u'1389631816', 'value': u'0'},
...  {'timestamp': u'1389633136', 'value': u'0'},
...  {'timestamp': u'1389633256', 'value': u'1'},
...  {'timestamp': u'1389633316', 'value': u'1'},
...  {'timestamp': u'1389633196', 'value': u'0'},
...  {'timestamp': u'1389633196', 'value': u'0'},
...  {'timestamp': u'1389633196', 'value': u'0'},
...  {'timestamp': u'1389633316', 'value': u'1'}]
>>> for value, group in groupby(list_of_dicts, key=itemgetter('value')):
...     print 'Group for value {}'.format(value)
...     for d in group:
...         print d
... 
Group for value 0
{'timestamp': u'1389631816', 'value': u'0'}
{'timestamp': u'1389633136', 'value': u'0'}
Group for value 1
{'timestamp': u'1389633256', 'value': u'1'}
{'timestamp': u'1389633316', 'value': u'1'}
Group for value 0
{'timestamp': u'1389633196', 'value': u'0'}
{'timestamp': u'1389633196', 'value': u'0'}
{'timestamp': u'1389633196', 'value': u'0'}
Group for value 1
{'timestamp': u'1389633316', 'value': u'1'}