我有两个具有千行的表,一个有6列,另一个有4列。两个表都以第一列排序。它们看起来像:
296.526 10 0.1213 -0.907 0.005 -1.337
296.690 10 0.0000 +0.026 0.005 -0.404
296.936 11 0.1102 -1.885 0.005 -2.315
297.010 10 0.1101 -0.717 0.020 -1.147
297.012 11 0.0873 —1.436 0.020 -1.866
297.313 10 0.0873 -0.471 0.005 -0.901
...
和
296.936 0.110 -2.252 0.037
297.012 0.087 -1.964 0.041
297.613 2.279 -0.968 0.041
298.144 0.052 -1.342 0.037
298.645 0.110 -3.088 0.045
299.442 0.052 -0.564 0.045
302.049 0.087 -0.846 0.045
...
我想找到其第一列的差在0.001
之内并且表1的第三列和表2的第二列之差也在0.001
之内的行,然后生成通过添加表1的第6和第5列创建一个新表。例如,我想从上面的表部分中获得新表的两行:
296.936 0.110 -2.252 0.037 -2.315 0.005
297.012 0.087 -1.964 0.041 -1.866 0.020
我刚刚开始学习python。如何在python中有效地做到这一点?
答案 0 :(得分:0)
如果这是您要寻找的:
table1 =[[296.936, 11, 0.1102, -1.885, 0.005, -2.315 ],[297.010, 10, 0.1101, -0.717, 0.020, -1.147],[297.012, 11, 0.0873, -1.436, 0.020, -1.866]]
table2 =[[296.936, 0.110, -2.252, 0.037 ], [ 297.012, 0.087, -1.964, 0.041 ]]
table3 = []
for i in range(len(table2)):
for j in range(len(table1)):
if float('{:.3f}'.format(abs(table2[i][0]-table1[j][0])))<=0.001 and float('{:.3f}'.format(abs(table2[i][1]-table1[j][2])))<=0.001:
newtable = table2[i]
newtable.append(table1[j][4])
newtable.append(table1[j][5])
table3.append(newtable)
print(table3)