Python 如何计算浮点数列表和返回浮点数列表

Python 如何计算浮点数列表和返回浮点数列表,python,python-3.x,list,Python,Python 3.x,List,我需要计算每个网格的斜率,逻辑如下: 水平渐变:栅格的左侧高度减去栅格的右侧高度 垂直坡度:网格的上部高度减去网格的下部高度 返回:水平和垂直梯度的平方和的平方根 但是,我无法计算3个列表的列表,并返回浮动列表 #calculate slope main program def find_slope(map_of_heights, x, y): ind_row = len(map_of_heights)-1 ind_column = len(map_of_heights[0])

我需要计算每个网格的斜率,逻辑如下: 水平渐变:栅格的左侧高度减去栅格的右侧高度 垂直坡度:网格的上部高度减去网格的下部高度
返回:水平和垂直梯度的平方和的平方根

但是,我无法计算3个列表的列表,并返回浮动列表

#calculate slope main program 
def find_slope(map_of_heights, x, y):
    ind_row = len(map_of_heights)-1
    ind_column = len(map_of_heights[0])-1

    if y in range(1, ind_column) and x in range(1, ind_row):
        #main logic
        dx = map_of_heights[y][x-1] - map_of_heights[y][x+1]
        dy = map_of_heights[y+1][x] - map_of_heights[y-1][x]

    #different cases when the point is at the edge     
    elif x == 0 and y != 0 and y!= ind_row:
        dx = 0 - map_of_heights[y][x+1]
        dy = map_of_heights[y+1][x] - map_of_heights[y-1][x]
    elif y == 0 and x != 0 and x != ind_column:
        dx = map_of_heights[y][x-1] - map_of_heights[y][x+1]
        dy = map_of_heights[y+1][x] - 0
    elif x == ind_column and y != 0 and y!= ind_row:
        dx = map_of_heights[y][x-1] - 0
        dy = map_of_heights[y+1][x] - map_of_heights[y-1][x]
    elif y == ind_row and x != 0 and x != ind_column:
        dx = map_of_heights[y][x-1] - map_of_heights[y][x+1]
        dy = 0 - map_of_heights[y-1][x]
    elif x == 0 and y == 0:
        dx = 0 - map_of_heights[y][x+1]
        dy = map_of_heights[y+1][x] - map_of_heights[0][x]
    elif x == ind_column and y == 0:
        dx = map_of_heights[y][x-1] - 0
        dy = map_of_heights[y+1][x] - 0
    elif x == 0 and y == ind_row:
        dx = 0 - map_of_heights[y][x+1]
        dy = 0 - map_of_heights[y-1][x]
    elif x == ind_column and y == ind_row:
        dx = map_of_heights[y][x-1] - 0
        dy = 0 - map_of_heights[y-1][x]
    return math.sqrt(dx**2 + dy**2)
例如,在上面的测试_映射中,当x=1和y=1时,对于第一个网格

[[0, 1, 2], 
[2, 10, 4], 
[3, 4, 5]]]
我选择的值是10,因此左边的值是2,右边的值是4,下面的值是4,上面的值是1。 然后应用公式sqrt((左-右)**2+(下-上)**2),3.60551275463989作为结果

TypeError: unsupported operand type(s) for -: 'list' and 'list'


正如错误所说,除非覆盖/重载
\uuuuuuu sub\uuuu
方法,否则不能使用“-”运算符减去两个列表。但有一种更简单的方法可以做到这一点:

f = lambda a, b: [a[i]-b[i] for i in range(len(a))]
例如:

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> f(a, b)
[-3, -3, -3]

正如错误所说,除非覆盖/重载
\uuuuuuu sub\uuuu
方法,否则不能使用“-”运算符减去两个列表。但有一种更简单的方法可以做到这一点:

f = lambda a, b: [a[i]-b[i] for i in range(len(a))]
例如:

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> f(a, b)
[-3, -3, -3]

正如Javadmk已经指出的那样,您不能简单地使用-运算符对列表进行细分。 如果您希望逐个元素在列表上执行类似的操作,我建议使用numpy,因为它支持这种开箱即用的操作

使用Javadmk的解决方案给我带来了另一个错误,因为下面这行不支持对列表的操作:
return math.sqrt(dx**2+dy**2)

如果将此行添加到导入中:

import numpy as np
并按如下方式替换列表初始化:

test_maps = np.array([[[0, 1, 2], [2, 10, 4], [3, 4, 5]], [[10, 1, 2], [2, 3, 4], [3, 4, 5]], [[0, 1, 2], [2, 3, 4], [3, 4, 10]],  [[0, 1, 10], [2, 3, 10], [3, 4, 10]]])
return np.sqrt(dx**2 + dy**2)
平方根的计算如下:

test_maps = np.array([[[0, 1, 2], [2, 10, 4], [3, 4, 5]], [[10, 1, 2], [2, 3, 4], [3, 4, 5]], [[0, 1, 2], [2, 3, 4], [3, 4, 10]],  [[0, 1, 10], [2, 3, 10], [3, 4, 10]]])
return np.sqrt(dx**2 + dy**2)

然后您的代码将产生一个结果。但这并不是您所期望的,因此您的代码中可能有一些逻辑错误。

正如Javadmk已经指出的那样,您不能简单地使用-运算符对列表进行细分。 如果您希望逐个元素在列表上执行类似的操作,我建议使用numpy,因为它支持这种开箱即用的操作

使用Javadmk的解决方案给我带来了另一个错误,因为下面这行不支持对列表的操作:
return math.sqrt(dx**2+dy**2)

如果将此行添加到导入中:

import numpy as np
并按如下方式替换列表初始化:

test_maps = np.array([[[0, 1, 2], [2, 10, 4], [3, 4, 5]], [[10, 1, 2], [2, 3, 4], [3, 4, 5]], [[0, 1, 2], [2, 3, 4], [3, 4, 10]],  [[0, 1, 10], [2, 3, 10], [3, 4, 10]]])
return np.sqrt(dx**2 + dy**2)
平方根的计算如下:

test_maps = np.array([[[0, 1, 2], [2, 10, 4], [3, 4, 5]], [[10, 1, 2], [2, 3, 4], [3, 4, 5]], [[0, 1, 2], [2, 3, 4], [3, 4, 10]],  [[0, 1, 10], [2, 3, 10], [3, 4, 10]]])
return np.sqrt(dx**2 + dy**2)

然后您的代码将产生一个结果。但是,它不是您所期望的,因此您的代码中可能有一些逻辑错误。

x和y的初始值是多少?你能在解释的同时添加一个例子来得到你想要的结果吗!另外,
map\u of_heights[*][*]
是一个列表,您正在尝试减去两个列表!我已经添加了测试用例,x=1,y=1,还添加了一个示例以及您想要的结果的解释@BKCN@DeveshKumarSingh好的,我更新了一个例子来解释它是如何工作的。例如,在上面的测试图中,当x=1和y=1时,对于第一个网格,[[0,1,2],[2,10,4],[3,4,5]],我选择的值是10,因此左侧的值是2,右侧的值是4,下部的值是4,上部的值是1。然后应用公式sqrt((左-右)**2+(下-上)**2),3.60551275463989作为结果。相同的公式和技术适用于所有度量@BCKN?x和y的初始值是多少?你能在解释的同时添加一个例子来得到你想要的结果吗!另外,
map\u of_heights[*][*]
是一个列表,您正在尝试减去两个列表!我已经添加了测试用例,x=1,y=1,还添加了一个示例以及您想要的结果的解释@BKCN@DeveshKumarSingh好的,我更新了一个例子来解释它是如何工作的。例如,在上面的测试图中,当x=1和y=1时,对于第一个网格,[[0,1,2],[2,10,4],[3,4,5]],我选择的值是10,因此左侧的值是2,右侧的值是4,下部的值是4,上部的值是1。然后应用公式sqrt((左-右)**2+(下-上)**2),3.60551275463989作为结果。相同的公式和技术适用于所有公制@BCKN?