Tuples are immutable objects in Python. This means that they cannot be changed once they are initialized. First how can you access this tuple?
Let's make your dictionary as follows
import random
d={}
num_points=8
for i in range(num_points):
d[i]=(random.randrange(-250,251),random.randrange(-250,251))
print(d)
{0: (229, -137), 1: (178, 71), 2: (7, 19), 3: (180, 150), 4:
(-126, -65), 5: (-235, 80), 6: (-174, -241), 7: (200, 16)}
Then to access one of these 2D points we can do
d[0]
(229, -137)
Then we can try and access the first number of this coordinate as
d[0][0]
229
But now lets try to change this value as
d[0][0] = d[0][0] + 10
--------------------------------------------------------------------------- TypeError Traceback (most recent call
last) in ()
----> 1 d[0][0] = d[0][0] + 10
TypeError: 'tuple' object does not support item assignment
This does not work. It tells us that a tuple does not support item assignment. It cannot be changed.
Use a list
You should instead use a list. Replace your ( )
to [ ]
in the coordinate creation step.
import random
d={}
num_points=8
for i in range(num_points):
d[i]=[random.randrange(-250,251),random.randrange(-250,251)]
Now we can do this and it will add 10 to the first dimension of the first coordinate.
d[0][0] = d[0][0] + 10
Use a numpy matrix
You can set each row to be each coordinate and then the columns represent the first and second value in the coordinate.
import numpy as np
num_points=8
d = np.zeros((num_points, 2))
for i in range(num_points):
d[i,0], d[i,1]= random.randrange(-250,251), random.randrange(-250,251)
d[0]
array([-233., 92.])
d[0,1] += 100
array([-233., 192.])