【问题标题】:How to find largest surrounding value of a 2D list?如何找到二维列表的最大周围值?
【发布时间】:2018-08-06 06:52:52
【问题描述】:

你有一个类似下面的列表

list =  [ 
        [1,7,1,5,6],
        [1,8,9,1,5],
        [1,1,10,1,5],
        [2,1,1,1,1],
        [1,1,1,1,1]
]

从列表的中间开始,你将如何分析周围的数字并将你的位置更改为具有最大值的位置。所以在这种情况下,

arr[2][2] (10) -> arr[1][2] (9) -> arr[1][1] (8) -> arr[0][1] (7)

意思是从数组中间开始你如何只选择值

[8,9,1]
[1,10,1]
[1,1,1]

然后对具有最大价值的位置执行相同的操作 [9]。

到目前为止我所拥有的:

midLow=0
midHigh=0
rowLow=0
rowHigh = 0
# -- Columns -- 
evenOddCol = (len(arr)%2) # Find if there are even/off number of items in the list 
mid = (len(arr)/2) # Define Middle row 

if evenOddCol == 1: # If number of rows is odd, make no changes 
    mid = mid
else: # If number of rows is even, define the upper/lower list
    midLow = mid-1
    midHigh = mid
# -- Rows -- 
rowLen = (len(arr[1])) # Length of an arbitary row
evenOddRow = ((rowLen)%2) # Does row have an even/off number of elements 
rowMid = (rowLen/2) # Define the middle of the row 

if evenOddRow == 1: # If the number of rows is odd, make no changes
    rowMid = rowMid
else: # If the number of rows is even, define the upper/lower rows 
    rowLow = rowMid-1
    rowHigh = rowMid

# -- Logic --

return arr[mid][rowMid]

返回 10,二维列表数组的中间值。

返回中间值后,您必须确定最高的周围值。不知道该怎么做。

【问题讨论】:

  • 目标是什么?你求的这个递归函数,它是如何终止递归的?
  • @pstatix 该函数将在到达数组边缘时终止。表示第一行/最后一行或列中的任何变量。类似于寻找最大价值的迷宫,直到你到达终点。
  • 而且它必须总是在数组的中心初始化?
  • 当然你需要处理我们最大值是所有选项的情况(即质心周围的所有值都相同)
  • @pstatix 是的,这是正确的。然而,现在我主要只是在寻找一种有效的方法来分析周围的值。它必须始终初始化到数组的中心,是的。

标签: arrays python-3.x parsing math


【解决方案1】:

代码需要一些重构(尤其是像v in visited 这样的表达式),但输出正是您所需要的。

如果您在访问点时将list中的值更改为None,而不是累积访问点列表,则可以删除v in visited检查。

class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def add(self, other):
        return Vector(self.x + other.x, self.y + other.y)

    def __str__(self):
        return "(%s; %s)" % (self.x, self.y)

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

list =  [
    [1,7,1,5,6],
    [1,8,9,1,5],
    [1,1,10,1,5],
    [2,1,1,1,1],
    [1,1,1,1,1]
]

size = Vector(5, 5)

# All possible directions
directions = [
    Vector(1, 0),  # right
    Vector(1, 1),  # right and down
    Vector(0, 1),  # down
    Vector(-1, 1),  # left and down
    Vector(-1, 0),  # left
    Vector(-1, -1),  # left and up
    Vector(0, -1),  # up
    Vector(1, -1),  # right and up
]

# Getting value of interesting point except visited points
def get_val(v, visited):
    # x and y are swapped because the first dimension of
    # list is "rows" and the second dimension is "columns"
    return list[v.y][v.x] if v not in visited and 0 <= v.x < size.x and 0 <= v.y < size.y else None

# Getting the greater direction except visited points
def get_greater_direction(pos, visited):
    dir = max(directions, key=lambda d: get_val(pos.add(d), visited))
    return None if pos.add(dir) in visited else dir


position = Vector(2, 2)
visited = []
while True:
    print(position)
    visited.append(position)
    greater_direction = get_greater_direction(position, visited)
    if greater_direction is None:
        print("The travel is ended!")
        break
    position = position.add(greater_direction)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 2021-03-22
    • 2021-02-18
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多