python xyz_python中xyz坐标的欧几里德距离
使用生成器表達(dá)式的簡(jiǎn)單解決方案From PEP 289 Generator Expressions
Rationale
Experience with list comprehensions has shown their widespread utility
throughout Python. However, many of the use cases do not need to have a full list created in memory. Instead, they only need to iterate over the elements one at a time.
因?yàn)槟悴恍枰4嬷虚g結(jié)果
可能你有一個(gè)大的數(shù)據(jù)集
以及itertools標(biāo)準(zhǔn)庫(kù)模塊中的^{},因?yàn)槟枰?jì)算數(shù)據(jù)集中每對(duì)有趣的點(diǎn)的距離。在$ cat euclid.py
from scipy.spatial.distance import euclidean
from itertools import combinations
lines = ['HETATM 1 H10 XSHQ 0 10.139 2.231 0.091 1.00 0.00 H',
'HETATM 2 N1 XSHQ 0 9.641 1.386 -0.104 1.00 0.00 N',
'HETATM 3 H9 XSHQ 0 9.773 1.133 -1.063 1.00 0.00 H',
'HETATM 4 C1 XSHQ 0 8.245 1.531 0.230 1.00 0.00 H']
H_lines = (line for line in lines if line[-1]=='H')
H_lists = (line.split() for line in H_lines)
H_data = ((int(tok[1]), [float(x) for x in tok[5:8]]) for tok in H_lists)
H_dist = {(i[0], j[0]):euclidean(i[1], j[1])
for i, j in combinations(H_data, 2)}
for m, n in H_dist:
print('Distance between points %d and %d is %.6f'%(
m, n, H_dist[m, n]))
$ python3 euclid.py
Distance between points 1 and 3 is 1.634404
Distance between points 1 and 4 is 2.023995
Distance between points 3 and 4 is 2.040842
$
總結(jié)
以上是生活随笔為你收集整理的python xyz_python中xyz坐标的欧几里德距离的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: qt android glsl,基于Qt
- 下一篇: Python元类(type()和meta