gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:54:29 25 4
gpt4 key购买 nike

下面是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

我的代码是想实现把重复的点挑出来,并标明它在列表中的索引的功能。此外,我还需要实现显示我的唯一部分有多少等值点以及这些等值点的索引的功能。

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

最佳答案

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

关于Python:在进行重复计数时删除重复的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24171384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com