Python Groupby statement

若如初见. 提交于 2019-12-22 10:51:18

问题


I mam trying to group the following details list:

details = [('20130325','B'), ('20130320','A'), ('20130325','B'), ('20130320','A')]

>>for k,v in itertools.groupby(details,key=operator.itemgetter(0)):
>>  print k,list(v)

And this is the output with the above groupby statement:

20130325 [('20130325', 'B')]

20130320 [('20130320', 'A')]

20130325 [('20130325', 'B')]

20130320 [('20130320', 'A')]

But my expected output was:

20130325 [('20130325', 'B'),('20130325', 'B')]

20130320 [('20130320', 'A'),('20130320', 'A')]

Am I doing wrong somewhere?


回答1:


You have to sort your details first:

details.sort(key=operator.itemgetter(0))

or

fst = operator.itemgetter(0)
itertools.groupby(sorted(details, key=fst), key=fst)

 

Groupby groups consecutive matching records together.

Documentation:

The operation of groupby() is similar to the uniq filter in Unix. It generates a break or new group every time the value of the key function changes (which is why it is usually necessary to have sorted the data using the same key function). That behavior differs from SQL’s GROUP BY which aggregates common elements regardless of their input order.




回答2:


The toolz project offers a non-streaming groupby

$ pip install toolz
$ ipython

In [1]: from toolz import groupby, first

In [2]: details = [('20130325','B'), ('20130320','A'), ('20130325','B'), ('20130320','A')]

In [3]: groupby(first, details)
Out[3]: 
{'20130320': [('20130320', 'A'), ('20130320', 'A')],
 '20130325': [('20130325', 'B'), ('20130325', 'B')]}


来源:https://stackoverflow.com/questions/15636008/python-groupby-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!