首页 > 编程知识 正文

python如何求两点之间距离,python怎么计算两点之间的距离

时间:2023-05-05 08:31:03 阅读:221604 作者:4880

我需要创建一个列表,找到最接近的两个点,然后打印出来.如何比较列表中的每个点?

没有任何需要绘制或任何东西,只是比较点,找到列表中最接近的两个.

import math # 'math' needed for 'sqrt'

# Distance function

def distance(xi,xii,yi,yii):

sq1 = (xi-xii)*(xi-xii)

sq2 = (yi-yii)*(yi-yii)

return math.sqrt(sq1 + sq2)

# Run through input and reorder in [(x, y), (x,y) ...] format

oInput = ["9.5 7.5", "10.2 19.1", "9.7 10.2"] # Original input list (entered by spacing the two points).

mInput = [] # Manipulated list

fList = [] # Final list

for o in oInput:

mInput = o.split()

x,y = float(mInput[0]), float(mInput[1])

fList += [(x, y)] # outputs [(9.5, 7.5), (10.2, 19.1), (9.7, 10.2)]

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。