<p>您可以使用<code>itertools.groupby</code>和<code>collections.Counter</code>:</p>
<pre><code>import itertools
from collections import Counter
s = [['pm', 15], ['pm', 15], ['pm', 15], ['pm', 15], ['gvt', 1], ['tools', 2], ['drm', 14], ['vgem', 12], ['template', 2], ['gem', 101], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['kms', 7], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['meta', 8], ['drv', 24], ['gen3', 5], ['sw', 18], ['syncobj', 81], ['gen7', 1], ['testdisplay', 1], ['debugfs', 3], ['perf', 27], ['core', 17], ['prime', 134]]
new_s = [(a, list(b)) for a, b in itertools.groupby(sorted(s, key=lambda x:x[0]), key=lambda x:x[0])]
for a, b in new_s:
print("{} found {} times, with {}".format(a, len(b), ' '.join('{} values of {}'.format(d, c) for c, d in Counter([i[-1] for i in b]).items())))
</code></pre>
<p>输出:</p>
<pre><code>core found 1 times, with 1 values of 17
debugfs found 1 times, with 1 values of 3
drm found 1 times, with 1 values of 14
drv found 1 times, with 1 values of 24
gem found 16 times, with 1 values of 101 15 values of 150
gen3 found 1 times, with 1 values of 5
gen7 found 1 times, with 1 values of 1
gvt found 1 times, with 1 values of 1
kms found 10 times, with 9 values of 150 1 values of 7
meta found 1 times, with 1 values of 8
perf found 1 times, with 1 values of 27
pm found 4 times, with 4 values of 15
prime found 1 times, with 1 values of 134
sw found 1 times, with 1 values of 18
syncobj found 1 times, with 1 values of 81
template found 1 times, with 1 values of 2
testdisplay found 1 times, with 1 values of 1
tools found 1 times, with 1 values of 2
vgem found 1 times, with 1 values of 12
</code></pre>
<p>输出语法有点泛化,但我相信你能把它修好。你知道吗</p>