开发者

Delete duplicates in list structure (python)

开发者 https://www.devze.com 2023-03-24 02:26 出处:网络
I have the following structure in python: revisions = [ [\'01.02.2010\',\'abc\',\'qwe\'], [\'02.02.2010\',\'abc\',\'qwe\'],

I have the following structure in python:

revisions = [
['01.02.2010','abc','qwe'],
['02.02.2010','abc','qwe'],
['03.02.2010','aaa','qwe'],
['04.02.2010','aaa','qwe'],
['05.02.2010','aaa','qwe'],
['06.02.2010','aaa','dsa'],
]

how can i remove the duplicates with minimum algorithmic complexity? Output example:

 revisions = [
['01.02.2010','abc','qwe'],
[开发者_Go百科'03.02.2010','aaa','qwe'],
['06.02.2010','aaa','dsa'],

]

EDIT: the list is already ordered by date. EDIT2: Fixed example Thanks in advance!


A crude approach (while guessing what you're trying to do):

#!/usr/bin/env python

import pprint

revisions = [
    ['01.02.2010','abc','qwe'],
    ['02.02.2010','abc','qwe'],
    ['03.02.2010','aaa','qwe'],
    ['04.02.2010','aaa','qwe'],
    ['05.02.2010','aaa','qwe'],
    ['06.02.2010','aaa','dsa'],
]

uniq, seen = [], set() # sets have O(1) membership tests

for rev in revisions:
    if tuple(rev[1:]) in seen:
        continue
    else:
        seen.add(tuple(rev[1:]))
        uniq.append(rev)

pprint.pprint(uniq)

# prints:
# [['01.02.2010', 'abc', 'qwe'],
#  ['03.02.2010', 'aaa', 'qwe'],
#  ['06.02.2010', 'aaa', 'dsa']]
0

精彩评论

暂无评论...
验证码 换一张
取 消