直接上例子:
rs= [
... {
... "datetime": "Mon, 31 Aug 2015 23:00:00 GMT",
... "id": 1,
... "name":"a"
... },
... {
... "datetime": "Mon, 31 Aug 2015 23:00:00 GMT",
... "id": 2,
... "name":"b"
... },
... {
... "datetime": "Mon, 31 Aug 2015 23:00:00 GMT",
... "id": 3,
... "name":"c"
... },
... {
... "datetime": "Mon, 31 Aug 2015 23:00:00 GMT",
... "id": 4,
... "name":"d"
... }
... ]
fun = operator.itemgetter('id', 'name')
for key, g in itertools.groupby(rs, fun):
... print key
... print g
...
(1, 'a')
<itertools._grouper object at 0x7f07e9623650>
(2, 'b')
<itertools._grouper object at 0x7f07e96236d0>
(3, 'c')
<itertools._grouper object at 0x7f07e9623650>