# performance - python - 在嵌套列表中，重复列表的索引

`y = [[1,2,3],[1,2,3],[3,4,5],[6,5,4],[4,2,5],[4,2,5],[1,2,8],[1,2,3]]`

`[[0,1,7],[2],[3],[4,5],[6]]`

``````
from collections import OrderedDict

y = [[1,2,3],[1,2,3],[3,4,5],[6,5,4],[4,2,5],[4,2,5],[1,2,8],[1,2,3]]

lookup = OrderedDict()

for idx,l in enumerate(y):

lookup.setdefault(tuple(l), []).append(idx)

list(lookup.values())

# [[0, 1, 7], [2], [3], [4, 5], [6]]

``````

``````
result = []

for num in range(len(y)):

occurances = [i for i, x in enumerate(y) if x == y[num]]

if occurances not in result: result.append(occurances)

result

#[[0, 1, 7], [2], [3], [4, 5], [6]]

``````

``````
import numpy as np

y = [

[1, 2, 3],

[1, 2, 3],

[3, 4, 5],

[6, 5, 4],

[4, 2, 5],

[4, 2, 5],

[1, 2, 8],

[1, 2, 3]

]

# Returns unique values of array, indices of that

# array, and the indices that would rebuild the original array

unique, indices, inverse = np.unique(y, axis=0, return_index=True, return_inverse=True)

``````

``````
unique = [

[1 2 3]

[1 2 8]

[3 4 5]

[4 2 5]

[6 5 4]]

indices = [0 6 2 4 3]

inverse = [0 0 2 4 3 3 1 0]

``````

``````
new_list = []

for i in np.argsort(indices):

new_list.append(np.where(inverse == i)[0].tolist())

``````

``````
new_list = [[0, 1, 7], [2], [3], [4, 5], [6]]

``````

``````
y = [[1, 2, 3], [1, 2, 3], [3, 4, 5], [6, 5, 4], [4, 2, 5], [4, 2, 5], [1, 2, 8], [1, 2, 3]]

occurrences = {}

for i, v in enumerate(y):

v = tuple(v)

if v not in occurrences:

occurrences.update({v: []})

occurrences[v].append(i)

print(occurrences.values())

``````