python - 比较两个文件中的 x,y,z 坐标
问题描述
我在编程方面超级新鲜,所以我可能会问一些非常基本的问题。我有一个包含 x、y、z 坐标和第四个值的文件,以及一个包含 x、y、z 值的第二个文件。第二个文件的坐标随机包含在第一个文件中。我想做的是在第一个文件中搜索第二个文件的确切坐标,如果它们相同,则修改第一个文件的第四个值。
我写了一些有用的东西,但是非常耗时(需要三个小时..)。第一个文件大约 300K 行和 4 列,而第二个文件大约 100K 和 3 列。
在我写的代码下面:
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
ordict
来存储文件中的坐标。这样,您可以进行 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
只是第二个文件中的一组坐标。不过,您也可以不使用它并在迭代第二个文件时直接写入结果文件。
推荐阅读
- php - foreach循环PHP中值的递增计数
- python - 将登录用户与模型连接
- reactjs - 为什么我的组件会重新渲染这么多次?
- firebase-realtime-database - 如何使用 push() 在 Firebase 中存储多个项目
- linux - Linux:增加当前服务器的容量与添加新的更多服务器
- python - Python for 下划线的循环目的
- c# - 如何在 protobuf / C# 中定义和(反)序列化 IPAddress?
- java - post方法应该发送到哪个url地址(文件或目录)?
- leaflet - 边界无效
- python - 我将如何使输入重复?