Python:在进行重复计数时删除重复的元素

时间:2014-06-11 19:49:01

标签: python for-loop duplicates tuples

以下是3D点组的元组

f = [[10, 20, 0], 
    [40, 20, 30], 
    [20, 0, 30], 
    [10, 10, 0], 
    [30, 10, 10], 
    [20, 0, 30], 
    [20, 10, 20], 
    [10, 10, 0]]

每个点对应一个指示点类型(假设)的数字(索引)

ic=[1,2,3,2,1,3,2,3,1]

因此,前一个元组可以表示为

f = [[10, 20, 0, 1], 
    [40, 20, 30, 2], 
    [20, 0, 30, 3], 
    [10, 10, 0, 2], 
    [30, 10, 10, 1], 
    [20, 0, 30, 3], 
    [20, 10, 20, 2], 
    [10, 10, 0, 3],
    [20, 0, 30, 1]]

这是我的代码:

def selectUnique(x):
    count = Counter((i[0], i[1], i[2]) for i in x)
    out = [i for i in x if count[(i[0], i[1], i[2])] == 1]
    return out

selectUnique(f)

我想获得的结果如下所示:

独特的部分:

index       point         equivalent points    index
  0      [10, 20, 0, 1]           1             [1]
  1      [40, 20, 30, 2]          1             [2]
  2      [20, 0, 30, 3]           3             [3,3,1]
  3      [10, 10, 0, 2]           2             [2,3]
  4      [30, 10, 10, 1]          1             [1]
  6      [20, 10, 20, 2]          1             [2]

重复部分:

index       point         Duplicate or not
  5      [20, 0, 30, 3]       duplicate
  7      [10, 10, 0, 3]       duplicate
  8      [20, 0, 30, 1]       duplicate

我的代码旨在实现挑选重复点的功能,并在列表中指明其索引。另外,我还需要实现功能,显示我的独特部分中有多少等效点以及这些等效点的索引。

但是,我的代码不起作用,我该如何修改呢?

4 个答案:

答案 0 :(得分:2)

uniq = []
dup = []
count = 0
for i, j, k  in f:
    if not [f.index([i,j,k]),i,j,k] in uniq:
        uniq.append([count,i,j,k])
    else:
        dup.append([count,i,j,k,"duplicate"])
    count += 1
uniq.extend(dup)
print uniq

[[0, 10, 20, 0], [1, 40, 20, 30], [2, 20, 0, 30], [3, 10, 10, 0], [4, 30, 10, 10], [6, 20, 10, 20], [5, 20, 0, 30, 'duplicate'], [7, 10, 10, 0, 'duplicate']]

for j in (uniq):
    j.append(ic[j[0]])
print uniq


 [[0, 10, 20, 0, 1], [1, 40, 20, 30, 2], [2, 20, 0, 30, 3], [3, 10, 10, 0, 2], [4, 30, 10, 10, 1], [6, 20, 10, 20, 2], [5, 20, 0, 30, 'duplicate', 3], [7, 10, 10, 0, 'duplicate', 3]]

答案 1 :(得分:1)

如果您不介意其他解决方案......

f=[tuple(x) for x in f]
set(f)

答案 2 :(得分:1)

通过检查count[(i[0], i[1], i[2])] == 1],您已经删除了多次发生的所有项目。您只需返回计数器的键即可获得重复数据删除版本

def selectUnique(x):
    count = Counter((i[0], i[1], i[2]) for i in x)
    return count.keys()

答案 3 :(得分:1)

编辑:此答案符合原始规范

这是另一种方式:

def selectunique(lst):
    a = set()
    return [x for x in lst if tuple(x) not in a and not a.add(tuple(x))]

有关

for point in selectunique(f):
    print(point)

输出

[10, 20, 0]
[40, 20, 30]
[20, 0, 30]
[10, 10, 0]
[30, 10, 10]
[20, 10, 20]