使用Python优化基于两个文本文件的搜索和输出

希思

我在使用python函数时遇到性能问题,因为我正在使用加载两个5+ GB选项卡描述的txt文件,这些txt文件具有相同的格式和不同的值,并使用第三个文本文件作为键来确定应保留哪些值以进行输出。如果可能,我希望获得一些帮助以提高速度。

这是代码:

def rchfile():
# there are 24752 text lines per stress period, 520 columns, 476 rows
# there are 52 lines per MODFLOW model row
lst = []
out = []
tcel = 0
end_loop_break = False

# key file that will set which file values to use. If cell address is not present or value of cellid = 1 use
# baseline.csv, otherwise use test_p97 file.
with open('input/nrd_cells.csv') as csvfile:
    reader = csv.reader(csvfile)
    for item in reader:
        lst.append([int(item[0]), int(item[1])])

# two files that are used for data
with open('input/test_baseline.rch', 'r') as b, open('input/test_p97.rch', 'r') as c:
    for x in range(3):  # skip the first 3 lines that are the file header
        b.readline()
        c.readline()

    while True:  # loop until end of file, this should loop here 1,025 times
        if end_loop_break == True: break
        for x in range(2):  # skip the first 2 lines that are the stress period header
            b.readline()
            c.readline()

        for rw in range(1, 477):
            if end_loop_break == True: break

            for cl in range(52):
                # read both files at the same time to get the different data and split the 10 values in the row
                b_row = b.readline().split()
                c_row = c.readline().split()

                if not b_row:
                    end_loop_break == True
                    break

                for x in range(1, 11):
                    # search for the cell address in the key file to find which files datat to keep
                    testval = [i for i, xi in enumerate(lst) if xi[0] == cl * 10 + x + tcel]

                    if not testval:  # cell address not in key file
                        out.append(b_row[x - 1])
                    elif lst[testval[0]][1] == 1:  # cell address value == 1
                        out.append(b_row[x - 1])
                    elif lst[testval[0]][1] == 2:  # cell address value == 2
                        out.append(c_row[x - 1])

                    print(cl * 10 + x + tcel)  # test output for cell location

            tcel += 520

print('success')`

密钥文件如下所示:

37794, 1
37795, 0
37796, 2

数据文件每个大约5GB,从计数的角度来看很复杂,但是格式是标准的,看起来像:

0    0    0    0    0    0    0    0    0    0
1.5  1.5  0    0    0    0    0    0    0    0

这个过程耗时很长,希望有人可以帮助加快速度。

高呼

我相信您的速度问题来自此行:

testval = [i for i, xi in enumerate(lst) if xi[0] == cl * 10 + x + tcel]

您正在遍历HUGE输出文件中每个单个值的整个键列表。情况不妙。

看来cl * 10 + x + tcel是您要寻找的公式lst[n][0]

我可以建议您使用dict而不是list来将数据存储在中lst

lst = {}
for item in reader:
   lst[int(item[0])] = int(item[1])

现在,lst是一个映射,这意味着您可以简单地使用in运算符来检查密钥的存在。这是一个近乎即时的查找,因为该dict类型基于哈希,并且对于键查找非常有效。

something in lst
# for example
(cl * 10 + x) in lst

您可以通过以下方式获取价值:

lst[something] 
#or
lst[cl * 10 + x]

进行一点重构,您的代码应该可以快速地进行加速。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用python和regex合并两个文本文件

如何使用python3.6.4提取不同行的单词文本并在两个文本文件中输出

Python。比较两个文本文件的内容

在Python中串联两个文本文件

使用python多次打印文本文件中两个单词之间的文本

使用python从文本文件中删除两个重复项(原始和重复项)

如何使用HTML5和Javascript同时读取两个文本文件

python 比较两个文本文件,然后将输出打印到另一个文件

如何使用python“联接”两个文本文件?

如何使用Python逐行合并两个文本文件?

尝试使用python比较两个文本文件

Python 使用文本文件作为输入和输出

在bash中加入两个文本文件以将输出匹配为两列

如何从python中两个单独的文本文件中基于子字符串提取行?

如何搜索文本文件C#中存在的两个或多个单词

输出两个文本文件的公共行(相似性)(与diff相反)?

如何输出两个文本文件之间的差异?

如何在单独的行上将两个回声输出到文本文件

将两个文本文件中的两个矩阵相乘得出输出为零

使用python通过两个标识符从一个巨大的文本文件中提取行作为开始和结束

如何将两个文本文件与文件路径进行比较,并在另一个文本文件中输出差异?

从文本文件中获取使用两个变量作为开始和结束参数的范围

如何比较两个文本文件的内容并在另一个文本文件中输出?文字1-文字2

在 Golang 中合并两个文本文件

同时逐行读取两个文本文件

同时逐行读取两个文本文件

Linux结合了两个不同的文本文件

内部联接两个文本文件

对两个文本文件求和