对fetchall()结果的Python列表理解?

对fetchall()结果的Python列表理解?,python,mysql,python-2.7,list,Python,Mysql,Python 2.7,List,因此,我在python中运行了一个查询并获得了一个结果列表,现在在这些结果中,同一个人可以/有多个条目,例如: [ ["1", "someone", "cool", "RO", "AC", "SKST", "yes", "2/24/2017 0:00", "2/24/2017 10:51"], ["102", "another", "person", "RO", "AC", "SKST", "No", "1/26/2015 15:54", "1/26/2015 15:54"],

因此,我在python中运行了一个查询并获得了一个结果列表,现在在这些结果中,同一个人可以/有多个条目,例如:

[
    ["1", "someone", "cool", "RO",  "AC", "SKST", "yes", "2/24/2017 0:00", "2/24/2017 10:51"],
    ["102", "another", "person", "RO", "AC", "SKST", "No", "1/26/2015 15:54", "1/26/2015 15:54"],
    ["102", "another", "person", "RO", "AC", "SKST", "NO", "6/29/2015 0:00", "6/29/2015 12:36"],
    ["102", "another", "person", "RO", "AC", "SKST", "yes", "8/31/2017 0:00", "8/31/2017 13:12"],
    ["62", "again", "someoneelse", "RO", "AC", "SKST", "No", "1/30/2017 0:00", "1/30/2017 13:49"],
    etc...
]
因此,查看这些数据,我们可以看到id为102的人有多个条目,我想过滤这个列表,这样我们每个人只能得到一个条目,并且使用last date字段使其成为最近的条目

因此,对于id为102的人员,我们将删除所有其他条目,只保留最新的条目,日期为:2017年8月31日13:12

我是python新手,所以我不知道该怎么做,提前谢谢。

您可以在Python3中使用itertools.groupby和dateutils:

import itertools
s = [
["1", "someone", "cool", "RO",  "AC", "SKST", "yes", "2/24/2017 0:00", "2/24/2017 10:51"],
["102", "another", "person", "RO", "AC", "SKST", "No", "1/26/2015 15:54", "1/26/2015 15:54"],
["102", "another", "person", "RO", "AC", "SKST", "NO", "6/29/2015 0:00", "6/29/2015 12:36"],
["102", "another", "person", "RO", "AC", "SKST", "yes", "8/31/2017 0:00", "8/31/2017 13:12"],
["62", "again", "someoneelse", "RO", "AC", "SKST", "No", "1/30/2017 0:00", "1/30/2017 13:49"],

]
new_data = [(a, sorted([i[1:] for i in list(b)], key=lambda x:dateutil.parser.parse(x[-1]))) for a, b in itertools.groupby(sorted(s, key=lambda x:x[0]), key=lambda x:x[0])]
final_data = [[a]+b[-1] for a, b in new_data]
for i in final_data:
   print(i)
输出:

['1', 'someone', 'cool', 'RO', 'AC', 'SKST', 'yes', '2/24/2017 0:00', '2/24/2017 10:51']
['102', 'another', 'person', 'RO', 'AC', 'SKST', 'yes', '8/31/2017 0:00', '8/31/2017 13:12']
['62', 'again', 'someoneelse', 'RO', 'AC', 'SKST', 'No', '1/30/2017 0:00', '1/30/2017 13:49']
想要无麻烦且易于掌握的代码


如果您想开发自己的逻辑而不导入任何itertool模块,那么您可以尝试纯python方式:

只是一个意见:

我又为测试用例添加了一个带有两个日期的重复项:

data=[
    ["1", "someone", "cool", "RO",  "AC", "SKST", "yes", "2/24/2017 0:00", "2/24/2017 10:51"],
    ["1", "someone", "cool", "RO",  "AC", "SKST", "yes", "2/25/2017 0:00", "2/26/2017 10:51"],

    ["102", "another", "person", "RO", "AC", "SKST", "No", "1/26/2015 15:54", "1/26/2015 15:54"],
    ["102", "another", "person", "RO", "AC", "SKST", "NO", "6/29/2015 0:00", "6/29/2015 12:36"],
    ["102", "another", "person", "RO", "AC", "SKST", "yes", "8/31/2017 0:00", "8/31/2017 13:12"],
    ["62", "again", "someoneelse", "RO", "AC", "SKST", "No", "1/30/2017 0:00", "1/30/2017 13:49"]
]
from operator import itemgetter

track=[]
no_duplicate=[]
duplicate_dict={}
for index,value in enumerate(data):
    if value[0] not in track:
        track.append(value[0])
        no_duplicate.append(value)
    else:
        if value[0] not in duplicate_dict:
            duplicate_dict[value[0]]=[data[index]]
            duplicate_dict[value[0]].extend([data[index-1]])

        else:
            duplicate_dict[value[0]].extend([data[index]])
            duplicate_dict[value[0]].extend([data[index - 1]])





for index,value in enumerate(no_duplicate):
    for item in [max(value,key=itemgetter(7))for key,value in duplicate_dict.items()]:
        if item[0] in value:
            no_duplicate[index]=item
print(no_duplicate)
输出:

[['1', 'someone', 'cool', 'RO', 'AC', 'SKST', 'yes', '2/25/2017 0:00', '2/26/2017 10:51'], ['102', 'another', 'person', 'RO', 'AC', 'SKST', 'yes', '8/31/2017 0:00', '8/31/2017 13:12'], ['62', 'again', 'someoneelse', 'RO', 'AC', 'SKST', 'No', '1/30/2017 0:00', '1/30/2017 13:49']]

为什么不修改查询?还有什么工具?它不是Python标准库的一部分?pip是否在测试库中安装更多工具。我还得多读一些有关这方面的书。但是,由于您在回答中使用了第三方模块,你应该写一篇关于你在更多itertools中使用的方法如何工作的解释。有很多非常酷的函数扩展了itertools python标准库的功能。你可以在这里找到它。如果你喜欢,请投票。我花了很多时间写这个并与你分享
[['1', 'someone', 'cool', 'RO', 'AC', 'SKST', 'yes', '2/25/2017 0:00', '2/26/2017 10:51'], ['102', 'another', 'person', 'RO', 'AC', 'SKST', 'yes', '8/31/2017 0:00', '8/31/2017 13:12'], ['62', 'again', 'someoneelse', 'RO', 'AC', 'SKST', 'No', '1/30/2017 0:00', '1/30/2017 13:49']]