Python dijkstra'中的逻辑错误;二维阵列的s算法

Python dijkstra'中的逻辑错误;二维阵列的s算法,python,arrays,logic,dijkstra,Python,Arrays,Logic,Dijkstra,我的项目是创建一个python程序,在用户可以输入的一组节点上使用dijkstra的最短路径算法,我的想法是,我可以在用户输入的任何大小的地图上执行。 然而,我还没有深入到这一点,因为我在开始时遇到了一个错误 #if user wants a small map i.e. less than 27 nodes, the nodes will be named differently to if it is a large map global array_type #creates the li

我的项目是创建一个python程序,在用户可以输入的一组节点上使用dijkstra的最短路径算法,我的想法是,我可以在用户输入的任何大小的地图上执行。 然而,我还没有深入到这一点,因为我在开始时遇到了一个错误

#if user wants a small map i.e. less than 27 nodes, the nodes will be named differently to if it is a large map
global array_type
#creates the list
node_list=[]
node_array=[]
#makes sure the user only inputs a valid number of nodes
def check_int(x):
    while True:
        try:
            #checks if node is integer
            int(x)
            #checks if node is negative
            if int(x)<1:
                #if it is, then it changes it to'x'
                x='x'
                #this means that it is picked up as a value error and passed to the except
                int(x)
            #returns the number of nodes if it is valid
            return(x)
        except ValueError:
            print('only a whole positive number of nodes')
            x= input('how many nodes in your map?   ')

node_no= input('how many nodes in your map?   ')
node_no=check_int(node_no)
#if there are less than 27 nodes then they can be labled a, b, c...
if int(node_no) < 27:
    #creates a list with all the nodes in
    for i in range(int(node_no)):
        node_list.append(chr(int(i)+65))
        node_array.append(node_list)
    array_type=1
    #this is what the node list should stay the entire time
    print('node list=' + str(node_list))
#if there are more than 26 nodes then they will be labled a1, a2, a3...
elif int(node_no) >26:
    #creates a list with all the nodes in
    for i in range(int(node_no)):
        node_list.append('A' + str(i+1))
        node_array.append(node_list)
    array_type=2
    print('node list=' + str(node_list))
#creates a 2d array
for i in range(len(node_list)):
    for i2 in range(len(node_list)):
        #the error is here
        #for some reason this line changes the values inside 'node_list'
        #as you can see there is nowhere that i am redifining node_list
        #have i used incorrect syntax? or is this just an incorrect method to do what i want?
#---------------------------------------------------------------------
        node_array[i][i2]=str(node_list[i])+str(node_list[i2])
#---------------------------------------------------------------------
        print('node list='+str(node_list))
        print('node array='+str(node_array))
#如果用户想要一个小地图,即少于27个节点,则节点的命名将与大地图不同
全局数组类型
#创建列表
节点列表=[]
节点_数组=[]
#确保用户只输入有效数量的节点
def检查_int(x):
尽管如此:
尝试:
#检查节点是否为整数
int(x)
#检查节点是否为负
如果int(x)26:
#创建包含中所有节点的列表
对于范围内的i(int(节点号)):
node_list.append('A'+str(i+1))
节点\数组.追加(节点\列表)
数组类型=2
打印('node list='+str(node_list))
#创建二维阵列
对于范围内的i(len(节点列表)):
对于范围内的i2(len(节点列表)):
#错误就在这里
#出于某种原因,此行更改了“节点列表”中的值
#正如您所看到的,我没有重新定义节点列表的地方
#我是否使用了错误的语法?或者这只是一个不正确的方法来做我想做的事?
#---------------------------------------------------------------------
节点数组[i][i2]=str(节点列表[i])+str(节点列表[i2])
#---------------------------------------------------------------------
打印('node list='+str(node_list))
打印('node array='+str(node_array))
如果输入值2,则我希望数组如下所示:

[['AA','AB'],['BA','BB']]

但结果是:

[['AABAA','AABAAB'],['AABAA','AABAAB']]

如果值为3,则应如下所示:

[['AA'、'AB'、'AC']、['BA'、'BB'、'BC']、['CA'、'CB'、'CC']

但实际上它看起来是这样的:

[['aababaacaabaa'、'aababacaabaab'、'aabababacaabaac']、['aabababaacaabaa'、'aababacaabaab'、'aababacaabaab'、'aababacaabaab'、'aababacaabaac']

我之所以要这样做,是因为数组中的每个单元格都代表不同的旅程,然后我会询问您可以从哪个节点到达哪个节点(目前还不打算这样做),这将允许用户定义每个链接的权重

我花了几个小时研究这个问题,以确保我没有使用错误的语法,但我无法找到任何有帮助的东西,尽管我可能是在搜索错误的东西


如果您能够解决我一直遇到的问题或提供替代解决方案,我将非常感激,我知道最好不要使用try/except和全局变量。我主要关注的是在使其尽可能高效之前获得有效的结果。

因为
节点列表
已成为
的一部分节点数组
,修改
节点数组
也会修改
节点列表
。如果您不想发生这种情况,可以复制
节点列表
,例如使用
节点列表[:]

下面是一个简单的例子:

>>> l = [1,2]
>>> l2 = [l,l]
>>> l
[1, 2]
>>> l2
[[1, 2], [1, 2]]
>>> l2[0][1]=3
>>> l
[1, 3]

谢谢,我能够解决我的问题:)但不幸的是,它直接导致了另一个错误,我觉得最好还是把它作为一个全新的问题来问,谢谢你的回答