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

时间:2019-05-19 07:45:05

标签: 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])-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)
#the test height maps
test_maps = [
        [[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]]]

例如,在上面的test_maps中,对于第一个网格,当x = 1且y = 1时,

[[0, 1, 2], 
[2, 10, 4], 
[3, 4, 5]]]

我选择的值为10,因此左侧的值为2,右侧的值为4,下部为4,上部为1。 然后应用公式sqrt((left-right)** 2 +(lower-upper)** 2),3.605551275463989作为结果。

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

#test case
find_slope(test_maps, 1, 1)
#expected output
[3.605551275463989, 3.605551275463989, 3.605551275463989, 8.54400374531753]

2 个答案:

答案 0 :(得分:0)

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

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]

答案 1 :(得分:0)

正如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)

然后您的代码将产生结果。不过,这不是您所期望的,因此您的代码中某处可能会出现逻辑错误。