ccc
ccc

Reputation: 574

Python - updating a set

I'm trying to update a set of nodes with their positions (coordinates) using Python.

n = 5  #number of current nodes
vertices = []
vertices=list(range(n)) # list of vertices=[0,1,2,3,4]

I'm creating a random set of points as follows for each element in vertices:

pos = {i:(random.randint(0,50),random.randint(0,50)) for i in vertices}

Also, I have another list(called "s") with some coordinates and I'm enumerating those elements in "s" to add an index(to continue with indices in "vertices") to each coordinate in "s".

for index, i in enumerate(s , vertices[-1]+1):
    print(index, i)

Then, I'm getting my output as

5 (29.5, 34.0)
6 (20.0, 25.75)
7 (23.75, 36.0)

Now I need to update my set "pos" with these points and their corresponding indices. How can I do that?

Upvotes: 2

Views: 82

Answers (1)

nehem
nehem

Reputation: 13642

You can assign like this while iterating itself instead of just printing

for index, i in enumerate(s, vertices[-1] + 1):
    pos[index] = i

And finally, the pos will be something like

{0: (0, 10), 1: (16, 33), 2: (17, 36), 3: (40, 13), 4: (26, 39), 5: (29.5, 34.0), 6: (20.0, 25.75), 7: (23.75, 36.0)}

Upvotes: 3

Related Questions