我有一个django模型ReportDashboard,根据日期和时间有相同报告的多行。我想把单个结果作为字典的名称作为字典的键,并且值是来自其他行的所有updated_on的列表。
"""Required OUTPUT"""
{'Daily Pickup':[2016-04-19 11:48:00,2016-04-18 11:40:00, 2016-04-17 10:30:00]}
{'Surface MIS LHRTO':[2016-04-19 11:52:00, DATE2,DATE3,DATE4,DATE5, SO ON]}
{...........}
{...........}
Django模型
repo=ReportDashboard.objects.filter(status=2)
for r in repo:
print r.report_name, r.updated_on
输出
Daily Pickup 2016-04-19 11:48:00
Surface MIS LHRTO 2016-04-19 11:52:00
Daily Pickup 2016-04-18 11:40:00
Daily Pickup 2016-04-17 10:30:00
答案 0 :(得分:0)
在python中最容易完成(groupby在@Anentropic中指出是很方便的):
from itertools import groupby
repos = [(r.report_name, r.updated_on) for r in repo]
repos.sort() # sorts by name and date
# group by name and collect dates, use as key/value in dict-comprehension
result = {k: [r[1] for r in g] for k, g in groupby(repos, lambda x: x[0])}