Python 在列表中查找列表中的数字
在列表环境中,我很难在列表中找到周围的数字。其思想是,列表彼此下方形成一个网格Python 在列表中查找列表中的数字,python,list,Python,List,在列表环境中,我很难在列表中找到周围的数字。其思想是,列表彼此下方形成一个网格 grid= [[1, 5, 4, 1], [2, 5, 3, 2], [6, 3, **6**, 3], [1, 4, 2, 1]] 我尝试编写的函数具有给定值,即数字的位置和网格本身。程序应该找到周围的数字。e、 g.用这个 >>> grid= [[1, 5, 4, 1], [2, 5, 3, 2], [6, 3, **6**, 3], [1, 4
grid= [[1, 5, 4, 1],
[2, 5, 3, 2],
[6, 3, **6**, 3],
[1, 4, 2, 1]]
我尝试编写的函数具有给定值,即数字的位置和网格本身。程序应该找到周围的数字。e、 g.用这个
>>> grid= [[1, 5, 4, 1], [2, 5, 3, 2], [6, 3, **6**, 3], [1, 4, 2, 1]]
>>> neighbours(2, 2, grid)
{1, 2, 3, 4, 5}
在本例中,选择的元素是粗体6。
我不知道如何做到这一点,而不经常超出索引或使代码非常困难
任何帮助都将不胜感激请尝试以下代码:
def neighbours(m, i, j, dist=1):
neighbors = []
i_min = max(0, i-dist)
i_max = i+dist+1
j_min = max(0, j-dist)
j_max = j+dist+1
for row in m[i_min:i_max]:
neighbors.append(row[j_min:j_max])
# neighbors will hold all the neighbour elements as a list
# flatten the list to get the required outpout format
flat_list = [item for sublist in neighbors for item in sublist]
#remove duplicates from the list
flat_list = list(set(flat_list))
# remove the elemnt in question as it's not required for the output
flat_list.remove(grid[2][2])
return flat_list
这将给你所有的邻居,在你的例子中,m=grid,i,j=2,2。
注意:此示例用于理解每个步骤,可以修改以获得所需的输出。或使用
切片
对象
def neighbor(i, j, grid):
cut_rows = slice(max(i-1, 0), i+2)
rows = grid[cut_rows]
cut_cols = slice(max(j-1, 0), j+2)
throw_row = 0 if max(i-1, 0) == 0 and i != 1 else 1
throw_col = 0 if max(j-1, 0) == 0 and j != 1 else 1
rest = [r[cut_cols] for r in rows]
throw_away_center = rest[throw_row].pop(throw_col)
flat = [i for j in rest for i in j]
return set(flat)
更新以处理0,0和1,1。
但是现在整个事情看起来都很笨拙…我们需要首先创建一个所有邻居的
列表,然后将其作为集返回:
def neighbours(r, c, l):
ns = [] #declare a neighbours list
if r > 0: #check if our row is greater than 0
ns += l[r-1][max(0, c-1):c+2] #add to the neighbours the row - 1
if r < len(l)-1: #if not the last row
ns += l[r+1][max(0, c-1):c+2] #add row + 1 to the neighbours
if c > 0: #if column is greater than 0
ns.append(l[r][c-1]) #add the element to the left
if c < len(l[r])-1: #if column less than the right edge
ns.append(l[r][c+1]) #add the element to the right
return set(ns) #return the neighbours as a set
收集邻居和单元本身,然后移除单元本身
def neighbours(i, j, grid):
vals = sum((row[j-(j>0):j+2] for row in grid[i-(i>0):i+2]), [])
vals.remove(grid[i][j])
return set(vals)
是否安装了numpy或pandas?请使用numpy阵列。如果您不知道如何添加numpy,只需在windows cmd中键入以下行:“py-3.6-m pip install numpy”并用您的python版本替换3.6。如果有帮助的话,我会使用pycharm和anaconda(很抱歉,我在这方面很新),这看起来是正确的方法,但不是m
int
,在这种情况下m[I_min:I_max]
不起作用。对不起,忘了提一下,您可能需要将列表列表转换为平面列表,明天我们将对此进行查看,对不起,输入前有没有办法将这些列表展平?因为我的输入是列表中的列表。已经非常感谢你们俩了哇…你们至少可以让它更可读一点。这里的重点不是要赢得一场代码高尔夫比赛,而是要让OP明白你做了什么:p做得很好anywayz@Cajuu“实际上,我在测试其他值时发现了一个错误!因此,我现在已经把函数做得非常清楚:)@joeIddon这很有效,但对于一些例子来说,它不知怎么地忽略了右边的邻居。例如,邻居(4,3,[[4,3,6,4,3],[2,1,5,2,1],[5,7,8,7,5],[4,3,6,4,3],[2,1,5,2,1]]给我的结果是{3,4,5,6}忘记了最后一个1。@LibertyVerleysen很抱歉!有一个小的索引错误,我将r
切换为0。现在全部修复:)除非我有以下输入,否则这似乎可以工作:邻居(0,0,[[1,5,4,1],[2,5,3,2],[6,3,6,3],[1,4,2,1]])。在这里我得到一个指数超出范围时,扔掉中心试图预制。这就是你的意思吗?检查,然后你是指一个“如果”-语句?是的,这就是为什么其他人的代码中有max
。你希望如何包装,0,0的预期输出是什么?我的预期输出是2和5,因为2正好是1,5在1oki之下,所以你只需要一个角,然后max
技巧就可以了,等一下,我会用这个方法更新代码的
def neighbours(i, j, grid):
vals = sum((row[j-(j>0):j+2] for row in grid[i-(i>0):i+2]), [])
vals.remove(grid[i][j])
return set(vals)