【发布时间】:2012-11-16 19:55:41
【问题描述】:
我有两个文本文件:
样本-r1.txt
Bud Abbott 51 92.3
Mary Boyd 52 91.4
Hillary Clinton 50 82.1
样本-r2.txt
Don Adams 51 90.4
Jill Carney 53 76.3
Randy Newman 50 41.2
我想用姓氏对它们进行合并和排序,姓氏是每行的第二个索引(程序可能不使用任何预先存在的合并或排序软件)
这是我的代码
one = open("sample-r1.txt",'r')
two = open("sample-r2.txt",'r')
for line in one:
k = line.rstrip().split('\t')
for record in two:
h= record.rstrip().split('\t')
i=0
j=0
newList=[]
while i < len(k) and j<len(h) :
if k[i][1] <= h[j][1]:
newList.append(k[i])
i+=1
else:
newList.append(h[j])
j+=1
print(newList)
【问题讨论】:
-
请修正你的缩进。
标签: python sorting text merge mergesort