擅长:python、mysql、java
<p>您可以使用<a href="https://docs.python.org/3/library/collections.html#defaultdict-examples" rel="nofollow">^{<cd1>}</a>来收集每组元素的索引:</p>
<pre><code>In [1]: from collections import defaultdict
In [2]: data = [1, 7, 2, 1, 1, 1, 2, 2, 7, 1, 7, 7, 2]
In [3]: indices = defaultdict(list)
In [4]: for i, d in enumerate(data):
...: indices[d].append(i)
...:
In [5]: indices
Out[5]: defaultdict(<class 'list'>, {1: [0, 3, 4, 5, 9], 2: [2, 6, 7, 12], 7: [1, 8, 10, 11]})
</code></pre>