1

i recently posted a question to do with my project because i encountered an error, that error was solved but it led to a new different error that i thought would be better off being asked in a new question

#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))

the outcome i recieve for this should be: (3 as node_no)

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

however before i chaned the code to use a generator i got this:

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

upon doing my reserch i found i had to use a generator, but since i did impliment it, it now says "TypeError: 'generator' object does not support item assignment" and i dont know how to solve this, should i have used a different method to solve my last problem? or should i assign to the array in a different method?

aidan
  • 85
  • 1
  • 2
  • 8
  • First of all on the line `for i in range(len(node_list)):` shouldn't it be `for i in range(len(node_array))`? – Ben10 Apr 16 '18 at 03:03
  • Also before creating the 2D array, could you please print out node_list, and node_array and tell me the output? – Ben10 Apr 16 '18 at 03:07
  • @Ben10 , no because i want it to be a 2d array with len(node_list) number of collumns and same number of rows – aidan Apr 16 '18 at 03:08
  • @Ben10 the node list is ['A', 'B', 'C'] and the node array is an empty list – aidan Apr 16 '18 at 03:10
  • Could you change this line, and check again what node array equals: `node_array.append(node_list2 for i in range(int(node_no)))` to `for i in range(int(node_no)): node_array.append(node_list2)` – Ben10 Apr 16 '18 at 03:14
  • Actually I think I've got it. Change `node_list2[:]=node_list` to `node_list2=node_list[:]` – Ben10 Apr 16 '18 at 03:18
  • @Ben10 `[['CA', 'CB', 'CC'], ['CA', 'CB', 'CC'], ['CA', 'CB', 'CC']]` – aidan Apr 16 '18 at 03:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169048/discussion-between-ben10-and-aidan). – Ben10 Apr 16 '18 at 03:20

1 Answers1

1

The problem is due to node_list2 having only one memory address, which all of it's instances were pointing to. That means if you change one, it changes all of them.

Solution

In the line

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

change it to

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

[:] on a list clones it, essentially creating a new variable instance with it's own memory address instead of sharing one (which was what was happening before)

Ben10
  • 460
  • 1
  • 3
  • 13