Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 距离计算不起作用_Python_List_Math_Distance - Fatal编程技术网

Python 距离计算不起作用

Python 距离计算不起作用,python,list,math,distance,Python,List,Math,Distance,使用Python,我试图计算两点之间的距离,但我所写的东西不起作用。 我有两个列表,每行有两个元素,有效的坐标列表,我试图用毕达哥拉斯来计算它们的分离度,如果它们足够接近,就打印出来。 我有: 导入数学 对于范围(len(a))中的i:#a和c是导入的列表 对于范围内的j(len(c)): y=b[i,0]-d[j,0]#b和d是实际列表,它们已被清理,以便可以使用 z=b[i,1]-d[j,1] 定义f(y,z):(数学sqrt((y**2)+(z**2))) 如果f有两个问题: 您的函数不

使用Python,我试图计算两点之间的距离,但我所写的东西不起作用。 我有两个列表,每行有两个元素,有效的坐标列表,我试图用毕达哥拉斯来计算它们的分离度,如果它们足够接近,就打印出来。 我有:

导入数学
对于范围(len(a))中的i:#a和c是导入的列表
对于范围内的j(len(c)):
y=b[i,0]-d[j,0]#b和d是实际列表,它们已被清理,以便可以使用
z=b[i,1]-d[j,1]
定义f(y,z):(数学sqrt((y**2)+(z**2)))

如果f有两个问题:

  • 您的函数不返回任何内容
  • 你从不调用你的函数
使用
def
定义函数时,必须使用
return
返回结果:

def f(y, z): return math.sqrt(y**2 + z**2)
或者,当使用
lambda
时,
返回值是隐式的:

f = lambda y, z: math.sqrt(y**2 + z**2)

然后,您仍然需要调用函数(
f给定
b
d
,如下所示:

b = ((1,1),(2,2))
d = ((1.001,1),(2,2))
以下代码:

import math
import itertools

for point_a, point_b in itertools.product(b, d):
    y = point_a[0] - point_b[0]
    z = point_a[1] - point_b[1]
    distance = math.sqrt((y**2) + (z**2))
    if distance < 0.0056:
        print "%s => %s: %s" % (point_a, point_b, distance)

这是什么语言?看起来你在声明某种内联函数(
f
),但是没有明显的地方可以调用这个函数,看起来你在比较函数对象本身(
f
)有一个数字。当然,这可能是错误的,但这就是为什么你应该添加一个语言标记。我正在使用Python,很抱歉混淆了!然后我的其余注释看起来可能是正确的-你定义了
f
函数,但你从来没有调用过它,因此从来没有实际计算过距离。然后比较函数
 f
使用
0.0056
。我刚刚尝试了Tobias的建议,只要我删除“if”行,它就会起作用。我不确定如何包含它。
 dist = math.sqrt(y**2 + z**2)
 if dist <= 0.0056:
     print i, j, b[i, 0], b[i, 1], d[j, 0], d[j, 1], dist
b = ((1,1),(2,2))
d = ((1.001,1),(2,2))
import math
import itertools

for point_a, point_b in itertools.product(b, d):
    y = point_a[0] - point_b[0]
    z = point_a[1] - point_b[1]
    distance = math.sqrt((y**2) + (z**2))
    if distance < 0.0056:
        print "%s => %s: %s" % (point_a, point_b, distance)
(1, 1) => (1.001, 1): 0.001
(2, 2) => (2, 2): 0.0