python提高了3Gb文件中查找行和删除行的时间

python提高了3Gb文件中查找行和删除行的时间,python,dictionary,large-files,Python,Dictionary,Large Files,第一个职位, 你想怎么批评就怎么批评 我的问题是: 我有一个1.4亿行的大文件(文件1)和一个300万行的小文件(文件2)。我想删除文件1中与文件2匹配的行。直觉上,这似乎是一个简单的查找和删除问题,不应该花费那么长时间。就我的代码而言,在24Gb处理器上运行大约需要4天时间。我想在几个文件上执行此操作,因此我希望及时改进。 任何帮助和评论都将不胜感激 示例文件1: reftig_0 43 0 1.0 reftig_0 44 1 1.0 reftig_0 45 0 1.0 reftig_0 46

第一个职位, 你想怎么批评就怎么批评

我的问题是: 我有一个1.4亿行的大文件(文件1)和一个300万行的小文件(文件2)。我想删除文件1中与文件2匹配的行。直觉上,这似乎是一个简单的查找和删除问题,不应该花费那么长时间。就我的代码而言,在24Gb处理器上运行大约需要4天时间。我想在几个文件上执行此操作,因此我希望及时改进。 任何帮助和评论都将不胜感激

示例文件1:

reftig_0 43 0 1.0
reftig_0 44 1 1.0
reftig_0 45 0 1.0
reftig_0 46 1 1.0
reftig_0 47 0 5.0
reftig_0 43
reftig_0 44
reftig_0 45
data = open('file_1', 'r')
data_2 = open('file_2', 'r')
new_file = open('new_file_1', 'w')

d2= {}
for line in data_2:
    line= line.rstrip()
    fields = line.split(' ')
    key = (fields[0], fields[1])
    d2[key]=1

#print d2.keys()
#print d2['reftig_1']
tocheck=d2.keys()
tocheck.sort()
#print tocheck

for sline in data:
    sline = sline.rstrip()
    fields = sline.split(' ')
    nkey = (fields[0],fields[1])
    #print nkey
    if nkey in tocheck:
        pass
    else:
        new_file.write(sline + '\n')
        #print sline
示例文件2:

reftig_0 43 0 1.0
reftig_0 44 1 1.0
reftig_0 45 0 1.0
reftig_0 46 1 1.0
reftig_0 47 0 5.0
reftig_0 43
reftig_0 44
reftig_0 45
data = open('file_1', 'r')
data_2 = open('file_2', 'r')
new_file = open('new_file_1', 'w')

d2= {}
for line in data_2:
    line= line.rstrip()
    fields = line.split(' ')
    key = (fields[0], fields[1])
    d2[key]=1

#print d2.keys()
#print d2['reftig_1']
tocheck=d2.keys()
tocheck.sort()
#print tocheck

for sline in data:
    sline = sline.rstrip()
    fields = sline.split(' ')
    nkey = (fields[0],fields[1])
    #print nkey
    if nkey in tocheck:
        pass
    else:
        new_file.write(sline + '\n')
        #print sline
代码:

reftig_0 43 0 1.0
reftig_0 44 1 1.0
reftig_0 45 0 1.0
reftig_0 46 1 1.0
reftig_0 47 0 5.0
reftig_0 43
reftig_0 44
reftig_0 45
data = open('file_1', 'r')
data_2 = open('file_2', 'r')
new_file = open('new_file_1', 'w')

d2= {}
for line in data_2:
    line= line.rstrip()
    fields = line.split(' ')
    key = (fields[0], fields[1])
    d2[key]=1

#print d2.keys()
#print d2['reftig_1']
tocheck=d2.keys()
tocheck.sort()
#print tocheck

for sline in data:
    sline = sline.rstrip()
    fields = sline.split(' ')
    nkey = (fields[0],fields[1])
    #print nkey
    if nkey in tocheck:
        pass
    else:
        new_file.write(sline + '\n')
        #print sline

使用
grep
可以更好地实现这一点:

grep -Fvf file2 file1

将短字符串每行写入一次
new_文件
会很慢。通过将内容附加到列表中,并仅当列表(例如)有1000行长时才写入
new\u文件,从而减少写入次数

N = 1000
with open('/tmp/out', 'w') as f:
    result = []
    for x in range(10**7):
        result.append('Hi\n')
        if len(result) >= N:
            f.write(''.join(result))
            result = []
以下是对
N
的各种值运行
time test.py
的结果:

|      N | time (sec) |
|      1 |      5.879 |
|     10 |      2.781 |
|    100 |      2.417 |
|   1000 |      2.325 |
|  10000 |      2.299 |
| 100000 |      2.309 |

您的脚本速度很慢,因为tocheck
中的行
if-nkey正在对照
列表检查
nkey
。这是非常非常慢的,因为它是一个线性搜索(即使
tocheck
已排序)

使用
集合

def getkey(line):
    line = line.rstrip()
    fields = line.split(' ')
    return (fields[0], fields[1])

tocheck = {getkey(line) for line in data_2}

for line in data:
    if getkey(line) not in tocheck:
        new_file.write(line)

再加上unutbu的写批处理,你的脚本应该运行得很快。

你有24Gb的处理器吗?我在哪里可以买到如果文件中的数据是按顺序排列的(看起来是这样):为什么你不把两个文件并排读,只写与文件2中下一行不匹配的所有行呢?对不起。而且文件实际上没有排序,因此最终会失去同步。这超出了预期的行数…我正在尝试,没有-FThanks,各位,我现在只剩下一个小时了!