Python “重建”;任何();函数查找最近的坐标

Python “重建”;任何();函数查找最近的坐标,python,Python,我有一个问题,我被赋予了坐标X和Y的每个列表的数字列表,我必须找到哪个坐标最接近列表的第一个坐标,因为我有以下代码: import math list1 = [1, 2, 1, 1] list2 = [4, 7, 6, 3] if len(list2) == len(list1): print ("number of elements does match\n") else: print ("number of elements doesnt match\n") a=1 d

我有一个问题,我被赋予了坐标X和Y的每个列表的数字列表,我必须找到哪个坐标最接近列表的第一个坐标,因为我有以下代码:

import math

list1 = [1, 2, 1, 1]
list2 = [4, 7, 6, 3]

if len(list2) == len(list1):
    print ("number of elements does match\n")
else:
    print ("number of elements doesnt match\n")

a=1
dist_list = []

for i in range(1, len(list1)):
    x1 = list1[0]
    y1 = list2[0]

    x2 = list1[a]
    y2 = list2[a]

    dist = abs(math.sqrt((x1-(x2)**2) - (y1-(y2)**2)))
    a=a+1
    dist_list.append(dist)

print ("distancias:", dist_list, "\n")

for element in iterable:
        if element:
            return
    return False

print(list1[b],",",list2[b], "es la coordenada mas cercana")
#                            "is the closest coordinate"
在这种情况下,坐标之间的距离表示坐标1,3最接近1,4

距离:[6.48074069840786,5.656854249492381,2.23606797749979]

我需要在这里说:
print(列表1[b],“,”,列表2[b],“es la coordenada mas cercana”)

我一直在搜索并找到
any()
函数,根据Python文档,我可以重新创建公式的方法是:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

但是我看不出如何重新创建它来获得想要的结果。另外,我必须说,对于这个练习,我只能使用
for
in
while
,sqer,abs和
append
,创建一个代表最近距离的变量

import math

list1 = [1, 2, 1, 1]
list2 = [4, 7, 6, 3]

if len(list2) == len(list1):
    print ("number of elements does match\n")
else:
    print ("number of elements doesnt match\n")

dist_list = []
x1 = list1[0]
y1 = list2[0]
closest = 10000
closest_xy = 0
for i in range(1, len(list1)):
    x2 = list1[i]
    y2 = list2[i]
    dist = math.sqrt((x1-x2)**2 + (y1-y2)**2)
    if closest > dist:
        closest = dist
        closest_xy = i
    dist_list.append(dist)

print ("distancias:", dist_list, "\n")


print(list1[closest_xy],",",list2[closest_xy], "es la coordenada mas cercana")
#                            "is the closest coordinate"

创建一个表示最近距离的变量

import math

list1 = [1, 2, 1, 1]
list2 = [4, 7, 6, 3]

if len(list2) == len(list1):
    print ("number of elements does match\n")
else:
    print ("number of elements doesnt match\n")

dist_list = []
x1 = list1[0]
y1 = list2[0]
closest = 10000
closest_xy = 0
for i in range(1, len(list1)):
    x2 = list1[i]
    y2 = list2[i]
    dist = math.sqrt((x1-x2)**2 + (y1-y2)**2)
    if closest > dist:
        closest = dist
        closest_xy = i
    dist_list.append(dist)

print ("distancias:", dist_list, "\n")


print(list1[closest_xy],",",list2[closest_xy], "es la coordenada mas cercana")
#                            "is the closest coordinate"
any()
对此无效。您必须重新创建
min()
(或者更确切地说是
numpy.argmin()

您必须在开始时获得第一个距离,并与所有其他值进行比较,当其他值较小时,则保留该值及其索引

b = 0   # get first index
b_value = dist_list[0]   # get first value

# compare with other distances
for i in range(1, len(dist_list)):
    if dist_list[i] < b_value:  # if new distance is smaller
        b = i  # keep new index
        b_value = dist_list[i]   # keep new value

b = b+1  # because `dist_list[0]` has value for `list1[1]`, not `list1[0]`

完整代码

import math

list1 = [1, 2, 1, 1]
list2 = [4, 7, 6, 3]

if len(list2) == len(list1):
    print ("number of elements does match\n")
else:
    print ("number of elements doesnt match\n")

dist_list = []

x1 = list1[0]
y1 = list2[0]

for i in range(1, len(list1)):

    x2 = list1[i]
    y2 = list2[i]

    #dist = abs(math.sqrt((x1-(x2)**2) - (y1-(y2)**2)))
    dist = math.sqrt((x1-x2)**2 + (y1-y2)**2)

    dist_list.append(dist)

print ("distancias:", dist_list, "\n")

b = 0
b_value = dist_list[0]

for i in range(1, len(dist_list)):
    if dist_list[i] < b_value:
        b = i
        b_value = dist_list[i]

b = b+1  # because `dist_list[0]` has value for `list1[1]`, not `list1[0]`

print(list1[b],",",list2[b], "es la coordenada mas cercana")
#                            "is the closest coordinate"
导入数学
列表1=[1,2,1,1]
列表2=[4,7,6,3]
如果len(列表2)=len(列表1):
打印(“元素数不匹配\n”)
其他:
打印(“元素数不匹配\n”)
dist_list=[]
x1=列表1[0]
y1=列表2[0]
对于范围(1,len(列表1))中的i:
x2=列表1[i]
y2=列表2[i]
#dist=abs(数学sqrt((x1-(x2)**2)-(y1-(y2)**2)))
距离=数学sqrt((x1-x2)**2+(y1-y2)**2)
dist_list.append(dist)
打印(“距离:”,距离列表“\n”)
b=0
b_值=距离列表[0]
对于范围内的i(1,len(dist_列表)):
如果距离列表[i]
any()
对此没有用处。您必须重新创建
min()
(或者更确切地说是
numpy.argmin()

您必须在开始时获得第一个距离,并与所有其他值进行比较,当其他值较小时,则保留该值及其索引

b = 0   # get first index
b_value = dist_list[0]   # get first value

# compare with other distances
for i in range(1, len(dist_list)):
    if dist_list[i] < b_value:  # if new distance is smaller
        b = i  # keep new index
        b_value = dist_list[i]   # keep new value

b = b+1  # because `dist_list[0]` has value for `list1[1]`, not `list1[0]`

完整代码

import math

list1 = [1, 2, 1, 1]
list2 = [4, 7, 6, 3]

if len(list2) == len(list1):
    print ("number of elements does match\n")
else:
    print ("number of elements doesnt match\n")

dist_list = []

x1 = list1[0]
y1 = list2[0]

for i in range(1, len(list1)):

    x2 = list1[i]
    y2 = list2[i]

    #dist = abs(math.sqrt((x1-(x2)**2) - (y1-(y2)**2)))
    dist = math.sqrt((x1-x2)**2 + (y1-y2)**2)

    dist_list.append(dist)

print ("distancias:", dist_list, "\n")

b = 0
b_value = dist_list[0]

for i in range(1, len(dist_list)):
    if dist_list[i] < b_value:
        b = i
        b_value = dist_list[i]

b = b+1  # because `dist_list[0]` has value for `list1[1]`, not `list1[0]`

print(list1[b],",",list2[b], "es la coordenada mas cercana")
#                            "is the closest coordinate"
导入数学
列表1=[1,2,1,1]
列表2=[4,7,6,3]
如果len(列表2)=len(列表1):
打印(“元素数不匹配\n”)
其他:
打印(“元素数不匹配\n”)
dist_list=[]
x1=列表1[0]
y1=列表2[0]
对于范围(1,len(列表1))中的i:
x2=列表1[i]
y2=列表2[i]
#dist=abs(数学sqrt((x1-(x2)**2)-(y1-(y2)**2)))
距离=数学sqrt((x1-x2)**2+(y1-y2)**2)
dist_list.append(dist)
打印(“距离:”,距离列表“\n”)
b=0
b_值=距离列表[0]
对于范围内的i(1,len(dist_列表)):
如果距离列表[i]
数学模块有一个功能,可以让您相对轻松地完成所需的工作:

import math


def find_closest(list1, list2):
    if len(list2) != len(list1):
        raise RuntimeError("number of elements does match")
    if len(list1) < 2:
        raise RuntimeError("the lists must contain at least two points")

    x0, y0 = list1[0], list2[0]
    index = 1
    min_dist = math.hypot(x0-list1[1], y0-list2[1]) # Distance between first and second pt.
    # See if any of the remaining pts are closer.
    for i in range(2, len(list1)):
        dist = math.hypot(x0-list1[i], y0-list2[i])
        if dist < min_dist:
            min_dist = dist
            index = i

    return (list1[i], list2[i])  # Closest point.


list1 = [1, 2, 1, 1]
list2 = [4, 7, 6, 3]

print(find_closest(list1, list2), "es la coordenada mas cercana")
如果您不能使用
math.hypot()
,那么基于
math.sqrt()
,编写自己的代码就相当容易了:

数学模块有一个功能,可以使您所需的操作相对容易:

import math


def find_closest(list1, list2):
    if len(list2) != len(list1):
        raise RuntimeError("number of elements does match")
    if len(list1) < 2:
        raise RuntimeError("the lists must contain at least two points")

    x0, y0 = list1[0], list2[0]
    index = 1
    min_dist = math.hypot(x0-list1[1], y0-list2[1]) # Distance between first and second pt.
    # See if any of the remaining pts are closer.
    for i in range(2, len(list1)):
        dist = math.hypot(x0-list1[i], y0-list2[i])
        if dist < min_dist:
            min_dist = dist
            index = i

    return (list1[i], list2[i])  # Closest point.


list1 = [1, 2, 1, 1]
list2 = [4, 7, 6, 3]

print(find_closest(list1, list2), "es la coordenada mas cercana")
如果您不能使用
math.hypot()
,那么基于
math.sqrt()
,编写自己的代码就相当容易了:

只是为了好玩:

这可以使用和的组合轻松完成:

导入数学
列表1=[1,2,1,1]
列表2=[4,7,6,3]
参考点=(列表1[0],列表2[0])
points=zip(列表1[1:],列表2[1:]
x、 y=最小值(点,键=λ点:(点[0]-参考点[0])**2+(点[1]-参考点[1])**2)
印刷品(x,,,,y,,“是塞尔卡纳的合作社”)
请注意,
abs
并不是真正必要的,因为2的幂的数字总是正的,而且因为我们只想比较,
sqrt
也不是必需的(如果
sqrt(x)>sqrt(y)
那么也
x>y
)。

只是为了好玩:

这可以使用和的组合轻松完成:

导入数学
列表1=[1,2,1,1]
列表2=[4,7,6,3]
参考点=(列表1[0],列表2[0])
points=zip(列表1[1:],列表2[1:]
x、 y=最小值(点,键=λ点:(点[0]-参考点[0])**2+(点[1]-参考点[1])**2)
印刷品(x,,,,y,,“是塞尔卡纳的合作社”)

请注意,
abs
并不是真正必要的,因为2的幂的数字总是正的,而且因为我们只想比较,
sqrt
也不是必需的(如果
sqrt(x)>sqrt(y)
那么也
x>y
)。

比较距离时不需要做平方根,只是三角洲平方和。min函数可与其关键参数一起使用,以根据计算值获取列表(或迭代器)中的最小项。使用zip,您可以将这两个列表组合成更有用的坐标对。Zip是一个迭代器,与min函数兼容:

list1 = [1, 2, 1, 1]
list2 = [4, 7, 6, 3]

points = zip(list1,list2)  # combine lists into (x,y) points
bx,by  = next(points)      # get the base point

# comparative distance calculation (square root not needed)
def distance(point): px,py = point; return (px-bx)**2+(py-by)**2

# minimum of remaining points based on distance calculation
px,py = min(points,key=distance)

print((px,py),"is nearest to",(bx,by))

# (1, 3) is nearest to (1, 4)
如果您正在寻找一个不使用min或zip的解决方案,这里有一个基于迭代器的简单循环,它将生成所需的结果。迭代器允许您在不使用索引的情况下同时遍历这两个列表(在Python中,索引被认为是丑陋的)

ix,iy=iter(列表1),iter(列表2)#列表迭代器
bx,by=next(ix),next(iy)#基点
cx,cy,cd=无,无,无#最近点和距离
对于ix中的px:
py=下一个(iy)#通过迭代器进行
dxy=(px-bx)**2+(py-by)**2#计算比较距离
如果cd为无或dxy