擅长:python、mysql、java
<p>如果文件按第一行中的数字排序,则可以使用<a href="https://docs.python.org/3/library/itertools.html#itertools.groupby" rel="nofollow">^{<cd1>}</a>:</p>
<pre><code>from itertools import groupby
import operator
with open(sys.argv[1]) as infile:
# split lines and group them by the number in the first column
groups= groupby([line.strip().split() for line in infile], operator.itemgetter(0))
# convert groups to lists and discard keys
groups= [list(lines) for _, lines in groups]
# discard groups that don't have 2 items and format the output
groups= ['%s\t%s'%(lines[0][4],lines[1][4]) for lines in groups if len(lines)==2]
# alternatively you can use
# groups= ['\t'.join(zip(*lines)[4]) for lines in groups if len(lines)==2]
print '\n'.join(groups)
</code></pre>