2011-08-01 89 views
-3

我在蟒蛇的结构如下:删除重复的表结构(蟒蛇)

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

]

编辑:列表已经通过日期排序。 编辑2:修正示例 提前致谢!

+4

您尝试在几十上侧相关问题的解决方案之一。 –

+0

我一直在检查它,我认为迭代是我找到的最简单和最不复杂的解决方案。 – Gandi

+1

您的示例在语法上不正确。 – deStrangis

回答

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']]