Python,在2-d列表中找到邻居 [英] Python, finding neighbors in a 2-d list

查看:316
本文介绍了Python,在2-d列表中找到邻居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里是这个问题,我有一个2-d字符'T'和'F'的列表,给定坐标我需要得到它的所有邻居。我有这个:

  from itertools import product,starmap 
x,y =(5,5)
cell = starmap(λa,b:(x + a,y + b),乘积((0,-1,+1),(0,-1,+1)))

from 确定单元格二维列表的邻居但是它只会给我一个协调器列表,所以我仍然提取值后缀。我希望搜索和检索完成一步,所以findNeighbors(5,5)将返回F,T,F,F,...而不是(5,4),(5,6),(4, 5),(4,4)...有一个快速的做法吗?解决方案可以包括除列表之外的结构以保存初始信息

解决方案

以下内容应该适用,只需稍微适应到目前的代码:

  from itertools import product,starmap,islice 

def findNeighbors(grid, x,y):
xi =(0,-1,1)如果0 < x < len(grid) - 1 else((0,-1)如果x> 0 else(0,1))
yi =(0,-1,1) y < len(grid [0]) - 1 else((0,-1)如果y> 0 else(0,1))
返回islice(starmap((lambda a,b:grid [x + a] [y + b]),产品(xi,yi)),1,无)

 >>> grid = [[0,1,2,3],
... [4,5,6,7],
... [8,9,10,11],
... [12,13,14,15]]
>>> list(findNeighbors(grid,2,1))#找到9
[8,10,5,4,6,13,12,14]的邻居
>>>列表(findNeighbors(grid,3,3))#找到15
的邻居[14,11,10]

为了清楚起见,这里是一些没有所有itertools魔术的等效代码:

  def findNeighbors (grid,x,y):
如果0 < x < len(grid) - 1:
xi =(0,-1,1)#这不是第一行或最后一行,所以我们可以看看上面和下面
elif x> 0:
xi =(0,-1)#这是最后一行,所以我们只能看到
以上的
$ b xi =(0,1)#这是第一行,所以我们只能看下面
#下面的行完成与上面的代码相同的东西,但对于列
yi =(0,-1,1)如果0 < y < len(grid [0]) - 1 else((0,-1)如果y> 0 else(0,1))
for a in xi:
for b in yi:
如果一个== b == 0:#此值被跳过使用原始代码中的islice
continue
yield grid [x + a] [y + b]


So here's the issue, I have a 2-d list of characters 'T' and 'F', and given coordinates I need to get all of its neighbors. I have this:

from itertools import product, starmap
x, y = (5, 5)
cells = starmap(lambda a, b: (x + a, y + b), product((0, -1, +1), (0, -1, +1))) 

from determining neighbors of cell two dimensional list But it will only give me a list of coordinantes, so i still fetch the values afterwords. I'd like the search and retrieval done in one step, so findNeighbors(5,5) would return F,T,F,F,... instead of (5, 4), (5, 6), (4, 5), (4, 4)... Is there a fast way of doing this? The solutin can include a structure other than a list to hold the initial information

解决方案

The following should work, with just a minor adaptation to the current code:

from itertools import product, starmap, islice

def findNeighbors(grid, x, y):
    xi = (0, -1, 1) if 0 < x < len(grid) - 1 else ((0, -1) if x > 0 else (0, 1))
    yi = (0, -1, 1) if 0 < y < len(grid[0]) - 1 else ((0, -1) if y > 0 else (0, 1))
    return islice(starmap((lambda a, b: grid[x + a][y + b]), product(xi, yi)), 1, None)

For example:

>>> grid = [[ 0,  1,  2,  3],
...         [ 4,  5,  6,  7],
...         [ 8,  9, 10, 11],
...         [12, 13, 14, 15]]
>>> list(findNeighbors(grid, 2, 1))   # find neighbors of 9
[8, 10, 5, 4, 6, 13, 12, 14]
>>> list(findNeighbors(grid, 3, 3))   # find neighbors of 15
[14, 11, 10]

For the sake of clarity, here is some equivalent code without all of the itertools magic:

def findNeighbors(grid, x, y):
    if 0 < x < len(grid) - 1:
        xi = (0, -1, 1)   # this isn't first or last row, so we can look above and below
    elif x > 0:
        xi = (0, -1)      # this is the last row, so we can only look above
    else:
        xi = (0, 1)       # this is the first row, so we can only look below
    # the following line accomplishes the same thing as the above code but for columns
    yi = (0, -1, 1) if 0 < y < len(grid[0]) - 1 else ((0, -1) if y > 0 else (0, 1))
    for a in xi:
        for b in yi:
            if a == b == 0:  # this value is skipped using islice in the original code
                continue
            yield grid[x + a][y + b]

这篇关于Python,在2-d列表中找到邻居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆