删除列表结构中的重复项(python)

时间:2011-08-01 14:10:24

标签: python

我在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'],
]

如何以最小的算法复杂度删除重复项?输出示例:

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

编辑:该列表已按日期排序。 EDIT2:固定的例子 提前谢谢!

1 个答案:

答案 0 :(得分:3)

粗略的做法(而猜测你正在尝试做什么):

#!/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']]