我有两个查询集,我希望与“月份”相结合。是结合的独特关键。
**查询集1(展示次数)**
[{'events': 1503, 'month': datetime.datetime(2018, 6, 1, 0, 0)}, {'events': 32747, 'month': datetime.datetime(2018, 5, 1, 0, 0)}]
**查询集2(点击次数)**
[{'events': 163, 'month': datetime.datetime(2018, 6, 1, 0, 0)}, {'events': 4184, 'month': datetime.datetime(2018, 5, 1, 0, 0)}, {'events': 5049, 'month': datetime.datetime(2018, 4, 1, 0, 0)}, {'events': 592, 'month': datetime.datetime(2018, 3, 1, 0, 0)}]
`
是否可以将它们组合起来,使得输出看起来像这样?
[{'clicks': 163, 'impressions': 1503, 'month': datetime.datetime(2018, 6, 1, 0, 0)}, {'clicks': 4184, 'impressions':32747, 'month': datetime.datetime(2018, 5, 1, 0, 0)}, {'events': 5049, 'month': datetime.datetime(2018, 4, 1, 0, 0)}, {'events': 592, 'month': datetime.datetime(2018, 3, 1, 0, 0)}]
答案 0 :(得分:1)
您可以使用以下代码片段
clicks = list(clicks.values())
impressions = list(impressions.values())
res = {i['month'] : {'month': i['month']} for i in clicks + impressions}
for i in clicks:
res[i['month']].update({'clicks': i['events']})
for i in impressions:
res[i['month']].update({'impressions': i['events']})
res = list(dict.values())