擅长:python、mysql、java
<pre><code>from collections import defaultdict
import operator
namesscore = ["Rory: 1", "Rory: 4", "Liam: 5", "Liam: 6", "Erin: 8", "Liam: 2",]
# Build a dictionary where the key is the name and the value is a list of scores
scores = defaultdict(list)
for ns in namesscore:
name, score = ns.split(':')
scores[name].append(score)
# Sort each persons scores
scores = {n: sorted(s) for n, s in scores.items()}
# Sort people by their scores returning a list of tuples
scores = sorted(scores.items(), key=operator.itemgetter(1))
# Output the final strings
scores = ['{}: {}'.format(n, ', '.join(s)) for n, s in scores]
print scores
> ['Rory: 1, 4', 'Liam: 2, 5, 6', 'Erin: 8']
</code></pre>