python, dijkstra的最短路径, typeerror - generator不支持item赋值

python, dijkstra's shortest path, type error - generator does not suppport item assignment

我最近发布了一个与我的项目有关的问题,因为我遇到了一个错误,该错误已解决,但它导致了一个新的不同错误,我认为在一个新问题中被问到会更好

#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))
#these two next lines are what i used to solve my previous error
#-----------------------------------------------------------------------
    node_list2[:]=node_list
    node_array.append(node_list2 for i in range(int(node_no)))
#-----------------------------------------------------------------------
    array_type=1
    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)):
#unfortunately when i solved my previous error my code started messing up here
#---------------------------------------------------------------------
        node_array[i][i2]=str(node_list[i])+str(node_list[i2])
#---------------------------------------------------------------------
        print('node list='+str(node_list))
        print('node array='+str(node_array))

我收到的结果应该是:(3 as node_no)

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

但是在我更改代码以使用生成器之前,我得到了这个:

[['CA', 'CB', 'CC'], ['CA', 'CB', 'CC'], ['CA', 'CB', 'CC']]

在进行研究后,我发现我必须使用生成器,但由于我确实实现了它,它现在显示 "TypeError: 'generator' object does not support item assignment" 我不知道如何解决这个问题,我是否应该使用不同的方法来解决我的最后一个问题?或者我应该用不同的方法分配给数组?

问题是由于 node_list2 只有一个内存地址,它的所有实例都指向该地址。这意味着如果你改变一个,它会改变所有的人。

解决方案

行中

node_array.append(node_list2 for i in range(int(node_no)))

改为

node_array.append(node_list2[:] for i in range(int(node_no)))

[:] 在列表上克隆它,本质上是用它自己的内存地址创建一个新的变量实例,而不是共享一个(这是之前发生的事情)