Python Groupby语句

我想尝试将以下详细信息列表分组:

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

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

这是上面groupby语句的输出:

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

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

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

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

但我的预期输出是:

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

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

我在某处做错了吗?

解决方法:

您必须先对详细信息进行排序:

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

要么

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

Groupby将连续匹配记录组合在一起.

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.

上一篇:从itertools模块导入izip在Python 3.x中给出了NameError


下一篇:Git切换远程分支