我有一个如下所示的元组列表:
temp = [
(1676L, 530L, 'please submit your lab', datetime.datetime(2017, 9, 29, 11, 10, 18), None),
(1677L, 527L, 'this is not the message youre looking for', datetime.datetime(2017, 9, 29, 11, 50, 27), None),
(1675L, 529L, 'hi. please come to my office at 5', datetime.datetime(2017, 9, 29, 11, 10, 8), None),
(1678L, 529L, 'hi. please come to my office at 6', datetime.datetime(2017, 9, 29, 11, 50, 56), None),
(1674L, 528L, 'hi, my name is John', datetime.datetime(2017, 9, 29, 11, 9, 40), None),
(543L, 527L, 'hi, this is message No 499', datetime.datetime(2017, 9, 26, 17, 53, 22), None)
(1630L, 527L, 'hi, I love you', datetime.datetime(2017, 9, 28, 17, 50, 00), None)
]
元组中的第二项是ids(530,527等),第四项是日期时间。列表中可能有几个具有相同id的记录(即第2和第6和第7个记录的id = 527)。
如何在python中编写一个返回唯一记录的代码(它们的id只显示一次),如果它们不是唯一的(列表中有多个具有相同ID的记录),则只返回具有最大日期时间的记录?
答案 0 :(得分:2)
您可以使用dictionary
轻松完成此操作,将第二列作为键。使用datetime
中的ascending order
列对列表进行排序。并且由于密钥不能重复,因此您将拥有最后一个值(如果密钥相同,则值更高)。要获取列表,只需访问values
。
#sort by datetime [3] col
new_t = sorted(temp,key=lambda x: x[3])
#create dictionary with the [1] column as key
max_dict = { x[1]: x for x in new_t }
#get the required list
print([val for val in max_dict.values()])
#driver values
IN :
temp = [ (1676L, 530L, 'please submit your lab', datetime.datetime(2017, 9, 29, 11, 10, 18), None),
(1677L, 527L, 'this is not the message youre looking for', datetime.datetime(2017, 9, 29, 11, 50, 27), None),
(1675L, 529L, 'hi. please come to my office at 5', datetime.datetime(2017, 9, 29, 11, 10, 8), None),
(1678L, 529L, 'hi. please come to my office at 6', datetime.datetime(2017, 9, 29, 11, 50, 56), None),
(1674L, 528L, 'hi, my name is John', datetime.datetime(2017, 9, 29, 11, 9, 40), None),
(543L, 527L, 'hi, this is message No 499', datetime.datetime(2017, 9, 26, 17, 53, 22), None),
(1630L, 527L, 'hi, I love you', datetime.datetime(2017, 9, 28, 17, 50, 00), None) ]
OUT :
[(1674L, 528L, 'hi, my name is John', datetime.datetime(2017, 9, 29, 11, 9, 40), None),
(1678L, 529L, 'hi. please come to my office at 6', datetime.datetime(2017, 9, 29, 11, 50, 56), None),
(1676L, 530L, 'please submit your lab', datetime.datetime(2017, 9, 29, 11, 10, 18), None),
(1677L, 527L, 'this is not the message youre looking for', datetime.datetime(2017, 9, 29, 11, 50, 27), None)]
答案 1 :(得分:-1)
您需要执行以下操作。它不优雅,但很容易理解。基本上只需要创建一个字典,其中每个项目都是具有相同id的元组列表。插入每个元组时,检查其日期以查看它是否大于前一个。这样,“最大”中的所有元组都会出现。字段要么是唯一的,要么具有最大的日期时间。
r = {}
for t in temp:
i = t[1]
if not i in r:
r[i] = {"max":t, "tuples":[t]}
else:
r[i]["tuples"].append(t)
d1 = t[3]
d2 = r[i]["max"][3]
if d1 > d2:
r[i]["max"] = t