<p>使用<a href="https://docs.python.org/3/library/itertools.html#itertools.groupby" rel="nofollow noreferrer">^{<cd1>}</a>。与<a href="https://docs.python.org/3/library/operator.html#operator.itemgetter" rel="nofollow noreferrer">^{<cd2>}</a>配对以实现高效的查找/切片</p>
<pre><code>from itertools import groupby
from operator import itemgetter
tuple_list = [(1, 'AA', 515), (1, 'BBT', 101), (1, 'CZF', 20), (2, 'TYZ', 8341), (2, 'ONR', 11)]
get_first = itemgetter(0)
result = [list(g) for k, g in groupby(sorted(tuple_list, key=get_first), get_first)]
</code></pre>
<hr/>
<p><strong>结果:</strong></p>
<pre><code>[[(1, 'AA', 515), (1, 'BBT', 101), (1, 'CZF', 20)], [(2, 'TYZ', 8341), (2, 'ONR', 11)]]
</code></pre>
<p>或使用<a href="https://docs.python.org/3/library/collections.html#collections.defaultdict" rel="nofollow noreferrer">^{<cd3>}</a></p>
<pre><code>from collections import defaultdict
d = defaultdict(list)
for t in tuple_list:
d[t[0]].append(t)
result = list(d.values())
</code></pre>
<hr/>
<p><strong>结果:</strong></p>
<pre><code>[[(1, 'AA', 515), (1, 'BBT', 101), (1, 'CZF', 20)], [(2, 'TYZ', 8341), (2, 'ONR', 11)]]
</code></pre>