擅长:python、mysql、java
<p>使用OrderedDict存储最后三个字符串作为值,第一个字符串作为键,然后在末尾写入键和值。你知道吗</p>
<pre><code>from collections import OrderedDict
od = OrderedDict()
with open("words.txt") as f,open("fixed.txt","w") as out:
for line in f:
if not line.startswith("*"):
spl = line.split(None,1)
od.setdefault(spl[0],[])
od[spl[0]].append(spl[1:])
for k, v in od.items():
out.write("{}\n{}\n".format(k,"".join(" ".join(row) for row in v)))
</code></pre>