我在编程方面非常新鲜,所以我可能会问一些非常基本的问题。我有一个由x,y,z坐标和第四个值组成的文件,以及另一个具有x,y,z值的文件。第二个文件的坐标随机包含在第一个文件中。我想做的是在第一个文件中搜索第二个文件的确切坐标,如果它们相同,则修改第一个文件的第四个值。
我已经写了一些行之有效的东西,但这非常耗时(需要三个小时..)。第一个文件约为30万行,分为4列,第二个文件约为100K,分为三列。
在我写的代码下面:
import numpy as np
with open('first file.txt', 'r') as t1:
l1=[]
for line in t1:
split = line.split()
l1.append((float(split[0]),float(split[1]),float(split[2]),float(split[3])))
l3=np.asarray(l1)
with open('second file.txt', 'r') as t2:
l2=[]
for line in t2:
split = line.split()
l2.append((float(split[0]),float(split[1]),float(split[2])))
with open('result file.txt', 'w') as outFile:
for i in l3:
for j in l2:
if i[0]==j[0] and i[1]==j[1] and i[2]==j[2]:
i[3]+=970000000
#outFile.write(i)
#print(i[3])
np.savetxt("result file.txt",l3,fmt='%7.4f'*3+'%10.3f')
如果您有任何加快该过程的提示,请告诉我!
最佳答案
您应该使用set
或dict
来存储文件中的坐标。这样,您可以执行O(1)查找,而不必比较两个文件中的每一对或坐标。因此,您只有300k + 100k
个迭代,而不是300k x 100k
。像这样的东西(未经测试):
coords_first = {}
with open('first file.txt', 'r') as t1:
for line in t1:
*pts, val = map(float, line.split())
coords[pts] = val
coords_second = set()
with open('second file.txt', 'r') as t2:
for line in t2:
pts = tuple(map(float, line.split()))
coords_second.add(pts)
with open('result file.txt', 'w') as outFile:
for pts in coords_first:
if pts in coords_second:
new_val = coords_first[pts] + 970000000
# write points and new value to file
在这里,
coords_first
正在将坐标从第一个文件映射到值,即{(x1,y1,z1): v1, (x2,y2,z2): v2, ...}
。 coords_second
只是第二个文件中的一组坐标。您也可以不使用它,而在迭代第二个文件时直接写入结果文件。