Python Groupby statement

北城以北 提交于 2019-12-05 18:49:11

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.

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