擅长:python、mysql、java
<p>打开文件,然后<code>str.rpartition</code>每行将数字与名称分开。然后构建一个生成器,用每个数字展开name,排序,然后执行输出所需的任何操作,例如:</p>
<p><strong>代码:</strong></p>
<pre><code>with open('input_file') as fin:
name_nums = (line.rpartition(':')[::2] for line in fin)
expanded = ((name, int(n)) for name, num in name_nums for n in num.split(','))
ordered = sorted(expanded, key=lambda L: L[1], reverse=True)
for name, num in ordered:
print '{}:{}'.format(name, num)
</code></pre>
<p><strong>输出</strong>:</p>
<pre><code>Raj,Joy:9
Campbell,Michelle:9
Raj,Joy:8
Smith,John:8
Campbell,Michelle:7
Campbell,Michelle:5
Raj,Joy:1
</code></pre>