我有一个列表如下:
list=[
('2013-01-04', u'crid2557171372', 1),
('2013-01-04', u'crid9904536154', 719677),
('2013-01-04', u'crid7990924609', 577352),
('2013-01-04', u'crid7990924609', 399058),
('2013-01-04', u'crid9904536154', 385260),
('2013-01-04', u'crid2557171372', 78873)
]
问题是第二列有重复id,但计数不同。我需要有一个清单,将汇总计数,使名单看起来像这样。python中是否有group by cluase?你知道吗
list=[
('2013-01-04', u'crid9904536154', 1104937),
('2013-01-04', u'crid7990924609', 976410),
('2013-01-04', u'crid2557171372', 78874)
]
“漫长”的道路:
让我们把你的列表命名为
a
,而不是list
(list
在Python中是一个非常有用的函数,我们不想屏蔽它):b
现在是:我不认为有任何内置的工具,可以做什么,你想在开箱即用。但是,使用
collections
模块中的defaultdict
可以很容易地实现自己的功能:这只需要线性运行时间,因此如果数据集很大,它可能比
groupby
实现要好,后者需要O(log n)
运行时间排序。你知道吗相关问题 更多 >
编程相关推荐