如何查看2d列表中的相邻项目 [英] How do I check adjacent items in 2d list

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

问题描述

我有以下2d列表,为此我创建了以下函数,它查看2 1并检查它是否相邻,因为它是相邻的,它之间必须有9个





I have the following 2d list, for which i have created the following function, which looks at 2 1's and checks if it is adjacent, for it to be adjacent it must have a 9 in between


[0, 0, 0, 0, 0]
[1, 9, 1, 9, 1]
[0, 0, 9, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 9, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 0, 0, 0]



我从网格创建两个列表
行的


I create two lists from the grid

lst = []
for rows, row in enumerate(grid):
    for cols, col in enumerate(row):
        if grid[rows][cols] in [1]:
            lst.append((rows, cols))
            
bondlst = []            
for rows, row in enumerate(grid):
    for cols, col in enumerate(row):
        if grid[rows][cols] in [9]:
            bondlst.append((rows, cols))       
            
            
print(lst)
print(bondlst)




def adjacentnode(nodea ,nodeb):
   if nodea[0] == nodeb[0] and nodea[1] == nodeb[1]+2 and nodea[1]-1 ==9:
        adjacent = True
   elif nodea[0] == nodeb[0] and nodea[1] == nodeb[1]-2 and nodea[1]-1 ==9:
        adjacent = True
   elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]+2 and nodea[1]-1 ==9:
        adjacent = True
   elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]-2 and nodea[1]-1 ==9:
        adjacent = True
   else:
        adjacent = False
   return adjacent

tempgraph = {}
for node in range(len(lst)):
    adjacencylist.append((lst[node] ,[]))
    for neighbour in range(len(lst)):
        adjacentnodes = (adjacentnode(lst[node] ,lst[neighbour]))
        print(adjacentnodes)

        if adjacentnodes == True:
            adjacencylist[node][1].append(lst[neighbour])

但对于每次调用我都会返回false而我看不清楚为什么

But for every call i make it returns false and i cannot see why

def adjacentnode(nodea ,nodeb):
   if nodea[0] == nodeb[0] and nodea[1] == nodeb[1]+2 and nodea[1]-1 ==9:
        adjacent = True
   elif nodea[0] == nodeb[0] and nodea[1] == nodeb[1]-2 and nodea[1]-1 ==9:
        adjacent = True
   elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]+2 and nodea[1]-1 ==9:
        adjacent = True
   elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]-2 and nodea[1]-1 ==9:
        adjacent = True
   else:
        adjacent = False
   return adjacent

tempgraph = {}
for node in range(len(lst)):
    adjacencylist.append((lst[node] ,[]))
    for neighbour in range(len(lst)):
        adjacentnodes = (adjacentnode(lst[node] ,lst[neighbour]))
        print(adjacentnodes)

        if adjacentnodes == True:
            adjacencylist[node][1].append(lst[neighbour])

但对于每次拨打我都会返回false而我看不清楚为什么



我尝试了什么:



谢谢你,这就是我有多远了/>

But for every call i make it returns false and i cannot see why

What I have tried:

thank you, this is how far i have got

def adjacentnode(nodea ,nodeb):
   if nodea[0] == nodeb[0] and nodea[1] == nodeb[1]+2 and nodea[1]-1 ==9:
        adjacent = True
   elif nodea[0] == nodeb[0] and nodea[1] == nodeb[1]-2 and nodea[1]-1 ==9:
        adjacent = True
   elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]+2 and nodea[1]-1 ==9:
        adjacent = True
   elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]-2 and nodea[1]-1 ==9:
        adjacent = True
   else:
        adjacent = False
   return adjacent

推荐答案

引用:

但是对于每次调用我都会返回false并且我看不清楚为什么

But for every call i make it returns false and i cannot see why



你的代码没有表现你期望的方式,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。 />
调试器在这里向你展示哟你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它找不到错误,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]



调试器只是向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于如何查看2d列表中的相邻项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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