python - 如何计算列表列表并返回 float 列表

标签 python python-3.x list

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

但是,我无法计算 3 个列表的列表,也无法返回 float 列表。

#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((左 - 右)**2 + (下 - 上)**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]

最佳答案

正如错误所述,除非您覆盖/重载 __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]

关于python - 如何计算列表列表并返回 float 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56205752/

相关文章:

python 3.x : How to compare two lists containing dictionaries where order doesn't matter

python - 使用带有分类数据的 seaborn barplot 的困难

python - 更多问题 'simple' 杂货 list

python - 如何将 .txt 读入 Python 对象列表?

python - 有条件地从列表中选择下一个元素

c# - 像 ForEach 一样使用 List<T>.RemoveAll 有什么缺点?

python - Python中的特定洗牌列表

python - 避免在 Sphinx 文档中扩展 sys.argv 关键字参数

python - 有没有办法使用 Pandas Dataframes 在 Excel 中设置敏感度标签?

list - 如何获得两个列表之间的差异?