【发布时间】:2014-04-18 16:01:37
【问题描述】:
我在连接两个具有 5 个公共列的大文件并返回结果时遇到问题,它们是相同的 5 个元组... 这就是我的意思:
文件 1:
132.227 49202 107.21 80
132.227 49202 107.21 80
132.227 49200 107.220 80
132.227 49200 107.220 80
132.227 49222 207.171 80
132.227 49339 184.730 80
132.227 49291 930.184 80
............
............
............
该文件包含很多行,而不仅仅是那些...
文件 2:
46.109498000 132.227 49200 107.220 80 17 48
46.927339000 132.227 49291 930.184 80 17 48
47.422919000 253.123 1985 224.300 1985 17 48
48.412761000 132.253 1985 224.078 1985 17 48
48.638454000 132.127 1985 232.123 1985 17 48
48.909658000 132.227 49291 930.184 80 17 65
48.911360000 132.227 49200 107.220 80 17 231
............
............
............
输出文件:
46.109498000 132.227 49200 107.220 80 17 48
46.927339000 132.227 49291 930.184 80 17 48
48.909658000 132.227 49291 930.184 80 17 65
48.911360000 132.227 49200 107.220 80 17 231
............
............
............
这是我写的代码:
with open('log1', 'r') as fl1:
f1 = [i.split(' ') for i in fl1.read().split('\n')]
with open('log2', 'r') as fl2:
f2 = [i.split(' ') for i in fl2.read().split('\n')]
def merging(x,y):
list=[]
for i in x:
for j in range(len(i)-1):
while i[j]==[a[b] for a in y]:
list.append(i)
j=j+1
return list
f3=merging(f1,f2)
for i in f3:
print i
【问题讨论】:
-
@mskimm ,是的,第二个文件按第一列排序(这是开始时间)
-
m 抱歉,我无法在此处添加另一个 comm,所以我将在这里写下它的 'python --version' 是 Python 2.7.6