我想找到每个日期的总点击次数。我尝试了很多方法,但是找不到合适的方法。下面我将写下字典的目标列表
data = [{"click":"3","time":"2020-08-21T00:00:00.000Z"}, {"click":"6","time":"2020-08-22T10:00:00.000Z"}, {"click":"7","time":"2020-08-22T11:00:00.000Z"}, {"click":"8","time":"2020-08-22T12:00:00.000Z"}, {"click":"29","time":"2020-08-24T00:00:00.000Z"}, {"click":"13","time":"2020-08-25T00:00:00.000Z"}]
在此数据列表中,对于2020-08-22日期,有3个具有不同点击值的字典。所以我想要2020-08-22日期的总点击数是21。
请帮助我获得这样的结果,
data = [{"click":"3","time":"2020-08-21T00:00:00.000Z"}, {"click":"21","time":"2020-08-22T00:00:00.000Z"}, {"click":"29","time":"2020-08-24T00:00:00.000Z"}, {"click":"13","time":"2020-08-25T00:00:00.000Z"}]
感谢您的考虑和即将来临的答复。
这是一个使用的解决方案,itertools.groupby
将日期T
与&使用日期部分拆分为groupby&将值求和。
from itertools import groupbyprint([ {"time": k + "T00:00:00.000Z", "click": sum(int(vv['click']) for vv in v)} for k, v in groupby(data, key=lambda x: x['time'].split("T")[0])])
[{'time': '2020-08-21T00:00:00.000Z', 'click': 3}, ...]
下面
from collections import defaultdictdata = [{"click": "3", "time": "2020-08-21T00:00:00.000Z"}, {"click": "6", "time": "2020-08-22T10:00:00.000Z"}, {"click": "7", "time": "2020-08-22T11:00:00.000Z"}, {"click": "8", "time": "2020-08-22T12:00:00.000Z"}, {"click": "29", "time": "2020-08-24T00:00:00.000Z"}, {"click": "13", "time": "2020-08-25T00:00:00.000Z"}]collected_clicks = defaultdict(int)collected_clicks_lst = []for entry in data: clicks = int(entry['click']) date = entry['time'][:10] collected_clicks[date] += clicksfor date,clicks in collected_clicks.items(): collected_clicks_lst.append({'click': clicks, 'time': date + 'T00:00:00.000Z'})print(collected_clicks_lst)
输出
[{'click': 3, 'time': '2020-08-21T00:00:00.000Z'}, {'click': 21, 'time': '2020-08-22T00:00:
只是python :)
groupedData = {}for oneSet in data: setDate = oneSet["time"].split("T")[0] if setDate not in groupedData: groupedData[setDate]= int(oneSet["click"]) else: groupedData[setDate]+=int(oneSet["click"])#Data grouped according to daterefinedData = [{"click":str(groupedData[key]),"time":key+"T00:00:00.000Z"}for key in groupedData]
输出量
[{'click':'3','time':'2020-08-21T00:00:00.000Z'},
{'click':'21','time':'2020-08-22T00:00: 00.000Z'},
{'click':'29','time':'2020-08-24T00:00:00.000Z'},
{'click':'13','time':'2020-08- 25T00:00:00.000Z'}]