如何创建具有相同元素的索引列表?

时间:2019-06-10 21:23:14

标签: python-3.x

我有一个包含许多向量(VC)的列表,并且我想获取一个等元素索引组的列表(VC_gr),我认为最简单的部分是获取一个值不相同的列表(VC_val)。

我已经看到collections.counter可以做一些类似的事情,但是只包含一个元素。

VC = [[54, 57, 58], [57, 58, 61], [54, 57, 58], [72, 69, 75], [66, 68, 68], [61, 64, 66], [66, 68, 68], [66, 68, 68], [66, 68, 68], [72, 69, 75], [54, 57, 58], [72, 69, 75]]

VC_gr = [[0, 2, 10], [1], [3, 9, 11], [4, 6, 7, 8], [5]]

VC_val = [[54, 57, 58], [57, 58, 61], [72, 69, 75], [66, 68, 68], [61, 64, 66]]

我希望有人可以提供一些线索来解决此任务。预先感谢

1 个答案:

答案 0 :(得分:1)

VC = [[54, 57, 58], [57, 58, 61], [54, 57, 58], [72, 69, 75], [66, 68, 68], [61, 64, 66], [66, 68, 68], [66, 68, 68], [66, 68, 68], [72, 69, 75], [54, 57, 58], [72, 69, 75]]

VC_gr = []
VC_val = []

seen = []
for v in VC:
    if v not in VC_val:
        VC_val.append(v)

    rv = []
    for i2, j in enumerate(VC):
        if v == j and i2 not in seen:
            rv.append(i2)
            seen.append(i2)
    if rv:
        VC_gr.append(rv)

print(VC_gr)
print(VC_val)

打印:

[[0, 2, 10], [1], [3, 9, 11], [4, 6, 7, 8], [5]]
[[54, 57, 58], [57, 58, 61], [72, 69, 75], [66, 68, 68], [61, 64, 66]]