【问题标题】:How to calculate the list of list and return list of floats如何计算列表列表并返回浮点列表
【发布时间】:2019-05-19 07:45:04
【问题描述】:

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

但是,我无法计算 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]

【问题讨论】:

  • xy 的初始值是多少?您能否添加一个示例以及解释以获得您想要的结果! map_of_heights[*][*] 也是一个列表,而您正在尝试减去两个列表!
  • 我已经添加了测试用例,x=1, y=1
  • 还添加一个示例以及您想要的结果的解释! @BKCN
  • @DeveshKumarSingh 好的,我更新了一个示例来解释它是如何工作的。比如上面的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。
  • 相同的公式和技术适用于所有指标@BCKN?

标签: python python-3.x list


【解决方案1】:

正如错误所说,您不能使用“-”运算符减去两个列表,除非您覆盖/重载 __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]

【讨论】:

    【解决方案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)
    

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

    【讨论】:

      猜你喜欢
      • 2021-07-03
      • 2020-08-12
      • 1970-01-01
      • 2012-09-06
      • 2013-12-15
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多