问题
I have a combination of four coordinates, and I am trying to find the distance between each coordinates. I have been able to complete this, but I cannot get my program to list the coordinate pair that corresponds to each distance.
import itertools
import math
point1 = (1,1,0.5)
point2 = (3,3,1)
point3 = (2,0.5,2)
point4 = (0.5,2,1)
points = [(point1),(point2),(point3),(point4)]
pointsPairs = itertools.combinations(points, 2)
for pair in pointsPairs:
x1 = pair[0][0]
y1 = pair[0][1]
z1 = pair[0][2]
x2 = pair[1][0]
y2 = pair[1][1]
z2 = pair[1][2]
"""Define the values for the distance between the atoms"""
def calculate_distance(x1,y1,x2,y2,z1,z2):
dist=math.sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2-z1)**2)
return dist
d=calculate_distance(x1,y1,x2,y2,z1,z2)
print d
and I get
2.87228132327
1.87082869339
1.22474487139
2.87228132327
2.69258240357
2.34520787991
I would like to find a way to make something like
"Distance between point1 and point2 is"
2.87228132327
for each combination
回答1:
Before your loop, you could create a dictionary with coordinates as key and the name (using indexes starting at 1) as value:
points_dict = {k:"point_{}".format(i) for i,k in enumerate(points,1)}
and in your loop, just fetch the name from the coordinates yielded by combinations:
print("computing distance between {} and {}".format(points_dict[pair[0]],points_dict[pair[1]]))
by doing this I get
computing distance between point_1 and point_2
2.8722813232690143
computing distance between point_1 and point_3
1.8708286933869707
computing distance between point_1 and point_4
1.224744871391589
computing distance between point_2 and point_3
2.8722813232690143
computing distance between point_2 and point_4
2.692582403567252
computing distance between point_3 and point_4
2.345207879911715
回答2:
Essentially, the name of your points is not carried in the data structures you are using. I modified your code slightly to include the names of the points:
import itertools
import math
"""Define the values for the distance between the atoms"""
def calculate_distance(x1,y1,x2,y2,z1,z2):
dist=math.sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2-z1)**2)
return dist
point1 = (1,1,0.5,'point1')
point2 = (3,3,1,'point2')
point3 = (2,0.5,2,'point3')
point4 = (0.5,2,1,'point4')
points = [(point1),(point2),(point3),(point4)]
pointsPairs = itertools.combinations(points, 2)
for pair in pointsPairs:
x1 = pair[0][0]
y1 = pair[0][1]
z1 = pair[0][2]
x2 = pair[1][0]
y2 = pair[1][1]
z2 = pair[1][2]
d=calculate_distance(x1,y1,x2,y2,z1,z2)
p1=pair[0][3]
p2=pair[1][3]
print "The distance between '%s' and '%s' is" % ( p1, p2 )
print d
This gives the following results:
The distance between 'point1' and 'point2' is
2.87228132327
The distance between 'point1' and 'point3' is
1.87082869339
The distance between 'point1' and 'point4' is
1.22474487139
The distance between 'point2' and 'point3' is
2.87228132327
The distance between 'point2' and 'point4' is
2.69258240357
The distance between 'point3' and 'point4' is
2.34520787991
来源:https://stackoverflow.com/questions/42985994/python-list-the-combination-pair-for-a-function-value