I have put all of my code for context. Basically it should print a list of vectors depending on which route the user selects. As it stands it prints the vectors to the screen like this:
[12, 2]
[12, 3]
[11, 3]
...
...
which is what i need, however, i also want the printed vectors to be written to a list in the order they are printed so i can call on them later on i.e. vector_list = [[12, 2], [12, 3], 11, 3]]....
I am very new to this and learning as I go so thank you for your patience. Below will create a list called vector_list but it will just save the same vector i.e. [12, 2] over and over again. I think my issue is i am using .append? I originally wanted to try something like vector_list = vector_list x but it says vector_list is not defined?
else:
print(start)
vector_list.append(start)
vectors()
print(vector_list)
The complete code for reference:
route001 = (3, 12, 'S', 'S', 'W', 'S', 'S', 'S', 'E', 'E', 'E', 'S', 'S', 'W',
'W', 'S', 'E', 'E', 'E', 'E', 'N', 'N', 'N', 'N', 'W', 'N', 'N',
'E', 'E', 'S', 'E', 'S', 'E', 'S', 'S', 'W', 'S', 'S', 'S', 'S',
'S', 'E', 'N', 'E', 'E')
route002 = (12, 11, 'W', 'W', 'S', 'S', 'S', 'W', 'W', 'N', 'N', 'N', 'W', 'W',
'W', 'S', 'S', 'S', 'S', 'E', 'E', 'S', 'W', 'W', 'W', 'W', 'N', 'N',
'W', 'W', 'S', 'S', 'S', 'S', 'E', 'E', 'E', 'S', 'E', 'S', 'E', 'S')
route003 = (3, 12, 'S', 'S', 'W', 'S', 'S', 'S', 'W', 'W', 'W', 'S', 'S', 'W',
'W', 'S', 'E', 'E', 'E', 'E', 'N', 'N', 'N', 'N', 'W', 'N', 'N', 'E',
'E', 'S', 'E', 'S', 'E', 'S', 'S', 'W', 'S', 'S', 'S', 'S', 'S', 'E',
'N', 'E', 'E')
route_selection = input('Select route 1, 2 or 3 ')
if route_selection == "1":
selectedRoute = route001
elif route_selection == "2":
selectedRoute = route002
elif route_selection == "3":
selectedRoute = route003
else:
print("error")
start = [selectedRoute[0]] [selectedRoute[1]]
directions = selectedRoute[2:]
coordinates = {"N": [0, 1], 'E': [1, 0], 'S': [0, -1], 'W': [-1, 0]}
vector_list = []
def vectors():
for d in directions:
dx, dy = coordinates[d]
start[0] = dx
start[1] = dy
if start[0] < 0 or start[0] > 12:
print('Error: This route goes outside the grid')
break
elif start[1] < 0 or start[1] > 12:
print('Error: This route goes outside the grid')
break
else:
print(start)
vector_list.append(start)
vectors()
print(vector_list)
CodePudding user response:
You can also try to print them in a loop. something like this.
for vector in vector_list: print(vector) // or print(vector, end=',')
CodePudding user response:
the problem is list is a mutable object in python(i assume you don't know what that is, i am encouraging you to google it:))
basically that means, when you append you list to the "list of vectors", it does NOT make a copy of that list and stores in the list of vectors, it(very roughly speaking, not that accurate) keeps a pointer to the list and stores it in the list of vectors
when you change your list, all the entries in the list of vectors change, because they are pointing to the same list you append
so, what you could do, for example, is make a copy of that list and store it inside "list of vectors"
just change this line:
vector_list.append(start)
to:
vector_list.append(start.copy())
and it should work, there might be a more efficient way of doing it, but i don't think you care about that to much in this case as a beginner(i didn't look on all of your logic, just on the specific thing you asked)