简体   繁体   English

Python:删除重复项并指示索引

[英]Python: Remove duplicates and indicate index

Below is the tuple for 3D point group (nine points) 以下是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],
    [20, 0, 30]]

Each point corresponds with a certain number (index) indicating the type of point (assumption) 每个点对应一个表示点类型(假设)的特定数字(索引)

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

Hence, the previous tuple could be presented as 因此,先前的元组可以表示为

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

Here is my code: 这是我的代码:

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)

for i,j in enumerate(uniq):
    j.append(ic[j[0]])
print(unique)

The result I want to obtain is shown below: 我想要获得的结果如下所示:

Unique part: 独特的部分:

index       point         equivalent points    index for same point
  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]

Duplicate part: 重复部分:

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

My code is intended to realize the function of picking the duplicated points out and also indicating its index in the list. 我的代码旨在实现挑选出重复点并在列表中指示其索引的功能。 In addition, I also need to realize function showing how many equivalent points in my unique part and also the index for these equivalent points. 另外,我还需要实现显示我的唯一部分中有多少个等效点以及这些等效点的索引的函数。

How can I revise it? 我该如何修改?

I'm not sure I follow where you're getting your index points, but here's how I'd count for duplicates. 我不确定我是否遵循您在哪里获得索引点,但是这就是我如何计算重复项的方法。 First you need immutable datatypes to count, so change your sublists to real tuples, and use collections.Counter to count them: 首先,您需要不可变的数据类型进行计数,因此将子列表更改为真正的元组,然后使用collections.Counter对它们进行计数:

import pprint # do your imports first
import collections


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], [20, 0, 30]]
t = [tuple(i) for i in f] # we need immutable datatypes to count

counts = collections.Counter(t)
pprint.pprint(counts)

prints 版画

{(10, 10, 0): 2,
 (10, 20, 0): 1,
 (20, 0, 30): 3,
 (20, 10, 20): 1,
 (30, 10, 10): 1,
 (40, 20, 30): 1}

And as you may intuit, Counter is just a subclassed dict , and has all the normal dict methods. 正如您可能会理解的那样, Counter只是dict的子类,并且具有所有常规dict方法。

To get your uniques and dupes: 要获取您的唯一身份和欺骗对象:

uniques = [k for k, v in counts.items() if v == 1]

which returns 哪个返回

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

and

dupes = [k for k, v in counts.items() if v > 1]

returns 退货

[(20, 0, 30), (10, 10, 0)]
for j in uniq+dup:
    if "duplicate" not in j:
        j += ic[j[0]],f.count(j[1:4]), [ic[j[0]]]
    else:
        j.append(ic[j[0]])    

for i in dup:
    for j in uniq:
        if i[1:4] == j[1:4]:
            j[-1].append(i[-1])

[[5, 20, 0, 30, 'duplicate', 3], [7, 10, 10, 0, 'duplicate', 3], [8, 20, 0, 30, 'duplicate', 1]]

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

This will add the count to each sublist without changing your original structure. 这会将计数添加到每个子列表,而不会更改您的原始结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM