最早日期的Python查询列表

时间:2016-09-27 20:31:49

标签: python list date dictionary time

我的查询在列表中返回以下内容:

"Alex";"275467125";"2015-02-03 02:55:36-05";"1"
"Alex";"275467125";"2015-01-13 02:09:39-05";"1"
"Alex";"275467125";"2015-01-05 04:13:35-05";"1"
"Alex";"275467125";"2014-12-27 04:55:47-05";"1"
"Alex";"275467125";"2014-12-27 04:54:52-05";"1"
"Alex";"275467125";"2014-12-07 03:13:24-05";"1"
"Alex";"275467125";"2014-12-04 03:34:56-05";"1"
"Alex";"275467125";"2014-12-02 04:16:33-05";"1"
"Ali";"275464747";"2016-02-17 10:52:12-05";"2"
"Alladin";"275467455";"2016-03-13 06:51:52-04";"2"
"Alladin";"275467455";"2016-03-13 06:51:47-04";"2"
"Anna";"275467401";"2016-03-26 03:56:41-04";"1"
"Anna";"275467401";"2016-03-26 03:55:21-04";"1"
"Anna";"275467401";"2016-03-21 23:04:28-04";"1"
"Anna";"275467401";"2016-02-12 13:24:44-05";"1"
"Anna";"275467401";"2015-12-03 08:20:35-05";"1"
"Anna";"275467401";"2015-11-09 04:18:27-05";"1"
"Anna";"275467401";"2015-11-09 04:11:59-05";"1"
"Anna";"275467401";"2015-09-13 21:27:12-04";"1"

我想用他们拥有的最早的记录创建一个人名字典。我已经想通了:

oldestlist = {d[0]:d[2] for d in records}

这会返回一个正确的答案,但我担心的是,如果我出现的列表没有按日期/时间的降序格式化,则无法提供正确的答案。创建具有名称和最早日期的字典的最佳方法是什么?

4 个答案:

答案 0 :(得分:2)

您无需对任何数据进行排序,只需使用 defaultdict 并检查当前日期与任何新日期并相应更新:

s = """"Alex";"275467125";"2015-02-03 02:55:36-05";"1"
"Alex";"275467125";"2015-01-13 02:09:39-05";"1"
"Alex";"275467125";"2015-01-05 04:13:35-05";"1"
"Alex";"275467125";"2014-12-27 04:55:47-05";"1"
"Alex";"275467125";"2014-12-27 04:54:52-05";"1"
"Alex";"275467125";"2014-12-07 03:13:24-05";"1"
"Alex";"275467125";"2014-12-04 03:34:56-05";"1"
"Alex";"275467125";"2014-12-02 04:16:33-05";"1"
"Ali";"275464747";"2016-02-17 10:52:12-05";"2"
"Alladin";"275467455";"2016-03-13 06:51:52-04";"2"
"Alladin";"275467455";"2016-03-13 06:51:47-04";"2"
"Anna";"275467401";"2016-03-26 03:56:41-04";"1"
"Anna";"275467401";"2016-03-26 03:55:21-04";"1"
"Anna";"275467401";"2016-03-21 23:04:28-04";"1"
"Anna";"275467401";"2016-02-12 13:24:44-05";"1"
"Anna";"275467401";"2015-12-03 08:20:35-05";"1"
"Anna";"275467401";"2015-11-09 04:18:27-05";"1"
"Anna";"275467401";"2015-11-09 04:11:59-05";"1"
"Anna";"275467401";"2015-09-13 21:27:12-04";"1"
"""

import  csv
from collections import defaultdict

d = defaultdict(str)
for name,_, date, _ in csv.reader(s.splitlines(), delimiter=";"):
    if not d[name] or d[name] > date:
        d[name] = date


from pprint import pprint as pp

pp(dict(d))

输出:

 {'Alex': '2014-12-02 04:16:33-05',
 'Ali': '2016-02-17 10:52:12-05',
'Alladin': '2016-03-13 06:51:47-04',
'Anna': '2015-09-13 21:27:12-04'}

因为日期是 y-m-d time 格式,所以可以安全地进行字典比较。

答案 1 :(得分:1)

获得你给出的"列表"有点令人沮丧。到一个实际的列表格式。如果您无法在查询中处理此任务,可以尝试:

from itertools import groupby
from operator import itemgetter

lst = '''"Alex";"275467125";"2015-02-03 02:55:36-05";"1",
"Alex";"275467125";"2015-01-13 02:09:39-05";"1",
"Alex";"275467125";"2015-01-05 04:13:35-05";"1",
"Alex";"275467125";"2014-12-27 04:55:47-05";"1",
"Alex";"275467125";"2014-12-27 04:54:52-05";"1",
"Alex";"275467125";"2014-12-07 03:13:24-05";"1",
"Alex";"275467125";"2014-12-04 03:34:56-05";"1",
"Alex";"275467125";"2014-12-02 04:16:33-05";"1",
"Ali";"275464747";"2016-02-17 10:52:12-05";"2",
"Alladin";"275467455";"2016-03-13 06:51:52-04";"2",
"Alladin";"275467455";"2016-03-13 06:51:47-04";"2",
"Anna";"275467401";"2016-03-26 03:56:41-04";"1",
"Anna";"275467401";"2016-03-26 03:55:21-04";"1",
"Anna";"275467401";"2016-03-21 23:04:28-04";"1",
"Anna";"275467401";"2016-02-12 13:24:44-05";"1",
"Anna";"275467401";"2015-12-03 08:20:35-05";"1",
"Anna";"275467401";"2015-11-09 04:18:27-05";"1",
"Anna";"275467401";"2015-11-09 04:11:59-05";"1",
"Anna";"275467401";"2015-09-13 21:27:12-04";"1"'''

broken_list = lst.split(',')
stripped = [item.replace('\n', '') for item in broken_list]

rebuilt = []
for line in stripped:
    line = line.split(';')
    rebuilt.append([item.strip('"') for item in line])

# Now actually sorting this        
grouped = []
for key, group in groupby(rebuilt, key=itemgetter(0)):
    grouped.append(list(group))

sort_grouped = [sorted(item, key=itemgetter(2)) for item in grouped]
#sort_grouped = 

oldestlist = {d[0][0]:d[0][2] for d in sort_grouped}

答案 2 :(得分:0)

由于您需要为每个name创建最后一条记录,而不是使用dict明确地执行该记录,因此请使您的查询集在名称上执行GROUP BY。在Django中,您可以使用.annotate执行此操作,如此处所述:Django Orm get latest for each group

因此,你的querset应该是:

YourModel.objects.values('name_column').annotate(latest_date=Max('date'))

附加信息,您应该在查询集中使用order_by(-your_date_column),以确保始终以desc顺序返回数据,其中-确保列表顺序时的desc顺序需要。

答案 3 :(得分:0)

我非常接近。我发现效果最好的答案是我的原始代码的调整,但使用sorted()函数。

对于最新的我会这样做:

newestlist = {d[0]:d[2] for d in sorted(records)}

对于我做过的最年长的人:

oldestlist = {d[0]:d[2] for d in sorted(records, reverse=True)}

感谢所有回答的人。如果我使用查询集,我会记住django引用。