【问题标题】:Optimal way to iterate through 3 files and generate a third file in python迭代3个文件并在python中生成第三个文件的最佳方法
【发布时间】:2016-10-19 08:23:09
【问题描述】:

我有三个带有列表列表的 txt 文件。

文件 1(9700 行):

ID1, data 1

文件 2(210 万行):

ID1, ID2

文件 3(1100 行):

ID2, data 3

我想制作一个文件 4

  • 获取文件 1 中的所有行(ID1 和数据 1)
  • 获取该行 ID1 的 ID2。
  • 获取该 ID2 的数据 3。
  • 在文件 4 中为文件 1 中的所有行保存一个 ID1、数据 1、ID2、数据 3 的文件

我已经用python为它做了一个脚本,但是ATM需要1个小时。

这是它的作用:

file1 = []
file4 = []
file3 = []

final_list.append("ID1, ID2, DATA1, DATA2")

#Import file1
with open('file1.txt') as inputfile: #file 1: around 9.7k
    for line in inputfile:
        temp = line.strip().split(' ')
        file1.append(temp)

#Import file3
with open('file3.txt') as inputfile: #file 3: around 1.1k
    for line in inputfile:
        temp = line.strip().split(' ')
        file3.append(temp)

print len(file1)

#Iterate through file2 (so I only iterate once through this)
with open('file2.txt') as inputfile: #File 2: 2.1 million
    for line in inputfile:
        temp = line.strip().split(' ')
        for sublist in file1: #Only if first element is also in list 1
            if sublist[0] == temp[0]:
                for sublist2 in file3:
                    if sublist2[0] == temp[1]:
                        file4.append([temp, sublist[1], sublist2[1]])

print len(file4)

print file4[:10]

thefile = open('final.txt', 'w')
for item in file4:
  thefile.write("%s\n" % item)
thefile.close()

如前所述,提款机需要一个小时。我怎样才能提高性能?我有很多循环,正在考虑是否可以通过某种方式更快地完成......

注意:ID只出现一次,数据可以是重复值

【问题讨论】:

  • 您可以通过将文件 1 和文件 3 中的数据加载到字典中(以 ID 为键)来大大加快速度。

标签: python performance file optimization


【解决方案1】:

由于您的 ID 是唯一的,因此在您编写时,您可以使用字典而不是 file1 和 file3 的列表。因此,您的循环检查以查看是否存在 ID 减少到在这些字典的键集中进行一次查找。我不知道您的原始列表,但我认为字典对于您的目的来说更快。因此,您可以在长文件中保存两次循环迭代。不过,将花费一些时间来组装密钥列表。请尝试以下方法:

file1 = {}                              # empty new dictionary
file4 = []
file3 = {}

final_list.append("ID1, ID2, DATA1, DATA2")

#Import file1
with open('file1.txt') as inputfile:    #file 1: around 9.7k
    for line in inputfile:
        temp = line.strip().split(' ')
        file1[temp[0]] = temp[1]        # store ID1 and associated data in dict

#Import file3
with open('file3.txt') as inputfile:    #file 3: around 1.1k
    for line in inputfile:
        temp = line.strip().split(' ')
        file3[temp[0]] = temp[1]        # store ID2 and associated data in dict

print len(file1)

#Iterate through file2 (so I only iterate once through this)
keys1 = file1.keys()                    # for fast lookup, precalculate the list of ID1 entries
keys3 = file3.keys()                    # for fast lookup, precalculate the list of ID2 entries
with open('file2.txt') as inputfile:    #File 2: 2.1 million
    for line in inputfile:
        temp = line.strip().split(' ')
        if temp[0] in keys1:
            if temp[1] in keys3:
                file4.append([temp, file1[temp[0]], file3[temp[0]]])

print len(file4)

print file4[:10]

thefile = open('final.txt', 'w')
for item in file4:
  thefile.write("%s\n" % item)
thefile.close()

问候,

【讨论】:

  • 出于好奇。程序现在运行需要多长时间?
  • 过去需要 1 小时,现在需要 10 分钟
  • 谢谢。我阅读了 Python 字典。看来您甚至可以省略“keys1 =...”和“keys3 = ...”这行,然后再写“if temp[0] in file1:”和“if temp[0] in file3:”以加快速度更多的事情。不过不确定多少。问候,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2011-07-08
  • 2018-08-23
相关资源
最近更新 更多