【问题标题】:Distance calculation for coordinates called from a file(Python2.7)从文件调用的坐标的距离计算(Python2.7)
【发布时间】:2014-03-13 10:14:43
【问题描述】:

我正在尝试从文件中读取坐标并查找文件中每两个原子之间的距离。 我想在一列中有计算的距离,在另一列中有相应的原子名称。

1 Br1 0 Br x,y,z 1.195 7.005 10.004
2 Br2 0 Br x,y,z 1.432 5.040 6.816
3 Br3 0 Br x,y,z -0.407 8.433 6.863
4 Br4 0 Br x,y,z 3.344 8.375 7.107
5 Fe1 0 Fe x,y,z 1.412 7.214 7.682
6 Br5 0 Br x,y,z 2.813 12.506 12.949
7 Br6 0 Br x,y,z 4.778 14.123 10.091
8 Br7 0 Br x,y,z 6.563 12.765 13.175
9 Br8 0 Br x,y,z 4.387 15.965 13.344

例如:

Br1-Br2 1.5

我遇到两个问题:

1-我不知道如何命令程序计算每两对原子之间的欧式距离,我使用 split() 从其他数据中分割坐标,但我仍然不知道如何计算距离。我找到了一些关于 same problem 的信息,但答案对我不起作用,因为它们适用于 perl。

atom_positions= open('Atoms.txt','r')
revised_atom_positions = open('Atoms_atoms.txt','w')
aline = atom_positions.readline()#for getting rid of the first line data which is general info

for aline in atom_positions:
     Values = aline.split()
     dataline = values[1]+' , '+ values[5] + ' , ' + values[6] +' , '+values[7]
     revised_atom_positions.write(dataline + '\n')


atom_positions.close()
revised_atom_positions.close()

当我尝试将其更改为数组并使用 pdist 时出现错误:

TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

2- 我可以将我的新数据以新顺序存储在另一个文件中,但我不知道如何调用它们进行距离计算!有一些关于 pdist 的指导方针,但它对我也不起作用。

有没有办法解决这个问题?

【问题讨论】:

    标签: python arrays scipy type-conversion euclidean-distance


    【解决方案1】:

    你可以使用索引:

    atom_positions= open('Atoms.txt','r')
    revised_atom_positions = open('Atoms_atoms.txt','w')
    
    lines = atom_positions.readlines()
    
    for i in range(0, len(lines)):
        line = lines[i]
        values = line.split()
        dataline = values[1]+' , '+ values[5] + ' , ' + values[6] +' , '+values[7]
        # You can access to next line with lines[i + 1]
        revised_atom_positions.write(dataline + '\n')
    
    
    atom_positions.close()
    revised_atom_positions.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      相关资源
      最近更新 更多