确定单元格二维列表的邻居
我有一个列表列表,类似于
[[1, 2, 3,],[4, 5, 6,],[7, 8, 9]]
。
以图形方式表示为:
1 2 3
4 5 6
7 8 9
我正在寻找一种优雅的方法来检查单元格的水平、垂直和对角邻居的值。例如,[0][2]的邻居是[0][1]、[1][1]和[1][2]或数字2、5、6。
现在我意识到,我可以这样做暴力攻击检查每个值 la:
[i-1][j]
[i][j-1]
[i-1][j-1]
[i+1][j]
[i][j+1]
[i+1][j+1]
[i+1][j-1]
[i-1][j+1]
但这很简单,我想我可以通过看到一些更优雅的方法来了解更多信息。
I have a list of lists, something like
[[1, 2, 3,],[4, 5, 6,],[7, 8, 9]]
.
Represented graphically as:
1 2 3
4 5 6
7 8 9
I'm looking for an elegant approach to check the value of neighbours of a cell, horizontally, vertically and diagonally. For instance, the neighbours of [0][2] are [0][1], [1][1] and [1][2] or the numbers 2, 5, 6.
Now I realise, I could just do a bruteforce attack checking every value a la:
[i-1][j]
[i][j-1]
[i-1][j-1]
[i+1][j]
[i][j+1]
[i+1][j+1]
[i+1][j-1]
[i-1][j+1]
But thats easy, and I figured I can learn more by seeing some more elegant approaches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
姆...
mb...
如果有人对选择直接(非对角)邻居的替代方法感到好奇,请看这里:
If someone is curious about alternative way to pick direct (non-diagonal) neighbors, here you go:
没有更干净的方法可以做到这一点。如果你真的想要你可以创建一个函数:
There's no cleaner way to do this. If you really want you could create a function:
这是你的列表:
所以 (x, y) 的水平邻居是 (x +/- 1, y)。
垂直邻居为 (x, y +/- 1)。
对角邻居为 (x +/- 1, y +/- 1)。
这些规则适用于无限矩阵。
为了确保邻居适合有限矩阵,如果初始 (x, y) 位于边缘,只需对邻居的坐标再施加一项限制 - 矩阵大小。
Here is your list:
So the horizontal neighbors of (x, y) are (x +/- 1, y).
The vertical neighbors are (x, y +/- 1).
Diagonal neighbors are (x +/- 1, y +/- 1).
These rules apply for an infinite matrix.
To make sure the neighbors fit into a finite matrix, if the initial (x, y) is at the edge, just apply one more restriction to the coordinates of neighbors - the matrix size.
我不知道它对你来说有多优雅,但它似乎无需任何硬编码即可工作。
I don't know how elegant it seems to you, but it seems to work w/o any hard-coding.
这会生成所有索引:
This generates all indices:
如果 lambda 使您感到畏惧,那么您就是了。但是 lambda 使您的代码看起来干净。@johniek_comp 有一个非常干净的解决方案 TBH
If lambdas daunt you here you are .But lambdas make your code look clean.@johniek_comp has a very clean solution TBH
受到之前答案之一的启发。
您可以使用 min() 和 max() 函数来缩短计算:
Inspired by one of the previous answers.
You can use min() and max() functions to shorten the calculations:
感谢 @JS_is_bad 提供有关邻居的重要提示。这是此问题的运行代码:
Thank you to @JS_is_bad for a great hint about the neighbors. Here is the running code for this problem:
受到约翰尼克的回答的启发,我的解决方案也检查了边界。
Inspired by johniek's answer here is my solution which also checks for boundaries.
也许您正在检查数独框。如果盒子是 nxn 并且当前单元格是 (x,y) 开始检查:
maybe you are checking a sudoku box. If the box is n x n and current cell is (x,y) start checking:
我不知道这是否被认为是干净的,但是这一行通过迭代它们并丢弃任何边缘情况为您提供了所有邻居。
I don't know if this is considered clean, but this one-liner gives you all neighbors by iterating over them and discarding any edge cases.
假设您有一个方阵:
使用
itertools.product
并感谢 Python 的 yield 表达式 和 < a href="https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists" rel="noreferrer">明星运算符,功能相当漂亮干但仍然足够可读。给定矩阵大小为 3,您可以(如果需要)将邻居收集到
列表
中:该函数的作用可以如下所示:
Assuming you have a square matrix:
Using
itertools.product
and thanks to Python's yield expression and star operator, the function is pretty dry but still readable enough.Given a matrix size of 3, you can then (if needed) collect the neighbours in a
list
:What the function does can be visualized as follows: